protonemedia /
laravel-ffmpeg
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ProtoneMedia\LaravelFFMpeg\Filesystem; |
||
| 4 | |||
| 5 | use Illuminate\Contracts\Filesystem\Filesystem; |
||
| 6 | use Illuminate\Filesystem\FilesystemAdapter; |
||
| 7 | use Illuminate\Support\Facades\Storage; |
||
| 8 | use Illuminate\Support\Traits\ForwardsCalls; |
||
| 9 | use League\Flysystem\Adapter\Local; |
||
| 10 | use League\Flysystem\AdapterInterface; |
||
| 11 | use League\Flysystem\Filesystem as LeagueFilesystem; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @mixin \Illuminate\Filesystem\FilesystemAdapter |
||
| 15 | */ |
||
| 16 | class Disk |
||
| 17 | { |
||
| 18 | use ForwardsCalls; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string|\Illuminate\Contracts\Filesystem\Filesystem |
||
| 22 | */ |
||
| 23 | private $disk; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $temporaryDirectory; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \Illuminate\Filesystem\FilesystemAdapter |
||
| 32 | */ |
||
| 33 | private $filesystemAdapter; |
||
| 34 | |||
| 35 | public function __construct($disk) |
||
| 36 | { |
||
| 37 | $this->disk = $disk; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Little helper method to instantiate this class. |
||
| 42 | */ |
||
| 43 | public static function make($disk): self |
||
| 44 | { |
||
| 45 | if ($disk instanceof self) { |
||
| 46 | return $disk; |
||
| 47 | } |
||
| 48 | |||
| 49 | return new static($disk); |
||
| 50 | } |
||
| 51 | |||
| 52 | public static function makeTemporaryDisk(): self |
||
| 53 | { |
||
| 54 | $filesystemAdapter = app('filesystem')->createLocalDriver([ |
||
| 55 | 'root' => app(TemporaryDirectories::class)->create(), |
||
| 56 | ]); |
||
| 57 | |||
| 58 | return new static($filesystemAdapter); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Creates a fresh instance, mostly used to force a new TemporaryDirectory. |
||
| 63 | */ |
||
| 64 | public function clone(): self |
||
| 65 | { |
||
| 66 | return new Disk($this->disk); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Creates a new TemporaryDirectory instance if none is set, otherwise |
||
| 71 | * it returns the current one. |
||
| 72 | */ |
||
| 73 | public function getTemporaryDirectory(): string |
||
| 74 | { |
||
| 75 | if ($this->temporaryDirectory) { |
||
| 76 | return $this->temporaryDirectory; |
||
| 77 | } |
||
| 78 | |||
| 79 | return $this->temporaryDirectory = app(TemporaryDirectories::class)->create(); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function makeMedia(string $path): Media |
||
| 83 | { |
||
| 84 | return Media::make($this, $path); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns the name of the disk. It generates a name if the disk |
||
| 89 | * is an instance of Flysystem. |
||
| 90 | */ |
||
| 91 | public function getName(): string |
||
| 92 | { |
||
| 93 | if (is_string($this->disk)) { |
||
| 94 | return $this->disk; |
||
| 95 | } |
||
| 96 | |||
| 97 | return get_class($this->getFlysystemAdapter()) . "_" . md5(json_encode(serialize($this->getFlysystemAdapter()))); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getFilesystemAdapter(): FilesystemAdapter |
||
| 101 | { |
||
| 102 | if ($this->filesystemAdapter) { |
||
| 103 | return $this->filesystemAdapter; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($this->disk instanceof Filesystem) { |
||
| 107 | return $this->filesystemAdapter = $this->disk; |
||
|
0 ignored issues
–
show
|
|||
| 108 | } |
||
| 109 | |||
| 110 | return $this->filesystemAdapter = Storage::disk($this->disk); |
||
| 111 | } |
||
| 112 | |||
| 113 | private function getFlysystemDriver(): LeagueFilesystem |
||
| 114 | { |
||
| 115 | return $this->getFilesystemAdapter()->getDriver(); |
||
| 116 | } |
||
| 117 | |||
| 118 | private function getFlysystemAdapter(): AdapterInterface |
||
| 119 | { |
||
| 120 | return $this->getFlysystemDriver()->getAdapter(); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function isLocalDisk(): bool |
||
| 124 | { |
||
| 125 | return $this->getFlysystemAdapter() instanceof Local; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Replaces backward slashes into forward slashes. |
||
| 130 | * |
||
| 131 | * @param string $path |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public static function normalizePath(string $path): string |
||
| 135 | { |
||
| 136 | return str_replace('\\', '/', $path); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the full path for the file at the given "short" path. |
||
| 141 | * |
||
| 142 | * @param string $path |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function path(string $path): string |
||
| 146 | { |
||
| 147 | $path = $this->getFilesystemAdapter()->path($path); |
||
| 148 | |||
| 149 | return $this->isLocalDisk() ? static::normalizePath($path) : $path; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Forwards all calls to Laravel's FilesystemAdapter which will pass |
||
| 154 | * dynamic methods call onto Flysystem. |
||
| 155 | */ |
||
| 156 | public function __call($method, $parameters) |
||
| 157 | { |
||
| 158 | return $this->forwardCallTo($this->getFilesystemAdapter(), $method, $parameters); |
||
| 159 | } |
||
| 160 | } |
||
| 161 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.