Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 45 |
| FilesManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
462 | |
0.00% |
0 / 45 |
| from | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| to | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| checkDiskSetted | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 5 |
|||
| directory | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 9 |
|||
| file | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 9 |
|||
| put | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 7 |
|||
| allDestinationFiles | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| cleanTo | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 3 |
|||
| cleanFrom | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 3 |
|||
| createParentDir | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 4 |
|||
| 1 | <?php |
| 2 | namespace Qmp\Laravel\ToolsLaravel\FilesManager; |
| 3 | |
| 4 | |
| 5 | use Illuminate\Support\Facades\File; |
| 6 | use Illuminate\Filesystem\FilesystemAdapter; |
| 7 | |
| 8 | /** |
| 9 | * @example (new FileManager()) |
| 10 | * ->from(Storage::disk('disk_from')) |
| 11 | * ->to(Storage::disk('disk_to)) |
| 12 | * ->directory('from_path_folder', 'to_path_folder', true) |
| 13 | * ->file('from_path_file', 'to_path_file', false, true) |
| 14 | * ->put('to_path_folder', new HttpFile('/index.html'), 'new_index.html') |
| 15 | * ->allDestinationFiles('to_path_folder'); |
| 16 | */ |
| 17 | class FilesManager |
| 18 | { |
| 19 | /** |
| 20 | * @var FilesystemAdapter |
| 21 | */ |
| 22 | protected $fromDisk; |
| 23 | |
| 24 | /** |
| 25 | * @var FilesystemAdapter |
| 26 | */ |
| 27 | protected $toDisk; |
| 28 | |
| 29 | /** |
| 30 | * @param FilesystemAdapter $storageDisk |
| 31 | * |
| 32 | * @return FilesManager |
| 33 | */ |
| 34 | public function from(FilesystemAdapter $storageDisk): FilesManager |
| 35 | { |
| 36 | $this->fromDisk = $storageDisk; |
| 37 | |
| 38 | return $this; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param FilesystemAdapter $storageDisk |
| 43 | * |
| 44 | * @return FilesManager |
| 45 | */ |
| 46 | public function to(FilesystemAdapter $storageDisk): FilesManager |
| 47 | { |
| 48 | $this->toDisk = $storageDisk; |
| 49 | |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @throws FilesManagerException |
| 55 | */ |
| 56 | protected function checkDiskSetted() |
| 57 | { |
| 58 | if (!$this->fromDisk) { |
| 59 | throw new FilesManagerException('From disk have to be setted.'); |
| 60 | } |
| 61 | |
| 62 | if (!$this->toDisk) { |
| 63 | throw new FilesManagerException('To disk have to be setted.'); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param string $fromPath |
| 69 | * @param string $toPath |
| 70 | * @param bool $cleanToBefore = false |
| 71 | * @param bool $cleanFromAfter = false |
| 72 | * |
| 73 | * @return FilesManager |
| 74 | */ |
| 75 | public function directory(string $fromPath, string $toPath, bool $cleanToBefore = false, bool $cleanFromAfter = false): FilesManager |
| 76 | { |
| 77 | $this->checkDiskSetted(); |
| 78 | |
| 79 | $this->cleanTo($toPath, $cleanToBefore); |
| 80 | $this->createParentDir($toPath); |
| 81 | |
| 82 | if ($this->toDisk->exists($fromPath)) { |
| 83 | File::copyDirectory( |
| 84 | $this->fromDisk->path($fromPath), |
| 85 | $this->toDisk->path($toPath) |
| 86 | ); |
| 87 | |
| 88 | $this->cleanFrom($fromPath, $cleanFromAfter); |
| 89 | } |
| 90 | |
| 91 | return $this; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param string $fromPath |
| 96 | * @param string $toPath |
| 97 | * @param bool $cleanToBefore = false |
| 98 | * @param bool $cleanFromAfter = false |
| 99 | * |
| 100 | * @return FilesManager |
| 101 | */ |
| 102 | public function file(string $fromPath, string $toPath, bool $cleanToBefore = false, bool $cleanFromAfter = false) : FilesManager |
| 103 | { |
| 104 | $this->checkDiskSetted(); |
| 105 | |
| 106 | $this->cleanTo($toPath, $cleanToBefore); |
| 107 | $this->createParentDir($toPath); |
| 108 | |
| 109 | if ($this->fromDisk->exists($fromPath)) { |
| 110 | File::copy( |
| 111 | $this->fromDisk->path($fromPath), |
| 112 | $this->toDisk->path($toPath) |
| 113 | ); |
| 114 | |
| 115 | $this->cleanFrom($fromPath, $cleanFromAfter); |
| 116 | } |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param string $toPath |
| 123 | * @param \SplFileInfo $file |
| 124 | * @param string $filename = null |
| 125 | * @param bool $cleanToBefore = false |
| 126 | * |
| 127 | * @return FilesManager |
| 128 | */ |
| 129 | public function put(string $toPath, \SplFileInfo $file, string $filename = null, bool $cleanToBefore = false) : FilesManager |
| 130 | { |
| 131 | $this->checkDiskSetted(); |
| 132 | |
| 133 | $this->cleanTo($toPath, $cleanToBefore); |
| 134 | $this->createParentDir($toPath); |
| 135 | |
| 136 | if ($filename) { |
| 137 | $this->toDisk->putFileAs($toPath, $file, $filename); |
| 138 | } else { |
| 139 | $this->toDisk->putFile($toPath, $file); |
| 140 | } |
| 141 | |
| 142 | return $this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param string $toPath |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public function allDestinationFiles(string $toPath) : array |
| 151 | { |
| 152 | return $this->toDisk->allfiles($toPath); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param string $toPath |
| 157 | * @param bool $cleanTo = false |
| 158 | */ |
| 159 | protected function cleanTo(string $toPath, bool $cleanTo = false) |
| 160 | { |
| 161 | if ($cleanTo && $this->toDisk->exists($toPath)) { |
| 162 | $this->toDisk->delete($toPath); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @param string $fromPath |
| 168 | * @param bool $cleanFromAfter = false |
| 169 | */ |
| 170 | protected function cleanFrom(string $fromPath, bool $cleanTo = false) |
| 171 | { |
| 172 | if ($cleanTo) { |
| 173 | $this->fromDisk->delete($fromPath); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param string $toPath |
| 179 | */ |
| 180 | protected function createParentDir(string $toPath) |
| 181 | { |
| 182 | $parentPath = implode('/', array_slice(explode('/', $toPath), 0, -1)); |
| 183 | if ($parentPath !== $toPath && strlen($parentPath) && !$this->toDisk->exists($parentPath)) { |
| 184 | $this->toDisk->makeDirectory($parentPath, 0775, true); |
| 185 | } |
| 186 | } |
| 187 | } |