/lib/vfsStream/vfsStream.php

https://github.com/pago/pantr · PHP · 141 lines · 52 code · 8 blank · 81 comment · 2 complexity · dc421d3c8d3e2c77866efb163ae20f01 MD5 · raw file

  1. <?php
  2. /**
  3. * Some utility methods for vfsStream.
  4. *
  5. * @package bovigo_vfs
  6. * @version $Id: vfsStream.php 132 2009-07-13 19:13:25Z google@frankkleine.de $
  7. */
  8. /**
  9. * @ignore
  10. */
  11. require_once dirname(__FILE__) . '/vfsStreamWrapper.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. * prepends the scheme to the given URL
  49. *
  50. * @param string $path
  51. * @return string
  52. */
  53. public static function url($path)
  54. {
  55. return self::SCHEME . '://' . str_replace('\\', '/', $path);
  56. }
  57. /**
  58. * restores the path from the url
  59. *
  60. * @param string $url
  61. * @return string
  62. */
  63. public static function path($url)
  64. {
  65. // remove line feeds and trailing whitespaces
  66. $path = trim($url, " \t\r\n\0\x0B/");
  67. $path = substr($path, strlen(self::SCHEME . '://'));
  68. $path = str_replace('\\', '/', $path);
  69. // replace double slashes with single slashes
  70. $path = str_replace('//', '/', $path);
  71. return $path;
  72. }
  73. /**
  74. * returns a new file with given name
  75. *
  76. * @param string $name
  77. * @param int $permissions
  78. * @return vfsStreamFile
  79. */
  80. public static function newFile($name, $permissions = 0666)
  81. {
  82. return new vfsStreamFile($name, $permissions);
  83. }
  84. /**
  85. * returns a new directory with given name
  86. *
  87. * If the name contains slashes, a new directory structure will be created.
  88. * The returned directory will always be the parent directory of this
  89. * directory structure.
  90. *
  91. * @param string $name
  92. * @param int $permissions
  93. * @return vfsStreamDirectory
  94. */
  95. public static function newDirectory($name, $permissions = 0777)
  96. {
  97. if ('/' === $name{0}) {
  98. $name = substr($name, 1);
  99. }
  100. $firstSlash = strpos($name, '/');
  101. if (false === $firstSlash) {
  102. return new vfsStreamDirectory($name, $permissions);
  103. }
  104. $ownName = substr($name, 0, $firstSlash);
  105. $subDirs = substr($name, $firstSlash + 1);
  106. $directory = new vfsStreamDirectory($ownName, $permissions);
  107. self::newDirectory($subDirs, $permissions)->at($directory);
  108. return $directory;
  109. }
  110. /**
  111. * returns current user
  112. *
  113. * If the system does not support posix_getuid() the current user will be root (0).
  114. *
  115. * @return int
  116. */
  117. public static function getCurrentUser()
  118. {
  119. return function_exists('posix_getuid') ? posix_getuid() : self::OWNER_ROOT;
  120. }
  121. /**
  122. * returns current group
  123. *
  124. * If the system does not support posix_getgid() the current group will be root (0).
  125. *
  126. * @return int
  127. */
  128. public static function getCurrentGroup()
  129. {
  130. return function_exists('posix_getgid') ? posix_getgid() : self::GROUP_ROOT;
  131. }
  132. }
  133. ?>