PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/league/flysystem/src/Util.php

https://gitlab.com/judielsm/Handora
PHP | 285 lines | 132 code | 45 blank | 108 comment | 17 complexity | 5d16c9efa607e33788f7caacb7c800ee MD5 | raw file
  1. <?php
  2. namespace League\Flysystem;
  3. use League\Flysystem\Util\MimeType;
  4. use LogicException;
  5. class Util
  6. {
  7. /**
  8. * Get normalized pathinfo.
  9. *
  10. * @param string $path
  11. *
  12. * @return array pathinfo
  13. */
  14. public static function pathinfo($path)
  15. {
  16. $pathinfo = pathinfo($path) + compact('path');
  17. $pathinfo['dirname'] = static::normalizeDirname($pathinfo['dirname']);
  18. return $pathinfo;
  19. }
  20. /**
  21. * Normalize a dirname return value.
  22. *
  23. * @param string $dirname
  24. *
  25. * @return string normalized dirname
  26. */
  27. public static function normalizeDirname($dirname)
  28. {
  29. if ($dirname === '.') {
  30. return '';
  31. }
  32. return $dirname;
  33. }
  34. /**
  35. * Get a normalized dirname from a path.
  36. *
  37. * @param string $path
  38. *
  39. * @return string dirname
  40. */
  41. public static function dirname($path)
  42. {
  43. return static::normalizeDirname(dirname($path));
  44. }
  45. /**
  46. * Map result arrays.
  47. *
  48. * @param array $object
  49. * @param array $map
  50. *
  51. * @return array mapped result
  52. */
  53. public static function map(array $object, array $map)
  54. {
  55. $result = [];
  56. foreach ($map as $from => $to) {
  57. if (! isset($object[$from])) {
  58. continue;
  59. }
  60. $result[$to] = $object[$from];
  61. }
  62. return $result;
  63. }
  64. /**
  65. * Normalize path.
  66. *
  67. * @param string $path
  68. *
  69. * @throws LogicException
  70. *
  71. * @return string
  72. */
  73. public static function normalizePath($path)
  74. {
  75. // Remove any kind of funky unicode whitespace
  76. $normalized = preg_replace('#\p{C}+|^\./#u', '', $path);
  77. $normalized = static::normalizeRelativePath($normalized);
  78. if (preg_match('#/\.{2}|^\.{2}/|^\.{2}$#', $normalized)) {
  79. throw new LogicException('Path is outside of the defined root, path: ['.$path.'], resolved: ['.$normalized.']');
  80. }
  81. $normalized = preg_replace('#\\\{2,}#', '\\', trim($normalized, '\\'));
  82. $normalized = preg_replace('#/{2,}#', '/', trim($normalized, '/'));
  83. return $normalized;
  84. }
  85. /**
  86. * Normalize relative directories in a path.
  87. *
  88. * @param string $path
  89. *
  90. * @return string
  91. */
  92. public static function normalizeRelativePath($path)
  93. {
  94. // Path remove self referring paths ("/./").
  95. $path = preg_replace('#/\.(?=/)|^\./|/\./?$#', '', $path);
  96. // Regex for resolving relative paths
  97. $regex = '#/*[^/\.]+/\.\.#Uu';
  98. while (preg_match($regex, $path)) {
  99. $path = preg_replace($regex, '', $path);
  100. }
  101. return $path;
  102. }
  103. /**
  104. * Normalize prefix.
  105. *
  106. * @param string $prefix
  107. * @param string $separator
  108. *
  109. * @return string normalized path
  110. */
  111. public static function normalizePrefix($prefix, $separator)
  112. {
  113. return rtrim($prefix, $separator).$separator;
  114. }
  115. /**
  116. * Get content size.
  117. *
  118. * @param string $contents
  119. *
  120. * @return int content size
  121. */
  122. public static function contentSize($contents)
  123. {
  124. return defined('MB_OVERLOAD_STRING') ? mb_strlen($contents, '8bit') : strlen($contents);
  125. }
  126. /**
  127. * Guess MIME Type based on the path of the file and it's content.
  128. *
  129. * @param string $path
  130. * @param string $content
  131. *
  132. * @return string|null MIME Type or NULL if no extension detected
  133. */
  134. public static function guessMimeType($path, $content)
  135. {
  136. $mimeType = MimeType::detectByContent($content);
  137. if (empty($mimeType) || $mimeType === 'text/plain') {
  138. $extension = pathinfo($path, PATHINFO_EXTENSION);
  139. if ($extension) {
  140. $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain';
  141. }
  142. }
  143. return $mimeType;
  144. }
  145. /**
  146. * Emulate directories.
  147. *
  148. * @param array $listing
  149. *
  150. * @return array listing with emulated directories
  151. */
  152. public static function emulateDirectories(array $listing)
  153. {
  154. $directories = [];
  155. $listedDirectories = [];
  156. foreach ($listing as $object) {
  157. list($directories, $listedDirectories) = static::emulateObjectDirectories($object, $directories, $listedDirectories);
  158. }
  159. $directories = array_diff(array_unique($directories), array_unique($listedDirectories));
  160. foreach ($directories as $directory) {
  161. $listing[] = static::pathinfo($directory) + ['type' => 'dir'];
  162. }
  163. return $listing;
  164. }
  165. /**
  166. * Ensure a Config instance.
  167. *
  168. * @param null|array|Config $config
  169. *
  170. * @return Config config instance
  171. *
  172. * @throw LogicException
  173. */
  174. public static function ensureConfig($config)
  175. {
  176. if ($config === null) {
  177. return new Config();
  178. }
  179. if ($config instanceof Config) {
  180. return $config;
  181. }
  182. if (is_array($config)) {
  183. return new Config($config);
  184. }
  185. throw new LogicException('A config should either be an array or a Flysystem\Config object.');
  186. }
  187. /**
  188. * Rewind a stream.
  189. *
  190. * @param resource $resource
  191. */
  192. public static function rewindStream($resource)
  193. {
  194. if (ftell($resource) !== 0 && static::isSeekableStream($resource)) {
  195. rewind($resource);
  196. }
  197. }
  198. public static function isSeekableStream($resource)
  199. {
  200. $metadata = stream_get_meta_data($resource);
  201. return $metadata['seekable'];
  202. }
  203. /**
  204. * Get the size of a stream.
  205. *
  206. * @param resource $resource
  207. *
  208. * @return int stream size
  209. */
  210. public static function getStreamSize($resource)
  211. {
  212. $stat = fstat($resource);
  213. return $stat['size'];
  214. }
  215. /**
  216. * Emulate the directories of a single object.
  217. *
  218. * @param array $object
  219. * @param array $directories
  220. * @param array $listedDirectories
  221. *
  222. * @return array
  223. */
  224. protected static function emulateObjectDirectories(array $object, array $directories, array $listedDirectories)
  225. {
  226. if (empty($object['dirname'])) {
  227. return [$directories, $listedDirectories];
  228. }
  229. $parent = $object['dirname'];
  230. while (!empty($parent) && !in_array($parent, $directories)) {
  231. $directories[] = $parent;
  232. $parent = static::dirname($parent);
  233. }
  234. if (isset($object['type']) && $object['type'] === 'dir') {
  235. $listedDirectories[] = $object['path'];
  236. return [$directories, $listedDirectories];
  237. }
  238. return [$directories, $listedDirectories];
  239. }
  240. }