PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Genv/Dir.php

https://gitlab.com/winiceo/levengit
PHP | 264 lines | 122 code | 20 blank | 122 comment | 21 complexity | 56a41da2f1ff1c44abaa49ad69e7e128 MD5 | raw file
  1. <?php
  2. class Genv_Dir{
  3. protected static $_tmp;
  4. /**
  5. *
  6. * Hack for [[php::is_dir() | ]] that checks the include_path.
  7. *
  8. * Use this to see if a directory exists anywhere in the include_path.
  9. *
  10. * {{code: php
  11. * $dir = Genv_Dir::exists('path/to/dir')
  12. * if ($dir) {
  13. * $files = scandir($dir);
  14. * } else {
  15. * echo "Not found in the include-path.";
  16. * }
  17. * }}
  18. *
  19. * @param string $dir Check for this directory in the include_path.
  20. *
  21. * @return mixed If the directory exists in the include_path, returns the
  22. * absolute path; if not, returns boolean false.
  23. *
  24. */
  25. public static function exists($dir)
  26. {
  27. // no file requested?
  28. $dir = trim($dir);
  29. if (! $dir) {
  30. return false;
  31. }
  32. // using an absolute path for the file?
  33. // dual check for Unix '/' and Windows '\',
  34. // or Windows drive letter and a ':'.
  35. $abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':');
  36. if ($abs && is_dir($dir)) {
  37. return $dir;
  38. }
  39. // using a relative path on the file
  40. $path = explode(PATH_SEPARATOR, ini_get('include_path'));
  41. foreach ($path as $base) {
  42. // strip Unix '/' and Windows '\'
  43. $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir;
  44. if (is_dir($target)) {
  45. return $target;
  46. }
  47. }
  48. // never found it
  49. return false;
  50. }
  51. /**
  52. *
  53. * "Fixes" a directory string for the operating system.
  54. *
  55. * Use slashes anywhere you need a directory separator. Then run the
  56. * string through fixdir() and the slashes will be converted to the
  57. * proper separator (for example '\' on Windows).
  58. *
  59. * Always adds a final trailing separator.
  60. *
  61. * @param string $dir The directory string to 'fix'.
  62. *
  63. * @return string The "fixed" directory string.
  64. *
  65. */
  66. public static function fix($dir)
  67. {
  68. $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
  69. return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  70. }
  71. // update by roy 2010-06-24
  72. public static function fixSeparator($dir) {
  73. $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
  74. return rtrim($dir);
  75. }
  76. /**
  77. *
  78. * Convenience method for dirname() and higher-level directories.
  79. *
  80. * @param string $file Get the dirname() of this file.
  81. *
  82. * @param int $up Move up in the directory structure this many
  83. * times, default 0.
  84. *
  85. * @return string The dirname() of the file.
  86. *
  87. */
  88. public static function name($file, $up = 0)
  89. {
  90. $dir = dirname($file);
  91. while ($up --) {
  92. $dir = dirname($dir);
  93. }
  94. return $dir;
  95. }
  96. /**
  97. *
  98. * Returns the OS-specific directory for temporary files; uses the Genv
  99. * `$system/tmp` directory when available.
  100. *
  101. * @param string $sub Add this subdirectory to the returned temporary
  102. * directory name.
  103. *
  104. * @return string The temporary directory path.
  105. *
  106. */
  107. public static function tmp($sub = '')
  108. {
  109. // find the tmp dir if needed
  110. if (! Genv_Dir::$_tmp) {
  111. // use the system if we can
  112. if (Genv::$system) {
  113. $tmp = Genv::$system . "/tmp";
  114. } elseif (function_exists('sys_get_temp_dir')) {
  115. $tmp = sys_get_temp_dir();
  116. } else {
  117. $tmp = Genv_Dir::_tmp();
  118. }
  119. // remove trailing separator and save
  120. Genv_Dir::$_tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
  121. }
  122. // do we have a subdirectory request?
  123. $sub = trim($sub);
  124. if ($sub) {
  125. // remove leading and trailing separators, and force exactly
  126. // one trailing separator
  127. $sub = trim($sub, DIRECTORY_SEPARATOR)
  128. . DIRECTORY_SEPARATOR;
  129. }
  130. return Genv_Dir::$_tmp . DIRECTORY_SEPARATOR . $sub;
  131. }
  132. /**
  133. *
  134. * Returns the OS-specific temporary directory location.
  135. *
  136. * @return string The temp directory path.
  137. *
  138. */
  139. protected static function _tmp()
  140. {
  141. // non-Windows system?
  142. if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
  143. $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
  144. if ($tmp) {
  145. return $tmp;
  146. } else {
  147. return '/tmp';
  148. }
  149. }
  150. // Windows 'TEMP'
  151. $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
  152. if ($tmp) {
  153. return $tmp;
  154. }
  155. // Windows 'TMP'
  156. $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
  157. if ($tmp) {
  158. return $tmp;
  159. }
  160. // Windows 'windir'
  161. $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
  162. if ($tmp) {
  163. return $tmp;
  164. }
  165. // final fallback for Windows
  166. return getenv('SystemRoot') . '\\temp';
  167. }
  168. /**
  169. *
  170. * Replacement for mkdir() to supress warnings and throw exceptions in
  171. * their place.
  172. *
  173. * @param string $path The directory path to create.
  174. *
  175. * @param int $mode The permissions mode for the directory.
  176. *
  177. * @param bool $recursive Recursively create directories along the way.
  178. *
  179. * @return bool True on success; throws exception on failure.
  180. *
  181. * @see [[php::mkdir() | ]]
  182. *
  183. */
  184. public static function mkdir($path, $mode = 0777, $recursive = false)
  185. {
  186. $result = @mkdir($path, $mode, $recursive);
  187. if (! $result) {
  188. $info = error_get_last();
  189. $info['dir'] = $path;
  190. $info['mode'] = $mode;
  191. $info['recursive'] = $recursive;
  192. throw Genv_Dir::_exception('ERR_MKDIR_FAILED', $info);
  193. } else {
  194. return true;
  195. }
  196. }
  197. /**
  198. *
  199. * Replacement for rmdir() to supress warnings and throw exceptions in
  200. * their place.
  201. *
  202. * @param string $path The directory path to remove
  203. *
  204. * @return bool True on success; throws exception on failure.
  205. *
  206. * @see [[php::rmdir() | ]]
  207. *
  208. */
  209. public static function rmdir($path)
  210. {
  211. $result = @rmdir($path);
  212. if (! $result) {
  213. $info = error_get_last();
  214. $info['dir'] = $path;
  215. throw Genv_Dir::_exception('ERR_RMDIR_FAILED', $info);
  216. } else {
  217. return true;
  218. }
  219. }
  220. /**
  221. *
  222. * Returns a localized exception object.
  223. *
  224. * @param string $code The error code.
  225. *
  226. * @param array $info Additional error information.
  227. *
  228. * @return Genv_Exception
  229. *
  230. */
  231. protected static function _exception($code, $info = null)
  232. {
  233. $class = 'Genv_Dir';
  234. $locale = Genv_Registry::get('locale');
  235. return Genv::exception(
  236. $class,
  237. $code,
  238. $locale->fetch($class, $code, 1, $info),
  239. (array) $info
  240. );
  241. }
  242. }
  243. ?>