PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/league/flysystem/src/Adapter/Local.php

https://gitlab.com/Ofcadavidm/RentCarApp
PHP | 420 lines | 245 code | 75 blank | 100 comment | 20 complexity | 0d937f527458872ed0f5b3a622492597 MD5 | raw file
  1. <?php
  2. namespace League\Flysystem\Adapter;
  3. use DirectoryIterator;
  4. use FilesystemIterator;
  5. use Finfo;
  6. use League\Flysystem\AdapterInterface;
  7. use League\Flysystem\Config;
  8. use League\Flysystem\NotSupportedException;
  9. use League\Flysystem\Util;
  10. use RecursiveDirectoryIterator;
  11. use RecursiveIteratorIterator;
  12. use SplFileInfo;
  13. class Local extends AbstractAdapter
  14. {
  15. protected static $permissions = [
  16. 'file' => [
  17. 'public' => 0744,
  18. 'private' => 0700,
  19. ],
  20. 'dir' => [
  21. 'public' => 0755,
  22. 'private' => 0700,
  23. ]
  24. ];
  25. /**
  26. * @var string
  27. */
  28. protected $pathSeparator = DIRECTORY_SEPARATOR;
  29. /**
  30. * @var int
  31. */
  32. protected $writeFlags;
  33. /**
  34. * Constructor.
  35. *
  36. * @param string $root
  37. * @param int $writeFlags
  38. */
  39. public function __construct($root, $writeFlags = LOCK_EX)
  40. {
  41. $realRoot = $this->ensureDirectory($root);
  42. if (! is_dir($realRoot) || ! is_readable($realRoot)) {
  43. throw new \LogicException('The root path '.$root.' is not readable.');
  44. }
  45. $this->setPathPrefix($realRoot);
  46. $this->writeFlags = $writeFlags;
  47. }
  48. /**
  49. * Ensure the root directory exists.
  50. *
  51. * @param string $root root directory path
  52. *
  53. * @return string real path to root
  54. */
  55. protected function ensureDirectory($root)
  56. {
  57. if (! is_dir($root)) {
  58. $umask = umask(0);
  59. mkdir($root, static::$permissions['dir']['public'], true);
  60. umask($umask);
  61. }
  62. return realpath($root);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function has($path)
  68. {
  69. $location = $this->applyPathPrefix($path);
  70. return file_exists($location);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function write($path, $contents, Config $config)
  76. {
  77. $location = $this->applyPathPrefix($path);
  78. $this->ensureDirectory(dirname($location));
  79. if (($size = file_put_contents($location, $contents, $this->writeFlags)) === false) {
  80. return false;
  81. }
  82. $type = 'file';
  83. $result = compact('contents', 'type', 'size', 'path');
  84. if ($visibility = $config->get('visibility')) {
  85. $result['visibility'] = $visibility;
  86. $this->setVisibility($path, $visibility);
  87. }
  88. return $result;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function writeStream($path, $resource, Config $config)
  94. {
  95. $location = $this->applyPathPrefix($path);
  96. $this->ensureDirectory(dirname($location));
  97. $stream = fopen($location, 'w+');
  98. if (! $stream) {
  99. return false;
  100. }
  101. stream_copy_to_stream($resource, $stream);
  102. if (! fclose($stream)) {
  103. return false;
  104. }
  105. if ($visibility = $config->get('visibility')) {
  106. $this->setVisibility($path, $visibility);
  107. }
  108. return compact('path', 'visibility');
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function readStream($path)
  114. {
  115. $location = $this->applyPathPrefix($path);
  116. $stream = fopen($location, 'r');
  117. return compact('stream', 'path');
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function updateStream($path, $resource, Config $config)
  123. {
  124. return $this->writeStream($path, $resource, $config);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function update($path, $contents, Config $config)
  130. {
  131. $location = $this->applyPathPrefix($path);
  132. $mimetype = Util::guessMimeType($path, $contents);
  133. $size = file_put_contents($location, $contents, $this->writeFlags);
  134. if ($size === false) {
  135. return false;
  136. }
  137. return compact('path', 'size', 'contents', 'mimetype');
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function read($path)
  143. {
  144. $location = $this->applyPathPrefix($path);
  145. $contents = file_get_contents($location);
  146. if ($contents === false) {
  147. return false;
  148. }
  149. return compact('contents', 'path');
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function rename($path, $newpath)
  155. {
  156. $location = $this->applyPathPrefix($path);
  157. $destination = $this->applyPathPrefix($newpath);
  158. $parentDirectory = $this->applyPathPrefix(Util::dirname($newpath));
  159. $this->ensureDirectory($parentDirectory);
  160. return rename($location, $destination);
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function copy($path, $newpath)
  166. {
  167. $location = $this->applyPathPrefix($path);
  168. $destination = $this->applyPathPrefix($newpath);
  169. $this->ensureDirectory(dirname($destination));
  170. return copy($location, $destination);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function delete($path)
  176. {
  177. $location = $this->applyPathPrefix($path);
  178. return unlink($location);
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function listContents($directory = '', $recursive = false)
  184. {
  185. $result = [];
  186. $location = $this->applyPathPrefix($directory).$this->pathSeparator;
  187. if (! is_dir($location)) {
  188. return [];
  189. }
  190. $iterator = $recursive ? $this->getRecursiveDirectoryIterator($location) : $this->getDirectoryIterator($location);
  191. foreach ($iterator as $file) {
  192. $path = $this->getFilePath($file);
  193. if (preg_match('#(^|/|\\\\)\.{1,2}$#', $path)) {
  194. continue;
  195. }
  196. $result[] = $this->normalizeFileInfo($file);
  197. }
  198. return $result;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function getMetadata($path)
  204. {
  205. $location = $this->applyPathPrefix($path);
  206. $info = new SplFileInfo($location);
  207. return $this->normalizeFileInfo($info);
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getSize($path)
  213. {
  214. return $this->getMetadata($path);
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getMimetype($path)
  220. {
  221. $location = $this->applyPathPrefix($path);
  222. $finfo = new Finfo(FILEINFO_MIME_TYPE);
  223. return ['mimetype' => $finfo->file($location)];
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function getTimestamp($path)
  229. {
  230. return $this->getMetadata($path);
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function getVisibility($path)
  236. {
  237. $location = $this->applyPathPrefix($path);
  238. clearstatcache(false, $location);
  239. $permissions = octdec(substr(sprintf('%o', fileperms($location)), -4));
  240. $visibility = $permissions & 0044 ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE;
  241. return compact('visibility');
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function setVisibility($path, $visibility)
  247. {
  248. $location = $this->applyPathPrefix($path);
  249. $type = is_dir($location) ? 'dir' : 'file';
  250. chmod($location, static::$permissions[$type][$visibility]);
  251. return compact('visibility');
  252. }
  253. /**
  254. * {@inheritdoc}
  255. */
  256. public function createDir($dirname, Config $config)
  257. {
  258. $location = $this->applyPathPrefix($dirname);
  259. $umask = umask(0);
  260. $visibility = $config->get('visibility', 'public');
  261. if (! is_dir($location) && ! mkdir($location, static::$permissions['dir'][$visibility], true)) {
  262. $return = false;
  263. } else {
  264. $return = ['path' => $dirname, 'type' => 'dir'];
  265. }
  266. umask($umask);
  267. return $return;
  268. }
  269. /**
  270. * {@inheritdoc}
  271. */
  272. public function deleteDir($dirname)
  273. {
  274. $location = $this->applyPathPrefix($dirname);
  275. if (! is_dir($location)) {
  276. return false;
  277. }
  278. $contents = $this->listContents($dirname, true);
  279. $contents = array_reverse($contents);
  280. foreach ($contents as $file) {
  281. if ($file['type'] !== 'dir') {
  282. unlink($this->applyPathPrefix($file['path']));
  283. } else {
  284. rmdir($this->applyPathPrefix($file['path']));
  285. }
  286. }
  287. return rmdir($location);
  288. }
  289. /**
  290. * Normalize the file info.
  291. *
  292. * @param SplFileInfo $file
  293. *
  294. * @return array
  295. */
  296. protected function normalizeFileInfo(SplFileInfo $file)
  297. {
  298. if ($file->isLink()) {
  299. throw NotSupportedException::forLink($file);
  300. }
  301. $normalized = [
  302. 'type' => $file->getType(),
  303. 'path' => $this->getFilePath($file),
  304. ];
  305. $normalized['timestamp'] = $file->getMTime();
  306. if ($normalized['type'] === 'file') {
  307. $normalized['size'] = $file->getSize();
  308. }
  309. return $normalized;
  310. }
  311. /**
  312. * Get the normalized path from a SplFileInfo object.
  313. *
  314. * @param SplFileInfo $file
  315. *
  316. * @return string
  317. */
  318. protected function getFilePath(SplFileInfo $file)
  319. {
  320. $path = $file->getPathname();
  321. $path = $this->removePathPrefix($path);
  322. return trim($path, '\\/');
  323. }
  324. /**
  325. * @param string $path
  326. *
  327. * @return RecursiveIteratorIterator
  328. */
  329. protected function getRecursiveDirectoryIterator($path)
  330. {
  331. $directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
  332. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
  333. return $iterator;
  334. }
  335. /**
  336. * @param string $path
  337. *
  338. * @return DirectoryIterator
  339. */
  340. protected function getDirectoryIterator($path)
  341. {
  342. $iterator = new DirectoryIterator($path);
  343. return $iterator;
  344. }
  345. }