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

/Solar/Dir.php

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