[ Index ]

PHP Cross Reference of phool

title

Body

[close]

/Phool/ -> File.php (source)

   1  <?php
   2  //============================================================================
   3  // This program is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU Lesser General Public License (LGPL) as
   5  // published by the Free Software Foundation, either version 3 of the License,
   6  // or (at your option) any later version.
   7  //
   8  // This program is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU Lesser General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU Lesser General Public License
  14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15  //============================================================================
  16  /**
  17  * @copyright Francois Laupretre <phool@tekwire.net>
  18  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0
  19  * @category phool
  20  * @package phool
  21  */
  22  //============================================================================
  23  
  24  namespace Phool;
  25  
  26  class File
  27  {
  28  
  29  //----
  30  
  31  public static function suffix($filename)
  32  {
  33  $dotpos=strrpos($filename,'.');
  34  if ($dotpos===false) return '';
  35  
  36  return strtolower(substr($filename,$dotpos+1));
  37  }
  38  
  39  //----
  40  
  41  public static function fileSuffix($filename)
  42  {
  43  return self::suffix($filename);
  44  }
  45  
  46  //----
  47  /**
  48  * Combines a base path with another path
  49  *
  50  * The base path can be relative or absolute.
  51  *
  52  * The 2nd path can also be relative or absolute. If absolute, it is returned
  53  * as-is. If it is a relative path, it is combined to the base path.
  54  *
  55  * Uses '/' as separator (to be compatible with stream-wrapper URIs).
  56  *
  57  * @param string $base The base path
  58  * @param string|null $path The path to combine
  59  * @param bool $separ true: add trailing sep, false: remove it
  60  * @return string The resulting path
  61  */
  62  
  63  public static function combinePath($base,$path,$separ=false)
  64  {
  65  if (($base=='.') || ($base=='') || self::isAbsolutePath($path))
  66      $res=$path;
  67  elseif (($path=='.') || is_null($path))
  68      $res=$base;
  69  else    //-- Relative path : combine it to base
  70      $res=rtrim($base,'/\\').'/'.$path;
  71  
  72  return self::trailingSepar($res,$separ);
  73  }
  74  
  75  //---------------------------------
  76  /**
  77  * Adds or removes a trailing separator in a path
  78  *
  79  * @param string $path Input
  80  * @param bool $flag true: add trailing sep, false: remove it
  81  * @return bool The result path
  82  */
  83  
  84  public static function trailingSepar($path, $separ)
  85  {
  86  $path=rtrim($path,'/\\');
  87  if ($path=='') return '/';
  88  if ($separ) $path=$path.'/';
  89  return $path;
  90  }
  91  
  92  //---------------------------------
  93  /**
  94  * Determines if a given path is absolute or relative
  95  *
  96  * @param string $path The path to check
  97  * @return bool True if the path is absolute, false if relative
  98  */
  99  
 100  public static function isAbsolutePath($path)
 101  {
 102  return ((strpos($path,':')!==false)
 103      ||(strpos($path,'/')===0)
 104      ||(strpos($path,'\\')===0));
 105  }
 106  
 107  //---------------------------------
 108  /**
 109  * Build an absolute path from a given (absolute or relative) path
 110  *
 111  * If the input path is relative, it is combined with the current working
 112  * directory.
 113  *
 114  * @param string $path The path to make absolute
 115  * @param bool $separ True if the resulting path must contain a trailing separator
 116  * @return string The resulting absolute path
 117  */
 118  
 119  public static function mkAbsolutePath($path,$separ=false)
 120  {
 121  if (!self::isAbsolutePath($path)) $path=self::combinePath(getcwd(),$path);
 122  return self::trailingSepar($path,$separ);
 123  }
 124  
 125  //---------
 126  
 127  public static function readFile($path)
 128  {
 129  if (($data=@file_get_contents($path))===false)
 130      throw new \Exception($path.': Cannot get file content');
 131  return $data;
 132  }
 133  
 134  //---------
 135  // Throws exceptions and removes '.' and '..'
 136  
 137  public static function scandir($path)
 138  {
 139  if (($subnames=scandir($path))===false)
 140      throw new \Exception($path.': Cannot read directory');
 141  
 142  $a=array();
 143  foreach($subnames as $f)
 144      if (($f!='.') && ($f!='..')) $a[]=$f;
 145  
 146  return $a;
 147  }
 148  
 149  //---------------------------------
 150  
 151  public static function atomicWrite($path,$data)
 152  {
 153  $tmpf=tempnam(dirname($path),'tmp_');
 154  
 155  if (file_put_contents($tmpf,$data)!=strlen($data))
 156      throw new \Exception($tmpf.": Cannot write");
 157  
 158  // Windows does not support renaming to an existing file (looses atomicity)
 159  
 160  if (Util::envIsWindows()) @unlink($path);
 161  
 162  if (!rename($tmpf,$path))
 163      {
 164      unlink($tmpf);
 165      throw new \Exception($path,'Cannot replace file');
 166      }
 167  }
 168  
 169  //---------------------------------
 170  /**
 171  * Computes a string uniquely identifying a given path on this host.
 172  *
 173  * Mount point unicity is based on a combination of device+inode+mtime.
 174  *
 175  * On systems which don't supply a valid inode number (eg Windows), we
 176  * maintain a fake inode table, whose unicity is based on the path filtered
 177  * through realpath(). It is not perfect because I am not sure that realpath
 178  * really returns a unique 'canonical' path, but this is best solution I
 179  * have found so far.
 180  *
 181  * @param string $path The path to be mounted
 182  * @return string the computed mount point
 183  * @throws Exception
 184  */
 185  
 186  private static $simul_inode_array=array();
 187  private static $simul_inode_index=1;
 188  
 189  public static function pathUniqueID($prefix,$path,&$mtime)
 190  {
 191  if (($s=stat($path))===false) throw new \Exception("$path: File not found");
 192  
 193  $dev=$s[0];
 194  $inode=$s[1];
 195  $mtime=$s[9];
 196  
 197  if ($inode==0) // This system does not support inodes
 198      {
 199      $rpath=realpath($path);
 200      if ($rpath === false) throw new \Exception("$path: Cannot compute realpath");
 201  
 202      if (isset(self::$simul_inode_array[$rpath]))
 203          $inode=self::$simul_inode_array[$rpath];
 204      else
 205          { // Create a new slot
 206          $inode=self::$simul_inode_index++;    
 207          self::$simul_inode_array[$rpath]=$inode;
 208          }
 209      }
 210  
 211  return sprintf('%s_%X_%X_%X',$prefix,$dev,$inode,$mtime);
 212  }
 213  
 214  //---------------------------------
 215  
 216  public static function recursiveCopy($src,$dst)
 217  {
 218  if (is_dir($src))
 219      {
 220      $dir = opendir($src);
 221      @mkdir($dst);
 222      while(($entry=readdir($dir))!==false)
 223          {
 224          if (($entry==='.')||($entry==='..')) continue;
 225          self::recursiveCopy($src.'/'.$entry,$dst.'/'.$entry);
 226          }
 227      closedir($dir);
 228      }
 229  else
 230      {
 231      copy($src,$dst);
 232      }
 233  }
 234  
 235  //----------
 236  } // End of class
 237  //=============================================================================
 238  ?>


Generated: Thu Jun 4 19:17:11 2015 Cross-referenced by PHPXref 0.7.1