PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Minify/Solar/Dir.php

https://code.google.com/p/ecartcommerce/
PHP | 199 lines | 80 code | 16 blank | 103 comment | 17 complexity | 6c88f363f6830ac4dc3917eac3c9d78c MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  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: Dir.php 2926 2007-11-09 16:25:44Z pmjones $
  15. *
  16. */
  17. class Solar_Dir {
  18. /**
  19. *
  20. * The OS-specific temporary directory location.
  21. *
  22. * @var string
  23. *
  24. */
  25. protected static $_tmp;
  26. /**
  27. *
  28. * Hack for [[php::is_dir() | ]] that checks the include_path.
  29. *
  30. * Use this to see if a directory exists anywhere in the include_path.
  31. *
  32. * {{code: php
  33. * $dir = Solar_Dir::exists('path/to/dir')
  34. * if ($dir) {
  35. * $files = scandir($dir);
  36. * } else {
  37. * echo "Not found in the include-path.";
  38. * }
  39. * }}
  40. *
  41. * @param string $dir Check for this directory in the include_path.
  42. *
  43. * @return mixed If the directory exists in the include_path, returns the
  44. * absolute path; if not, returns boolean false.
  45. *
  46. */
  47. public static function exists($dir)
  48. {
  49. // no file requested?
  50. $dir = trim($dir);
  51. if (! $dir) {
  52. return false;
  53. }
  54. // using an absolute path for the file?
  55. // dual check for Unix '/' and Windows '\',
  56. // or Windows drive letter and a ':'.
  57. $abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':');
  58. if ($abs && is_dir($dir)) {
  59. return $dir;
  60. }
  61. // using a relative path on the file
  62. $path = explode(PATH_SEPARATOR, ini_get('include_path'));
  63. foreach ($path as $base) {
  64. // strip Unix '/' and Windows '\'
  65. $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir;
  66. if (is_dir($target)) {
  67. return $target;
  68. }
  69. }
  70. // never found it
  71. return false;
  72. }
  73. /**
  74. *
  75. * "Fixes" a directory string for the operating system.
  76. *
  77. * Use slashes anywhere you need a directory separator. Then run the
  78. * string through fixdir() and the slashes will be converted to the
  79. * proper separator (for example '\' on Windows).
  80. *
  81. * Always adds a final trailing separator.
  82. *
  83. * @param string $dir The directory string to 'fix'.
  84. *
  85. * @return string The "fixed" directory string.
  86. *
  87. */
  88. public static function fix($dir)
  89. {
  90. $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
  91. return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  92. }
  93. /**
  94. *
  95. * Convenience method for dirname() and higher-level directories.
  96. *
  97. * @param string $file Get the dirname() of this file.
  98. *
  99. * @param int $up Move up in the directory structure this many
  100. * times, default 0.
  101. *
  102. * @return string The dirname() of the file.
  103. *
  104. */
  105. public static function name($file, $up = 0)
  106. {
  107. $dir = dirname($file);
  108. while ($up --) {
  109. $dir = dirname($dir);
  110. }
  111. return $dir;
  112. }
  113. /**
  114. *
  115. * Returns the OS-specific directory for temporary files.
  116. *
  117. * @param string $sub Add this subdirectory to the returned temporary
  118. * directory name.
  119. *
  120. * @return string The temporary directory path.
  121. *
  122. */
  123. public static function tmp($sub = '')
  124. {
  125. // find the tmp dir if needed
  126. if (! Solar_Dir::$_tmp) {
  127. // use the system if we can
  128. if (function_exists('sys_get_temp_dir')) {
  129. $tmp = sys_get_temp_dir();
  130. } else {
  131. $tmp = Solar_Dir::_tmp();
  132. }
  133. // remove trailing separator and save
  134. Solar_Dir::$_tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
  135. }
  136. // do we have a subdirectory request?
  137. $sub = trim($sub);
  138. if ($sub) {
  139. // remove leading and trailing separators, and force exactly
  140. // one trailing separator
  141. $sub = trim($sub, DIRECTORY_SEPARATOR)
  142. . DIRECTORY_SEPARATOR;
  143. }
  144. return Solar_Dir::$_tmp . DIRECTORY_SEPARATOR . $sub;
  145. }
  146. /**
  147. *
  148. * Returns the OS-specific temporary directory location.
  149. *
  150. * @return string The temp directory path.
  151. *
  152. */
  153. protected static function _tmp()
  154. {
  155. // non-Windows system?
  156. if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
  157. $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
  158. if ($tmp) {
  159. return $tmp;
  160. } else {
  161. return '/tmp';
  162. }
  163. }
  164. // Windows 'TEMP'
  165. $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
  166. if ($tmp) {
  167. return $tmp;
  168. }
  169. // Windows 'TMP'
  170. $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
  171. if ($tmp) {
  172. return $tmp;
  173. }
  174. // Windows 'windir'
  175. $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
  176. if ($tmp) {
  177. return $tmp;
  178. }
  179. // final fallback for Windows
  180. return getenv('SystemRoot') . '\\temp';
  181. }
  182. }