PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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