Skip to content

Commit c65d1a6

Browse files
drjdrpionl
authored andcommitted
Add Laravel 9 support with support of old Laravel versions
1 parent 149f911 commit c65d1a6

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
"test": "./vendor/bin/phpunit"
1616
},
1717
"require": {
18-
"illuminate/http": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0",
19-
"illuminate/console": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0",
20-
"illuminate/support": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0",
21-
"illuminate/filesystem": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0"
18+
"illuminate/http": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0 | ^9.0",
19+
"illuminate/console": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0 | ^9.0",
20+
"illuminate/support": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0 | ^9.0",
21+
"illuminate/filesystem": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0 | ^9.0"
2222
},
2323
"require-dev": {
24-
"laravel/laravel": "5.2 - 5.8 | ^6.0 | ^7.0 | ^8.0",
2524
"phpunit/phpunit": "5.7 | 6.0 | 7.0 | 7.5 | 8.4 | ^8.5 | ^9.3",
2625
"mockery/mockery": "^1.1.0 | ^1.3.0",
2726
"friendsofphp/php-cs-fixer": "^2.16.0",

src/Storage/ChunkStorage.php

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
66
use Illuminate\Support\Collection;
7+
use Illuminate\Filesystem\FilesystemAdapter;
78
use League\Flysystem\Adapter\Local;
9+
use League\Flysystem\Local\LocalFilesystemAdapter;
810
use League\Flysystem\FilesystemInterface;
911
use Pion\Laravel\ChunkUpload\ChunkFile;
1012
use Pion\Laravel\ChunkUpload\Config\AbstractConfig;
@@ -28,52 +30,61 @@ public static function storage()
2830
* @var AbstractConfig
2931
*/
3032
protected $config;
31-
3233
/**
3334
* The disk that holds the chunk files.
3435
*
35-
* @var FilesystemAdapter
36+
* @var FilesystemContract|FilesystemAdapter
3637
*/
3738
protected $disk;
38-
3939
/**
40-
* @var Local
40+
* @var Local|LocalFilesystemAdapter
4141
*/
4242
protected $diskAdapter;
43+
protected $isLocalDisk;
4344

4445
/**
45-
* Is provided disk a local drive.
46-
*
47-
* @var bool
46+
* @var
4847
*/
49-
protected $isLocalDisk;
48+
protected $usingDeprecatedLaravel;
5049

5150
/**
52-
* ChunkStorage constructor.
53-
*
54-
* @param FilesystemContract $disk the desired disk for chunk storage
55-
* @param AbstractConfig $config
51+
* @param FilesystemAdapter|FilesystemContract $disk the desired disk for chunk storage
52+
* @param AbstractConfig $config
5653
*/
57-
public function __construct(FilesystemContract $disk, $config)
54+
public function __construct($disk, $config)
5855
{
5956
// save the config
6057
$this->config = $config;
61-
62-
// cache the storage path
58+
$this->usingDeprecatedLaravel = class_exists(LocalFilesystemAdapter::class) === false;
6359
$this->disk = $disk;
6460

65-
$driver = $this->driver();
61+
if ($this->usingDeprecatedLaravel === false) {
6662

67-
// try to get the adapter
68-
if (!method_exists($driver, 'getAdapter')) {
69-
throw new RuntimeException('FileSystem driver must have an adapter implemented');
70-
}
63+
// try to get the adapter
64+
if (!method_exists($this->disk, 'getAdapter')) {
65+
throw new RuntimeException('FileSystem driver must have an adapter implemented');
66+
}
67+
68+
// get the disk adapter
69+
$this->diskAdapter = $this->disk->getAdapter();
7170

72-
// get the disk adapter
73-
$this->diskAdapter = $driver->getAdapter();
71+
// check if its local adapter
72+
$this->isLocalDisk = $this->diskAdapter instanceof LocalFilesystemAdapter;
73+
} else {
74+
$driver = $this->driver();
75+
76+
// try to get the adapter
77+
if (!method_exists($driver, 'getAdapter')) {
78+
throw new RuntimeException('FileSystem driver must have an adapter implemented');
79+
}
80+
81+
// get the disk adapter
82+
$this->diskAdapter = $driver->getAdapter();
83+
84+
// check if its local adapter
85+
$this->isLocalDisk = $this->diskAdapter instanceof Local;
86+
}
7487

75-
// check if its local adapter
76-
$this->isLocalDisk = $this->diskAdapter instanceof Local;
7788
}
7889

7990
/**
@@ -85,10 +96,14 @@ public function __construct(FilesystemContract $disk, $config)
8596
*/
8697
public function getDiskPathPrefix()
8798
{
88-
if ($this->isLocalDisk) {
99+
if ($this->usingDeprecatedLaravel === true && $this->isLocalDisk) {
89100
return $this->diskAdapter->getPathPrefix();
90101
}
91102

103+
if ($this->isLocalDisk) {
104+
return $this->disk->path('');
105+
}
106+
92107
throw new RuntimeException('The full path is not supported on current disk - local adapter supported only');
93108
}
94109

@@ -99,7 +114,7 @@ public function getDiskPathPrefix()
99114
*/
100115
public function directory()
101116
{
102-
return $this->config->chunksStorageDirectory().'/';
117+
return $this->config->chunksStorageDirectory() . '/';
103118
}
104119

105120
/**
@@ -119,7 +134,7 @@ public function files($rejectClosure = null)
119134

120135
return $filesCollection->reject(function ($file) use ($rejectClosure) {
121136
// ensure the file ends with allowed extension
122-
$shouldReject = !preg_match('/.'.self::CHUNK_EXTENSION.'$/', $file);
137+
$shouldReject = !preg_match('/.' . self::CHUNK_EXTENSION . '$/', $file);
123138
if ($shouldReject) {
124139
return true;
125140
}
@@ -182,7 +197,7 @@ public function disk()
182197
/**
183198
* Returns the driver.
184199
*
185-
* @return FilesystemInterface
200+
* @return FilesystemOperator|FilesystemInterface
186201
*/
187202
public function driver()
188203
{

0 commit comments

Comments
 (0)