PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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