Go to the documentation of this file.00001 <?php
00012 final class file {
00013
00019 private static $cfg;
00020
00027 private static $searchFiles = array();
00028
00034 private static $saveCacheFiles = false;
00035
00041 private static $cacheFiles = null;
00042
00046 private function __construct() {}
00047
00051 public static function init() {
00052 self::initCache();
00053 }
00054
00058 private static function initCfg() {
00059 if (!self::$cfg)
00060 self::$cfg = new config(factory::loadCfg(__CLASS__));
00061 }
00062
00066 private static function initCache() {
00067 self::$saveCacheFiles = false;
00068 self::$cacheFiles = cache::getInstance();
00069 $tmp = self::$searchFiles;
00070 self::$searchFiles = array();
00071 self::$cacheFiles->get(self::$searchFiles, array(
00072 'ttl'=>0,
00073 'id'=>'nyroExists',
00074 'request'=>array('uri'=>false,'meth'=>array()),
00075 'serialize'=>true,
00076 ));
00077 self::$searchFiles = array_merge($tmp, self::$searchFiles);
00078 self::$saveCacheFiles = true;
00079 }
00080
00084 public static function saveCache() {
00085 if (!self::$saveCacheFiles) {
00086 self::$cacheFiles->save();
00087 self::$saveCacheFiles = true;
00088 }
00089 }
00090
00097 public static function exists($file) {
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 return file_exists($file);
00110 }
00111
00118 public static function name($file) {
00119 return basename($file);
00120 }
00121
00137 public static function nyroExists($prm) {
00138 if (!config::initTab($prm, array(
00139 'name'=>null,
00140 'realName'=>false,
00141 'type'=>'class',
00142 'rtl'=>true,
00143 'list'=>false,
00144 'tplExt'=>''
00145 )))
00146 throw new nException('File - nyroExists : name to search is empty.');
00147
00148 $cacheKey = implode(':', array_filter($prm, 'is_string'));
00149 if (!isset(self::$searchFiles[$cacheKey])) {
00150 $dir = explode(',', SEARCHROOT);
00151
00152 $nameTmp = $prm['realName'] ? $prm['name'] : str_replace('_', DS, $prm['name']);
00153
00154 $name = array();
00155 if ($prm['type'] == 'cfg') {
00156 $ext = 'cfg';
00157 $out = request::get('out');
00158 $name[] = $nameTmp.'.'.$ext.'.'.EXTPHP;
00159 $name[] = $nameTmp.'.'.$out.'.'.$ext.'.'.EXTPHP;
00160 $name[] = $nameTmp.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
00161 $name[] = $nameTmp.'.'.request::get('lang').'.'.$out.'.'.$ext.'.'.EXTPHP;
00162 $name[] = $nameTmp.'.'.NYROENV.'.'.$ext.'.'.EXTPHP;
00163 $name[] = $nameTmp.'.'.NYROENV.'.'.$out.'.'.$ext.'.'.EXTPHP;
00164 $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
00165 $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$out.'.'.$ext.'.'.EXTPHP;
00166 } else if ($prm['type'] == 'tpl') {
00167 $ext = $prm['tplExt'] ? $prm['tplExt'] : request::get('out');
00168 $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
00169 $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$ext;
00170 $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.EXTPHP;
00171 $name[] = $nameTmp.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
00172 $name[] = $nameTmp.'.'.request::get('lang').'.'.$ext;
00173 $name[] = $nameTmp.'.'.request::get('lang').'.'.EXTPHP;
00174 $name[] = $nameTmp.'.'.NYROENV.'.'.$ext.'.'.EXTPHP;
00175 $name[] = $nameTmp.'.'.NYROENV.'.'.$ext;
00176 $name[] = $nameTmp.'.'.NYROENV.'.'.EXTPHP;
00177 $name[] = $nameTmp.'.'.$ext.'.'.EXTPHP;
00178 $name[] = $nameTmp.'.'.$ext;
00179 $name[] = $nameTmp.'.'.EXTPHP;
00180 } else if ($prm['type'] == 'other')
00181 $name[] = $nameTmp;
00182 else
00183 $name[] = $nameTmp.'.'.$prm['type'].'.'.EXTPHP;
00184
00185 if (!$prm['rtl'])
00186 $dir = array_reverse($dir);
00187
00188 if ($prm['list'])
00189 $ret = array();
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 foreach($dir as &$d) {
00205 foreach($name as &$n) {
00206 if (self::exists($file = $d.$n)) {
00207 if ($prm['list'])
00208 $ret[] = $file;
00209 else if (!isset(self::$searchFiles[$cacheKey]))
00210 self::$searchFiles[$cacheKey] = $file;
00211 }
00212 }
00213 reset($name);
00214 }
00215
00216
00217 if ($prm['list'])
00218 self::$searchFiles[$cacheKey] = $ret;
00219 else if (!isset(self::$searchFiles[$cacheKey]))
00220 self::$searchFiles[$cacheKey] = false;
00221 self::$saveCacheFiles = false;
00222 }
00223 return self::$searchFiles[$cacheKey];
00224 }
00225
00232 public static function webExists($file) {
00233 return self::exists(WEBROOT.DS.$file);
00234 }
00235
00242 public static function read($file) {
00243 if (self::exists($file))
00244 return file_get_contents($file);
00245 else
00246 return false;
00247 }
00248
00256 public static function write($file, $content) {
00257 self::createDir(pathinfo($file, PATHINFO_DIRNAME));
00258 return (file_put_contents($file, $content) === self::size($file));
00259 }
00260
00268 public static function copy($oldName, $newName) {
00269 return copy($oldName, $newName);
00270 }
00271
00279 public static function move($oldName, $newName) {
00280 return rename($oldName, $newName);
00281 }
00282
00291 public static function createDir($path, $chmod=0777) {
00292 umask(0002);
00293 return is_dir($path) || mkdir($path, $chmod, true);
00294 }
00295
00302 public static function date($file) {
00303 clearstatcache();
00304 return filemtime($file);
00305 }
00306
00314 public static function isLater($file1, $file2) {
00315 clearstatcache();
00316 return (filemtime($file1) > filemtime($file2));
00317 }
00318
00325 public static function size($file) {
00326 clearstatcache();
00327 if (self::exists($file))
00328 return filesize($file);
00329 return 0;
00330 }
00331
00338 public static function humanSize($file) {
00339 $size = self::size($file);
00340 $mod = 1024;
00341 $units = explode(' ', 'B KB MB GB TB PB');
00342 for ($i = 0; $size > $mod; $i++) {
00343 $size /= $mod;
00344 }
00345 return round($size, 2).' '.$units[$i];
00346 }
00347
00354 public static function delete($file) {
00355 if (self::exists($file)) {
00356 if (is_dir($file)) {
00357 self::multipleDelete($file.'/*');
00358 @rmdir($file);
00359 } else {
00360 @unlink($file);
00361 clearstatcache();
00362 if (self::exists($file)) {
00363 $filesys = str_replace("/", "\\", $file);
00364 @system("del $filesys");
00365 clearstatcache();
00366 if (self::exists($file)) {
00367 @chmod($file, 0775);
00368 @unlink($file);
00369 @system("del $filesys");
00370 }
00371 }
00372 }
00373 clearstatcache();
00374 }
00375 return !self::exists($file);
00376 }
00377
00384 public static function multipleDelete($pattern) {
00385 $nb = 0;
00386 foreach(self::search($pattern) as $f)
00387 if (self::delete($f))
00388 $nb++;
00389 return $nb;
00390 }
00391
00398 public static function getExt($file) {
00399 return pathinfo($file, PATHINFO_EXTENSION);
00400 }
00401
00408 public static function getType($file) {
00409 self::initCfg();
00410 $ret = self::$cfg->getInArray('mimes', strtolower(self::getExt($file)));
00411 if (!$ret)
00412 $ret = self::$cfg->getInArray('mimes', 'unknown');
00413 return $ret;
00414
00415 $ret = false;
00416
00417 if (self::exists($file)) {
00418 $finfo = new finfo(FILEINFO_MIME);
00419 if ($finfo) {
00420 $ret = $finfo->file($filename);
00421 $finfo->close();
00422 } else
00423 $ret = mime_content_type($file);
00424 }
00425
00426 return $ret;
00427 }
00428
00436 public static function fetch($file, array $vars = array()) {
00437 extract($vars, EXTR_REFS OR EXTR_OVERWRITE);
00438 ob_start();
00439 include($file);
00440 $contents = ob_get_contents();
00441 ob_end_clean();
00442 return $contents;
00443 }
00444
00451 public static function search($pattern) {
00452 return glob($pattern);
00453 }
00454
00455 }