MODX Revolution PHP Cross Reference Content Management Systems

Source: /manager/min/lib/Solar/Dir.php - 199 lines - 5373 bytes - Summary - Text - Print

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

title

Description

title

Description

title

Description

title

title

Body