PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/vfsStream.php

https://gitlab.com/supriyanto-edodoe22/manpro
PHP | 389 lines | 146 code | 24 blank | 219 comment | 14 complexity | 9d0ddd738ae2ddeb7d827dc57dcf3c21 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of vfsStream.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @package org\bovigo\vfs
  9. */
  10. namespace org\bovigo\vfs;
  11. use org\bovigo\vfs\visitor\vfsStreamVisitor;
  12. /**
  13. * Some utility methods for vfsStream.
  14. *
  15. * @api
  16. */
  17. class vfsStream
  18. {
  19. /**
  20. * url scheme
  21. */
  22. const SCHEME = 'vfs';
  23. /**
  24. * owner: root
  25. */
  26. const OWNER_ROOT = 0;
  27. /**
  28. * owner: user 1
  29. */
  30. const OWNER_USER_1 = 1;
  31. /**
  32. * owner: user 2
  33. */
  34. const OWNER_USER_2 = 2;
  35. /**
  36. * group: root
  37. */
  38. const GROUP_ROOT = 0;
  39. /**
  40. * group: user 1
  41. */
  42. const GROUP_USER_1 = 1;
  43. /**
  44. * group: user 2
  45. */
  46. const GROUP_USER_2 = 2;
  47. /**
  48. * initial umask setting
  49. *
  50. * @type int
  51. */
  52. protected static $umask = 0000;
  53. /**
  54. * prepends the scheme to the given URL
  55. *
  56. * @param string $path path to translate to vfsStream url
  57. * @return string
  58. */
  59. public static function url($path)
  60. {
  61. return self::SCHEME . '://' . str_replace('\\', '/', $path);
  62. }
  63. /**
  64. * restores the path from the url
  65. *
  66. * @param string $url vfsStream url to translate into path
  67. * @return string
  68. */
  69. public static function path($url)
  70. {
  71. // remove line feeds and trailing whitespaces
  72. $path = trim($url, " \t\r\n\0\x0B/");
  73. $path = substr($path, strlen(self::SCHEME . '://'));
  74. $path = str_replace('\\', '/', $path);
  75. // replace double slashes with single slashes
  76. $path = str_replace('//', '/', $path);
  77. return $path;
  78. }
  79. /**
  80. * sets new umask setting and returns previous umask setting
  81. *
  82. * If no value is given only the current umask setting is returned.
  83. *
  84. * @param int $umask new umask setting
  85. * @return int
  86. * @since 0.8.0
  87. */
  88. public static function umask($umask = null)
  89. {
  90. $oldUmask = self::$umask;
  91. if (null !== $umask) {
  92. self::$umask = $umask;
  93. }
  94. return $oldUmask;
  95. }
  96. /**
  97. * helper method for setting up vfsStream in unit tests
  98. *
  99. * Instead of
  100. * vfsStreamWrapper::register();
  101. * vfsStreamWrapper::setRoot(vfsStream::newDirectory('root'));
  102. * you can simply do
  103. * vfsStream::setup()
  104. * which yields the same result. Additionally, the method returns the
  105. * freshly created root directory which you can use to make further
  106. * adjustments to it.
  107. *
  108. * Assumed $structure contains an array like this:
  109. * <code>
  110. * array('Core' = array('AbstractFactory' => array('test.php' => 'some text content',
  111. * 'other.php' => 'Some more text content',
  112. * 'Invalid.csv' => 'Something else',
  113. * ),
  114. * 'AnEmptyFolder' => array(),
  115. * 'badlocation.php' => 'some bad content',
  116. * )
  117. * )
  118. * </code>
  119. * the resulting directory tree will look like this:
  120. * <pre>
  121. * root
  122. * \- Core
  123. * |- badlocation.php
  124. * |- AbstractFactory
  125. * | |- test.php
  126. * | |- other.php
  127. * | \- Invalid.csv
  128. * \- AnEmptyFolder
  129. * </pre>
  130. * Arrays will become directories with their key as directory name, and
  131. * strings becomes files with their key as file name and their value as file
  132. * content.
  133. *
  134. * @param string $rootDirName name of root directory
  135. * @param int $permissions file permissions of root directory
  136. * @param array $structure directory structure to add under root directory
  137. * @return \org\bovigo\vfs\vfsStreamDirectory
  138. * @since 0.7.0
  139. * @see https://github.com/mikey179/vfsStream/issues/14
  140. * @see https://github.com/mikey179/vfsStream/issues/20
  141. */
  142. public static function setup($rootDirName = 'root', $permissions = null, array $structure = array())
  143. {
  144. vfsStreamWrapper::register();
  145. return self::create($structure, vfsStreamWrapper::setRoot(self::newDirectory($rootDirName, $permissions)));
  146. }
  147. /**
  148. * creates vfsStream directory structure from an array and adds it to given base dir
  149. *
  150. * Assumed $structure contains an array like this:
  151. * <code>
  152. * array('Core' = array('AbstractFactory' => array('test.php' => 'some text content',
  153. * 'other.php' => 'Some more text content',
  154. * 'Invalid.csv' => 'Something else',
  155. * ),
  156. * 'AnEmptyFolder' => array(),
  157. * 'badlocation.php' => 'some bad content',
  158. * )
  159. * )
  160. * </code>
  161. * the resulting directory tree will look like this:
  162. * <pre>
  163. * baseDir
  164. * \- Core
  165. * |- badlocation.php
  166. * |- AbstractFactory
  167. * | |- test.php
  168. * | |- other.php
  169. * | \- Invalid.csv
  170. * \- AnEmptyFolder
  171. * </pre>
  172. * Arrays will become directories with their key as directory name, and
  173. * strings becomes files with their key as file name and their value as file
  174. * content.
  175. *
  176. * If no baseDir is given it will try to add the structure to the existing
  177. * root directory without replacing existing childs except those with equal
  178. * names.
  179. *
  180. * @param array $structure directory structure to add under root directory
  181. * @param vfsStreamDirectory $baseDir base directory to add structure to
  182. * @return vfsStreamDirectory
  183. * @throws \InvalidArgumentException
  184. * @since 0.10.0
  185. * @see https://github.com/mikey179/vfsStream/issues/14
  186. * @see https://github.com/mikey179/vfsStream/issues/20
  187. */
  188. public static function create(array $structure, vfsStreamDirectory $baseDir = null)
  189. {
  190. if (null === $baseDir) {
  191. $baseDir = vfsStreamWrapper::getRoot();
  192. }
  193. if (null === $baseDir) {
  194. throw new \InvalidArgumentException('No baseDir given and no root directory set.');
  195. }
  196. return self::addStructure($structure, $baseDir);
  197. }
  198. /**
  199. * helper method to create subdirectories recursively
  200. *
  201. * @param array $structure subdirectory structure to add
  202. * @param vfsStreamDirectory $baseDir directory to add the structure to
  203. * @return vfsStreamDirectory
  204. */
  205. protected static function addStructure(array $structure, vfsStreamDirectory $baseDir)
  206. {
  207. foreach ($structure as $name => $data) {
  208. $name = (string) $name;
  209. if (is_array($data) === true) {
  210. self::addStructure($data, self::newDirectory($name)->at($baseDir));
  211. } elseif (is_string($data) === true) {
  212. self::newFile($name)->withContent($data)->at($baseDir);
  213. }
  214. }
  215. return $baseDir;
  216. }
  217. /**
  218. * copies the file system structure from given path into the base dir
  219. *
  220. * If no baseDir is given it will try to add the structure to the existing
  221. * root directory without replacing existing childs except those with equal
  222. * names.
  223. * File permissions are copied as well.
  224. * Please note that file contents will only be copied if their file size
  225. * does not exceed the given $maxFileSize which is 1024 KB.
  226. *
  227. * @param string $path path to copy the structure from
  228. * @param vfsStreamDirectory $baseDir directory to add the structure to
  229. * @param int $maxFileSize maximum file size of files to copy content from
  230. * @return vfsStreamDirectory
  231. * @throws \InvalidArgumentException
  232. * @since 0.11.0
  233. * @see https://github.com/mikey179/vfsStream/issues/4
  234. */
  235. public static function copyFromFileSystem($path, vfsStreamDirectory $baseDir = null, $maxFileSize = 1048576)
  236. {
  237. if (null === $baseDir) {
  238. $baseDir = vfsStreamWrapper::getRoot();
  239. }
  240. if (null === $baseDir) {
  241. throw new \InvalidArgumentException('No baseDir given and no root directory set.');
  242. }
  243. $dir = new \DirectoryIterator($path);
  244. foreach ($dir as $fileinfo) {
  245. if ($fileinfo->isFile() === true) {
  246. if ($fileinfo->getSize() <= $maxFileSize) {
  247. $content = file_get_contents($fileinfo->getPathname());
  248. } else {
  249. $content = '';
  250. }
  251. self::newFile($fileinfo->getFilename(),
  252. octdec(substr(sprintf('%o', $fileinfo->getPerms()), -4))
  253. )
  254. ->withContent($content)
  255. ->at($baseDir);
  256. } elseif ($fileinfo->isDir() === true && $fileinfo->isDot() === false) {
  257. self::copyFromFileSystem($fileinfo->getPathname(),
  258. self::newDirectory($fileinfo->getFilename(),
  259. octdec(substr(sprintf('%o', $fileinfo->getPerms()), -4))
  260. )
  261. ->at($baseDir),
  262. $maxFileSize
  263. );
  264. }
  265. }
  266. return $baseDir;
  267. }
  268. /**
  269. * returns a new file with given name
  270. *
  271. * @param string $name name of file to create
  272. * @param int $permissions permissions of file to create
  273. * @return vfsStreamFile
  274. */
  275. public static function newFile($name, $permissions = null)
  276. {
  277. return new vfsStreamFile($name, $permissions);
  278. }
  279. /**
  280. * returns a new directory with given name
  281. *
  282. * If the name contains slashes, a new directory structure will be created.
  283. * The returned directory will always be the parent directory of this
  284. * directory structure.
  285. *
  286. * @param string $name name of directory to create
  287. * @param int $permissions permissions of directory to create
  288. * @return vfsStreamDirectory
  289. */
  290. public static function newDirectory($name, $permissions = null)
  291. {
  292. if ('/' === $name{0}) {
  293. $name = substr($name, 1);
  294. }
  295. $firstSlash = strpos($name, '/');
  296. if (false === $firstSlash) {
  297. return new vfsStreamDirectory($name, $permissions);
  298. }
  299. $ownName = substr($name, 0, $firstSlash);
  300. $subDirs = substr($name, $firstSlash + 1);
  301. $directory = new vfsStreamDirectory($ownName, $permissions);
  302. self::newDirectory($subDirs, $permissions)->at($directory);
  303. return $directory;
  304. }
  305. /**
  306. * returns current user
  307. *
  308. * If the system does not support posix_getuid() the current user will be root (0).
  309. *
  310. * @return int
  311. */
  312. public static function getCurrentUser()
  313. {
  314. return function_exists('posix_getuid') ? posix_getuid() : self::OWNER_ROOT;
  315. }
  316. /**
  317. * returns current group
  318. *
  319. * If the system does not support posix_getgid() the current group will be root (0).
  320. *
  321. * @return int
  322. */
  323. public static function getCurrentGroup()
  324. {
  325. return function_exists('posix_getgid') ? posix_getgid() : self::GROUP_ROOT;
  326. }
  327. /**
  328. * use visitor to inspect a content structure
  329. *
  330. * If the given content is null it will fall back to use the current root
  331. * directory of the stream wrapper.
  332. *
  333. * Returns given visitor for method chaining comfort.
  334. *
  335. * @param vfsStreamVisitor $visitor the visitor who inspects
  336. * @param vfsStreamContent $content directory structure to inspect
  337. * @return vfsStreamVisitor
  338. * @throws \InvalidArgumentException
  339. * @since 0.10.0
  340. * @see https://github.com/mikey179/vfsStream/issues/10
  341. */
  342. public static function inspect(vfsStreamVisitor $visitor, vfsStreamContent $content = null)
  343. {
  344. if (null !== $content) {
  345. return $visitor->visit($content);
  346. }
  347. $root = vfsStreamWrapper::getRoot();
  348. if (null === $root) {
  349. throw new \InvalidArgumentException('No content given and no root directory set.');
  350. }
  351. return $visitor->visitDirectory($root);
  352. }
  353. /**
  354. * sets quota to given amount of bytes
  355. *
  356. * @param int $bytes
  357. * @since 1.1.0
  358. */
  359. public static function setQuota($bytes)
  360. {
  361. vfsStreamWrapper::setQuota(new Quota($bytes));
  362. }
  363. }
  364. ?>