Filesystem.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 $location, string $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 $location, bool $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 $source, string $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 $source, string $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 $path, string $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) !== 0 && stream_get_meta_data($resource)['seekable']) {
  135. rewind($resource);
  136. }
  137. }
  138. }