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

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 directoryExists(string $location): bool
  32.     {
  33.         return $this->adapter->directoryExists($this->pathNormalizer->normalizePath($location));
  34.     }
  35.     public function has(string $location): bool
  36.     {
  37.         $path $this->pathNormalizer->normalizePath($location);
  38.         return $this->adapter->fileExists($path) || $this->adapter->directoryExists($path);
  39.     }
  40.     public function write(string $locationstring $contents, array $config = []): void
  41.     {
  42.         $this->adapter->write(
  43.             $this->pathNormalizer->normalizePath($location),
  44.             $contents,
  45.             $this->config->extend($config)
  46.         );
  47.     }
  48.     public function writeStream(string $location$contents, array $config = []): void
  49.     {
  50.         /* @var resource $contents */
  51.         $this->assertIsResource($contents);
  52.         $this->rewindStream($contents);
  53.         $this->adapter->writeStream(
  54.             $this->pathNormalizer->normalizePath($location),
  55.             $contents,
  56.             $this->config->extend($config)
  57.         );
  58.     }
  59.     public function read(string $location): string
  60.     {
  61.         return $this->adapter->read($this->pathNormalizer->normalizePath($location));
  62.     }
  63.     public function readStream(string $location)
  64.     {
  65.         return $this->adapter->readStream($this->pathNormalizer->normalizePath($location));
  66.     }
  67.     public function delete(string $location): void
  68.     {
  69.         $this->adapter->delete($this->pathNormalizer->normalizePath($location));
  70.     }
  71.     public function deleteDirectory(string $location): void
  72.     {
  73.         $this->adapter->deleteDirectory($this->pathNormalizer->normalizePath($location));
  74.     }
  75.     public function createDirectory(string $location, array $config = []): void
  76.     {
  77.         $this->adapter->createDirectory(
  78.             $this->pathNormalizer->normalizePath($location),
  79.             $this->config->extend($config)
  80.         );
  81.     }
  82.     public function listContents(string $locationbool $deep self::LIST_SHALLOW): DirectoryListing
  83.     {
  84.         $path $this->pathNormalizer->normalizePath($location);
  85.         return new DirectoryListing($this->adapter->listContents($path$deep));
  86.     }
  87.     public function move(string $sourcestring $destination, array $config = []): void
  88.     {
  89.         $this->adapter->move(
  90.             $this->pathNormalizer->normalizePath($source),
  91.             $this->pathNormalizer->normalizePath($destination),
  92.             $this->config->extend($config)
  93.         );
  94.     }
  95.     public function copy(string $sourcestring $destination, array $config = []): void
  96.     {
  97.         $this->adapter->copy(
  98.             $this->pathNormalizer->normalizePath($source),
  99.             $this->pathNormalizer->normalizePath($destination),
  100.             $this->config->extend($config)
  101.         );
  102.     }
  103.     public function lastModified(string $path): int
  104.     {
  105.         return $this->adapter->lastModified($this->pathNormalizer->normalizePath($path))->lastModified();
  106.     }
  107.     public function fileSize(string $path): int
  108.     {
  109.         return $this->adapter->fileSize($this->pathNormalizer->normalizePath($path))->fileSize();
  110.     }
  111.     public function mimeType(string $path): string
  112.     {
  113.         return $this->adapter->mimeType($this->pathNormalizer->normalizePath($path))->mimeType();
  114.     }
  115.     public function setVisibility(string $pathstring $visibility): void
  116.     {
  117.         $this->adapter->setVisibility($this->pathNormalizer->normalizePath($path), $visibility);
  118.     }
  119.     public function visibility(string $path): string
  120.     {
  121.         return $this->adapter->visibility($this->pathNormalizer->normalizePath($path))->visibility();
  122.     }
  123.     /**
  124.      * @param mixed $contents
  125.      */
  126.     private function assertIsResource($contents): void
  127.     {
  128.         if (is_resource($contents) === false) {
  129.             throw new InvalidStreamProvided(
  130.                 "Invalid stream provided, expected stream resource, received " gettype($contents)
  131.             );
  132.         } elseif ($type get_resource_type($contents) !== 'stream') {
  133.             throw new InvalidStreamProvided(
  134.                 "Invalid stream provided, expected stream resource, received resource of type " $type
  135.             );
  136.         }
  137.     }
  138.     /**
  139.      * @param resource $resource
  140.      */
  141.     private function rewindStream($resource): void
  142.     {
  143.         if (ftell($resource) !== && stream_get_meta_data($resource)['seekable']) {
  144.             rewind($resource);
  145.         }
  146.     }
  147. }