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

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