Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| InMemoryFilesystemAdapter | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| visibility | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| lastModified | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| copy | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| write | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| createDirectories | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BrianHenryIE\Strauss\Helpers; |
| 4 | |
| 5 | use League\Flysystem\Config; |
| 6 | use League\Flysystem\FileAttributes; |
| 7 | use League\Flysystem\InMemory\InMemoryFilesystemAdapter as LeagueInMemoryFilesystemAdapter; |
| 8 | |
| 9 | class InMemoryFilesystemAdapter extends LeagueInMemoryFilesystemAdapter |
| 10 | { |
| 11 | |
| 12 | public function visibility(string $path): FileAttributes |
| 13 | { |
| 14 | if (!$this->fileExists($path)) { |
| 15 | // Assume it is a directory. |
| 16 | |
| 17 | // Maybe check does the directory exist. |
| 18 | // $parentDirContents = (array) $this->listContents(dirname($path), false); |
| 19 | // throw UnableToRetrieveMetadata::visibility($path, 'file does not exist'); |
| 20 | |
| 21 | return new FileAttributes($path, null, 'public'); |
| 22 | } |
| 23 | |
| 24 | |
| 25 | return parent::visibility($path); |
| 26 | } |
| 27 | |
| 28 | public function lastModified(string $path): FileAttributes |
| 29 | { |
| 30 | if (!$this->fileExists($path)) { |
| 31 | // Assume it is a directory |
| 32 | return new FileAttributes($path, null, null, 0); |
| 33 | } |
| 34 | |
| 35 | return parent::lastModified($path); |
| 36 | } |
| 37 | |
| 38 | public function copy(string $source, string $destination, Config $config): void |
| 39 | { |
| 40 | $this->createDirectories($destination, $config); |
| 41 | |
| 42 | parent::copy($source, $destination, $config); |
| 43 | } |
| 44 | |
| 45 | public function write(string $path, string $contents, Config $config): void |
| 46 | { |
| 47 | // Make sure there is a directory for the file to be written to. |
| 48 | if (false === strpos($path, '______DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST')) { |
| 49 | $this->createDirectories($path, $config); |
| 50 | } |
| 51 | |
| 52 | parent::write($path, $contents, $config); |
| 53 | } |
| 54 | |
| 55 | protected function createDirectories(string $path, Config $config): void |
| 56 | { |
| 57 | $pathDirs = explode('/', dirname($path)); |
| 58 | for ($level = 0; $level < count($pathDirs); $level++) { |
| 59 | $dir = implode('/', array_slice($pathDirs, 0, $level + 1)); |
| 60 | $this->createDirectory($dir, $config); |
| 61 | } |
| 62 | } |
| 63 | } |