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

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