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

/tests/vfsStream/vfsStream.php

https://bitbucket.org/areeves42/openfisma
PHP | 374 lines | 142 code | 23 blank | 209 comment | 14 complexity | 5a62a0dcc9a34d04a39759963916690a MD5 | raw file
  1. <?php
  2. /**
  3. * Some utility methods for vfsStream.
  4. *
  5. * @package bovigo_vfs
  6. */
  7. /**
  8. * @ignore
  9. */
  10. require_once dirname(__FILE__) . '/vfsStreamWrapper.php';
  11. require_once dirname(__FILE__) . '/visitor/vfsStreamVisitor.php';
  12. /**
  13. * Some utility methods for vfsStream.
  14. *
  15. * @package bovigo_vfs
  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. * @var int
  51. */
  52. protected static $umask = 0000;
  53. /**
  54. * prepends the scheme to the given URL
  55. *
  56. * @param string $path
  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
  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 optional
  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. * root
  121. * \- Core
  122. * |- badlocation.php
  123. * |- AbstractFactory
  124. * | |- test.php
  125. * | |- other.php
  126. * | \- Invalid.csv
  127. * \- AnEmptyFolder
  128. * Arrays will become directories with their key as directory name, and
  129. * strings becomes files with their key as file name and their value as file
  130. * content.
  131. *
  132. * @param string $rootDirName optional name of root directory
  133. * @param int $permissions optional file permissions of root directory
  134. * @param array<string,array|string> $structure optional directory structure to add under root directory
  135. * @return vfsStreamDirectory
  136. * @since 0.7.0
  137. * @see https://github.com/mikey179/vfsStream/issues/14
  138. * @see https://github.com/mikey179/vfsStream/issues/20
  139. */
  140. public static function setup($rootDirName = 'root', $permissions = null, array $structure = array())
  141. {
  142. vfsStreamWrapper::register();
  143. return self::create($structure, vfsStreamWrapper::setRoot(self::newDirectory($rootDirName, $permissions)));
  144. }
  145. /**
  146. * creates vfsStream directory structure from an array and adds it to given base dir
  147. *
  148. * Assumed $structure contains an array like this:
  149. * <code>
  150. * array('Core' = array('AbstractFactory' => array('test.php' => 'some text content',
  151. * 'other.php' => 'Some more text content',
  152. * 'Invalid.csv' => 'Something else',
  153. * ),
  154. * 'AnEmptyFolder' => array(),
  155. * 'badlocation.php' => 'some bad content',
  156. * )
  157. * )
  158. * </code>
  159. * the resulting directory tree will look like this:
  160. * baseDir
  161. * \- Core
  162. * |- badlocation.php
  163. * |- AbstractFactory
  164. * | |- test.php
  165. * | |- other.php
  166. * | \- Invalid.csv
  167. * \- AnEmptyFolder
  168. * Arrays will become directories with their key as directory name, and
  169. * strings becomes files with their key as file name and their value as file
  170. * content.
  171. *
  172. * If no baseDir is given it will try to add the structure to the existing
  173. * root directory without replacing existing childs except those with equal
  174. * names.
  175. *
  176. * @param array<string,array|string> $structure directory structure to add under root directory
  177. * @param vfsStreamDirectory $baseDir base directory to add structure to optional
  178. * @return vfsStreamDirectory
  179. * @throws InvalidArgumentException
  180. * @since 0.10.0
  181. * @see https://github.com/mikey179/vfsStream/issues/14
  182. * @see https://github.com/mikey179/vfsStream/issues/20
  183. */
  184. public static function create(array $structure, vfsStreamDirectory $baseDir = null)
  185. {
  186. if (null === $baseDir) {
  187. $baseDir = vfsStreamWrapper::getRoot();
  188. }
  189. if (null === $baseDir) {
  190. throw new InvalidArgumentException('No baseDir given and no root directory set.');
  191. }
  192. return self::addStructure($structure, $baseDir);
  193. }
  194. /**
  195. * helper method to create subdirectories recursively
  196. *
  197. * @param array<string,array|string> $structure subdirectory structure to add
  198. * @param vfsStreamDirectory $baseDir directory to add the structure to optional
  199. * @return vfsStreamDirectory
  200. */
  201. protected static function addStructure(array $structure, vfsStreamDirectory $baseDir)
  202. {
  203. foreach ($structure as $name => $data) {
  204. $name = (string) $name;
  205. if (is_array($data) === true) {
  206. self::addStructure($data, self::newDirectory($name)->at($baseDir));
  207. } elseif (is_string($data) === true) {
  208. self::newFile($name)->withContent($data)->at($baseDir);
  209. }
  210. }
  211. return $baseDir;
  212. }
  213. /**
  214. * copies the file system structure from given path into the base dir
  215. *
  216. * If no baseDir is given it will try to add the structure to the existing
  217. * root directory without replacing existing childs except those with equal
  218. * names.
  219. * File permissions are copied as well.
  220. * Please note that file contents will only be copied if their file size
  221. * does not exceed the given $maxFileSize which is 1024 KB.
  222. *
  223. * @param string $path path to copy the structure from
  224. * @param vfsStreamDirectory $baseDir directory to add the structure to optional
  225. * @param int $maxFileSize maximum file size of files to copy content from optional
  226. * @return vfsStreamDirectory
  227. * @throws InvalidArgumentException
  228. * @since 0.11.0
  229. * @see https://github.com/mikey179/vfsStream/issues/4
  230. */
  231. public static function copyFromFileSystem($path, vfsStreamDirectory $baseDir = null, $maxFileSize = 1048576)
  232. {
  233. if (null === $baseDir) {
  234. $baseDir = vfsStreamWrapper::getRoot();
  235. }
  236. if (null === $baseDir) {
  237. throw new InvalidArgumentException('No baseDir given and no root directory set.');
  238. }
  239. $dir = new DirectoryIterator($path);
  240. foreach ($dir as $fileinfo) {
  241. if ($fileinfo->isFile() === true) {
  242. if ($fileinfo->getSize() <= $maxFileSize) {
  243. $content = file_get_contents($fileinfo->getPathname());
  244. } else {
  245. $content = '';
  246. }
  247. self::newFile($fileinfo->getFilename(),
  248. octdec(substr(sprintf('%o', $fileinfo->getPerms()), -4))
  249. )
  250. ->withContent($content)
  251. ->at($baseDir);
  252. } elseif ($fileinfo->isDir() === true && $fileinfo->isDot() === false) {
  253. self::copyFromFileSystem($fileinfo->getPathname(),
  254. self::newDirectory($fileinfo->getFilename(),
  255. octdec(substr(sprintf('%o', $fileinfo->getPerms()), -4))
  256. )
  257. ->at($baseDir),
  258. $maxFileSize
  259. );
  260. }
  261. }
  262. return $baseDir;
  263. }
  264. /**
  265. * returns a new file with given name
  266. *
  267. * @param string $name
  268. * @param int $permissions optional
  269. * @return vfsStreamFile
  270. */
  271. public static function newFile($name, $permissions = null)
  272. {
  273. return new vfsStreamFile($name, $permissions);
  274. }
  275. /**
  276. * returns a new directory with given name
  277. *
  278. * If the name contains slashes, a new directory structure will be created.
  279. * The returned directory will always be the parent directory of this
  280. * directory structure.
  281. *
  282. * @param string $name
  283. * @param int $permissions optional
  284. * @return vfsStreamDirectory
  285. */
  286. public static function newDirectory($name, $permissions = null)
  287. {
  288. if ('/' === $name{0}) {
  289. $name = substr($name, 1);
  290. }
  291. $firstSlash = strpos($name, '/');
  292. if (false === $firstSlash) {
  293. return new vfsStreamDirectory($name, $permissions);
  294. }
  295. $ownName = substr($name, 0, $firstSlash);
  296. $subDirs = substr($name, $firstSlash + 1);
  297. $directory = new vfsStreamDirectory($ownName, $permissions);
  298. self::newDirectory($subDirs, $permissions)->at($directory);
  299. return $directory;
  300. }
  301. /**
  302. * returns current user
  303. *
  304. * If the system does not support posix_getuid() the current user will be root (0).
  305. *
  306. * @return int
  307. */
  308. public static function getCurrentUser()
  309. {
  310. return function_exists('posix_getuid') ? posix_getuid() : self::OWNER_ROOT;
  311. }
  312. /**
  313. * returns current group
  314. *
  315. * If the system does not support posix_getgid() the current group will be root (0).
  316. *
  317. * @return int
  318. */
  319. public static function getCurrentGroup()
  320. {
  321. return function_exists('posix_getgid') ? posix_getgid() : self::GROUP_ROOT;
  322. }
  323. /**
  324. * use visitor to inspect a content structure
  325. *
  326. * If the given content is null it will fall back to use the current root
  327. * directory of the stream wrapper.
  328. *
  329. * Returns given visitor for method chaining comfort.
  330. *
  331. * @param vfsStreamVisitor $visitor
  332. * @param vfsStreamContent $content optional
  333. * @return vfsStreamVisitor
  334. * @throws InvalidArgumentException
  335. * @since 0.10.0
  336. * @see https://github.com/mikey179/vfsStream/issues/10
  337. */
  338. public static function inspect(vfsStreamVisitor $visitor, vfsStreamContent $content = null)
  339. {
  340. if (null !== $content) {
  341. return $visitor->visit($content);
  342. }
  343. $root = vfsStreamWrapper::getRoot();
  344. if (null === $root) {
  345. throw new InvalidArgumentException('No content given and no root directory set.');
  346. }
  347. return $visitor->visitDirectory($root);
  348. }
  349. }
  350. ?>