vendor/league/flysystem/src/Filesystem.php line 142

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace League\Flysystem;
  4. use Generator;
  5. use Throwable;
  6. class Filesystem implements FilesystemOperator
  7. {
  8.     /**
  9.      * @var FilesystemAdapter
  10.      */
  11.     private $adapter;
  12.     /**
  13.      * @var Config
  14.      */
  15.     private $config;
  16.     /**
  17.      * @var PathNormalizer
  18.      */
  19.     private $pathNormalizer;
  20.     public function __construct(
  21.         FilesystemAdapter $adapter,
  22.         array $config = [],
  23.         PathNormalizer $pathNormalizer null
  24.     ) {
  25.         $this->adapter $adapter;
  26.         $this->config = new Config($config);
  27.         $this->pathNormalizer $pathNormalizer ?: new WhitespacePathNormalizer();
  28.     }
  29.     public function fileExists(string $location): bool
  30.     {
  31.         return $this->adapter->fileExists($this->pathNormalizer->normalizePath($location));
  32.     }
  33.     public function directoryExists(string $location): bool
  34.     {
  35.         return $this->adapter->directoryExists($this->pathNormalizer->normalizePath($location));
  36.     }
  37.     public function has(string $location): bool
  38.     {
  39.         $path $this->pathNormalizer->normalizePath($location);
  40.         return $this->adapter->fileExists($path) || $this->adapter->directoryExists($path);
  41.     }
  42.     public function write(string $locationstring $contents, array $config = []): void
  43.     {
  44.         $this->adapter->write(
  45.             $this->pathNormalizer->normalizePath($location),
  46.             $contents,
  47.             $this->config->extend($config)
  48.         );
  49.     }
  50.     public function writeStream(string $location$contents, array $config = []): void
  51.     {
  52.         /* @var resource $contents */
  53.         $this->assertIsResource($contents);
  54.         $this->rewindStream($contents);
  55.         $this->adapter->writeStream(
  56.             $this->pathNormalizer->normalizePath($location),
  57.             $contents,
  58.             $this->config->extend($config)
  59.         );
  60.     }
  61.     public function read(string $location): string
  62.     {
  63.         return $this->adapter->read($this->pathNormalizer->normalizePath($location));
  64.     }
  65.     public function readStream(string $location)
  66.     {
  67.         return $this->adapter->readStream($this->pathNormalizer->normalizePath($location));
  68.     }
  69.     public function delete(string $location): void
  70.     {
  71.         $this->adapter->delete($this->pathNormalizer->normalizePath($location));
  72.     }
  73.     public function deleteDirectory(string $location): void
  74.     {
  75.         $this->adapter->deleteDirectory($this->pathNormalizer->normalizePath($location));
  76.     }
  77.     public function createDirectory(string $location, array $config = []): void
  78.     {
  79.         $this->adapter->createDirectory(
  80.             $this->pathNormalizer->normalizePath($location),
  81.             $this->config->extend($config)
  82.         );
  83.     }
  84.     public function listContents(string $locationbool $deep self::LIST_SHALLOW): DirectoryListing
  85.     {
  86.         $path $this->pathNormalizer->normalizePath($location);
  87.         $listing $this->adapter->listContents($path$deep);
  88.         return new DirectoryListing($this->pipeListing($location$deep$listing));
  89.     }
  90.     private function pipeListing(string $locationbool $deepiterable $listing): Generator
  91.     {
  92.         try {
  93.             foreach ($listing as $item) {
  94.                 yield $item;
  95.             }
  96.         } catch (Throwable $exception) {
  97.             throw UnableToListContents::atLocation($location$deep$exception);
  98.         }
  99.     }
  100.     public function move(string $sourcestring $destination, array $config = []): void
  101.     {
  102.         $this->adapter->move(
  103.             $this->pathNormalizer->normalizePath($source),
  104.             $this->pathNormalizer->normalizePath($destination),
  105.             $this->config->extend($config)
  106.         );
  107.     }
  108.     public function copy(string $sourcestring $destination, array $config = []): void
  109.     {
  110.         $this->adapter->copy(
  111.             $this->pathNormalizer->normalizePath($source),
  112.             $this->pathNormalizer->normalizePath($destination),
  113.             $this->config->extend($config)
  114.         );
  115.     }
  116.     public function lastModified(string $path): int
  117.     {
  118.         return $this->adapter->lastModified($this->pathNormalizer->normalizePath($path))->lastModified();
  119.     }
  120.     public function fileSize(string $path): int
  121.     {
  122.         return $this->adapter->fileSize($this->pathNormalizer->normalizePath($path))->fileSize();
  123.     }
  124.     public function mimeType(string $path): string
  125.     {
  126.         return $this->adapter->mimeType($this->pathNormalizer->normalizePath($path))->mimeType();
  127.     }
  128.     public function setVisibility(string $pathstring $visibility): void
  129.     {
  130.         $this->adapter->setVisibility($this->pathNormalizer->normalizePath($path), $visibility);
  131.     }
  132.     public function visibility(string $path): string
  133.     {
  134.         return $this->adapter->visibility($this->pathNormalizer->normalizePath($path))->visibility();
  135.     }
  136.     /**
  137.      * @param mixed $contents
  138.      */
  139.     private function assertIsResource($contents): void
  140.     {
  141.         if (is_resource($contents) === false) {
  142.             throw new InvalidStreamProvided(
  143.                 "Invalid stream provided, expected stream resource, received " gettype($contents)
  144.             );
  145.         } elseif ($type get_resource_type($contents) !== 'stream') {
  146.             throw new InvalidStreamProvided(
  147.                 "Invalid stream provided, expected stream resource, received resource of type " $type
  148.             );
  149.         }
  150.     }
  151.     /**
  152.      * @param resource $resource
  153.      */
  154.     private function rewindStream($resource): void
  155.     {
  156.         if (ftell($resource) !== && stream_get_meta_data($resource)['seekable']) {
  157.             rewind($resource);
  158.         }
  159.     }
  160. }