PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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