nyroFwk  0.2
file.class.php
Go to the documentation of this file.
1 <?php
12 final class file {
13 
19  private static $cfg;
20 
27  private static $searchFiles = array();
28 
34  private static $saveCacheFiles = false;
35 
41  private static $cacheFiles = null;
42 
46  private function __construct() {}
47 
51  public static function init() {
52  self::initCache();
53  }
54 
58  private static function initCfg() {
59  if (!self::$cfg)
60  self::$cfg = new config(factory::loadCfg(__CLASS__));
61  }
62 
66  private static function initCache() {
67  self::$saveCacheFiles = false;
68  self::$cacheFiles = cache::getInstance();
69  $tmp = self::$searchFiles;
70  self::$searchFiles = array();
71  self::$cacheFiles->get(self::$searchFiles, array(
72  'ttl'=>0,
73  'id'=>'nyroExists',
74  'request'=>array('uri'=>false,'meth'=>array()),
75  'serialize'=>true,
76  ));
77  self::$searchFiles = array_merge($tmp, self::$searchFiles);
78  self::$saveCacheFiles = true;
79  }
80 
84  public static function saveCache() {
85  if (!self::$saveCacheFiles) {
86  self::$cacheFiles->save();
87  self::$saveCacheFiles = true;
88  }
89  }
90 
97  public static function exists($file) {
98  /*
99  if (empty(self::$searchFiles)) {
100  $search = explode(',', SEARCHROOT);
101  foreach($search as $k=>$d) {
102  foreach( new RegexFindFile($d, '`(.*)`') as $f) {
103  self::$searchFiles[] = $f->getPathname();
104  }
105  }
106  }
107  return in_array($file, self::$searchFiles);
108  */
109  return file_exists($file);
110  }
111 
118  public static function name($file) {
119  return basename($file);
120  }
121 
137  public static function nyroExists($prm) {
138  if (!config::initTab($prm, array(
139  'name'=>null,
140  'realName'=>false,
141  'type'=>'class',
142  'rtl'=>true,
143  'list'=>false,
144  'tplExt'=>''
145  )))
146  throw new nException('File - nyroExists : name to search is empty.');
147 
148  $cacheKey = implode(':', array_filter($prm, 'is_string'));
149  if (!isset(self::$searchFiles[$cacheKey])) {
150  $dir = explode(',', SEARCHROOT);
151 
152  $nameTmp = $prm['realName'] ? $prm['name'] : str_replace('_', DS, $prm['name']);
153 
154  $name = array();
155  if ($prm['type'] == 'cfg') {
156  $ext = 'cfg';
157  $out = request::get('out');
158  $name[] = $nameTmp.'.'.$ext.'.'.EXTPHP;
159  $name[] = $nameTmp.'.'.$out.'.'.$ext.'.'.EXTPHP;
160  $name[] = $nameTmp.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
161  $name[] = $nameTmp.'.'.request::get('lang').'.'.$out.'.'.$ext.'.'.EXTPHP;
162  $name[] = $nameTmp.'.'.NYROENV.'.'.$ext.'.'.EXTPHP;
163  $name[] = $nameTmp.'.'.NYROENV.'.'.$out.'.'.$ext.'.'.EXTPHP;
164  $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
165  $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$out.'.'.$ext.'.'.EXTPHP;
166  } else if ($prm['type'] == 'tpl') {
167  $ext = $prm['tplExt'] ? $prm['tplExt'] : request::get('out');
168  $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
169  $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.$ext;
170  $name[] = $nameTmp.'.'.NYROENV.'.'.request::get('lang').'.'.EXTPHP;
171  $name[] = $nameTmp.'.'.request::get('lang').'.'.$ext.'.'.EXTPHP;
172  $name[] = $nameTmp.'.'.request::get('lang').'.'.$ext;
173  $name[] = $nameTmp.'.'.request::get('lang').'.'.EXTPHP;
174  $name[] = $nameTmp.'.'.NYROENV.'.'.$ext.'.'.EXTPHP;
175  $name[] = $nameTmp.'.'.NYROENV.'.'.$ext;
176  $name[] = $nameTmp.'.'.NYROENV.'.'.EXTPHP;
177  $name[] = $nameTmp.'.'.$ext.'.'.EXTPHP;
178  $name[] = $nameTmp.'.'.$ext;
179  $name[] = $nameTmp.'.'.EXTPHP;
180  } else if ($prm['type'] == 'other')
181  $name[] = $nameTmp;
182  else
183  $name[] = $nameTmp.'.'.$prm['type'].'.'.EXTPHP;
184 
185  if (!$prm['rtl'])
186  $dir = array_reverse($dir);
187 
188  if ($prm['list'])
189  $ret = array();
190 
191  /*
192  array_walk($dir, create_function('&$v', '$v = substr($v, strlen(ROOT));'));
193  $regex = str_replace('\\', '\\\\', '`('.implode('|', $dir).')('.implode('|', $name).')`');
194  foreach(new RegexFindFile(ROOT, $regex) as $file) {
195  if ($prm['list'])
196  $ret[] = $file->getPathname();
197  else {
198  stEnd();
199  return $file->getPathname();
200  }
201  }
202  // */
203  //*
204  foreach($dir as &$d) {
205  foreach($name as &$n) {
206  if (self::exists($file = $d.$n)) {
207  if ($prm['list'])
208  $ret[] = $file;
209  else if (!isset(self::$searchFiles[$cacheKey]))
210  self::$searchFiles[$cacheKey] = $file;
211  }
212  }
213  reset($name);
214  }
215  // */
216 
217  if ($prm['list'])
218  self::$searchFiles[$cacheKey] = $ret;
219  else if (!isset(self::$searchFiles[$cacheKey]))
220  self::$searchFiles[$cacheKey] = false;
221  self::$saveCacheFiles = false;
222  }
223  return self::$searchFiles[$cacheKey];
224  }
225 
232  public static function webExists($file) {
233  return self::exists(WEBROOT.DS.$file);
234  }
235 
242  public static function read($file) {
243  if (self::exists($file))
244  return file_get_contents($file);
245  else
246  return false;
247  }
248 
256  public static function write($file, $content) {
257  self::createDir(pathinfo($file, PATHINFO_DIRNAME));
258  return (file_put_contents($file, $content) === self::size($file));
259  }
260 
268  public static function copy($oldName, $newName) {
269  return copy($oldName, $newName);
270  }
271 
279  public static function move($oldName, $newName) {
280  return rename($oldName, $newName);
281  }
282 
291  public static function createDir($path, $chmod=0777) {
292  umask(0002);
293  return is_dir($path) || mkdir($path, $chmod, true);
294  }
295 
302  public static function date($file) {
303  clearstatcache();
304  return filemtime($file);
305  }
306 
314  public static function isLater($file1, $file2) {
315  clearstatcache();
316  return (filemtime($file1) > filemtime($file2));
317  }
318 
325  public static function size($file) {
326  clearstatcache();
327  if (self::exists($file))
328  return filesize($file);
329  return 0;
330  }
331 
339  public static function humanSize($file, $sizeGiven = false) {
340  $size = $sizeGiven ? $file : self::size($file);
341  $mod = 1024;
342  $units = explode(' ', 'B KB MB GB TB PB');
343  for ($i = 0; $size > $mod; $i++) {
344  $size /= $mod;
345  }
346  return round($size, 2).' '.$units[$i];
347  }
348 
355  public static function delete($file) {
356  if (self::exists($file)) {
357  if (is_dir($file)) {
358  self::multipleDelete($file.'/*');
359  @rmdir($file);
360  } else {
361  @unlink($file);
362  clearstatcache();
363  if (self::exists($file)) {
364  $filesys = str_replace("/", "\\", $file);
365  @system("del $filesys");
366  clearstatcache();
367  if (self::exists($file)) {
368  @chmod($file, 0775);
369  @unlink($file);
370  @system("del $filesys");
371  }
372  }
373  }
374  clearstatcache();
375  }
376  return !self::exists($file);
377  }
378 
386  public static function multipleDelete($pattern, $matchPattern = null) {
387  $nb = 0;
388  foreach(self::search($pattern, $matchPattern) as $f)
389  if (self::delete($f))
390  $nb++;
391  return $nb;
392  }
393 
400  public static function getExt($file) {
401  $ext = pathinfo($file, PATHINFO_EXTENSION);
402  if (!$ext)
403  $ext = substr($file, strrpos($file, '.') + 1);
404  return $ext;
405  }
406 
413  public static function getType($file) {
414  self::initCfg();
415  $ret = self::$cfg->getInArray('mimes', strtolower(self::getExt($file)));
416  if (!$ret)
417  $ret = self::$cfg->getInArray('mimes', 'unknown');
418  return $ret;
419 
420  $ret = false;
421 
422  if (self::exists($file)) {
423  $finfo = new finfo(FILEINFO_MIME);
424  if ($finfo) {
425  $ret = $finfo->file($filename);
426  $finfo->close();
427  } else
428  $ret = mime_content_type($file);
429  }
430 
431  return $ret;
432  }
433 
441  public static function fetch($file, array $vars = array()) {
442  extract($vars, EXTR_REFS OR EXTR_OVERWRITE);
443  ob_start();
444  include($file);
445  $contents = ob_get_contents();
446  ob_end_clean();
447  return $contents;
448  }
449 
457  public static function search($pattern, $matchPattern = null) {
458  $ret = glob($pattern);
459  if (!is_null($matchPattern)) {
460  $tmp = array();
461  foreach($ret as $r) {
462  if (preg_match($matchPattern, $r))
463  $tmp[] = $r;
464  }
465  $ret = $tmp;
466  }
467  return $ret;
468  }
469 
470 }
static get($get=null)
static saveCache()
Definition: file.class.php:84
static read($file)
Definition: file.class.php:242
static getInstance(array $cfg=array())
Definition: cache.class.php:30
static $cacheFiles
Definition: file.class.php:41
__construct()
Definition: file.class.php:46
if(!defined('NYROROOT')) define('NYROROOT' ROOT DS
Definition: start.inc.php:56
static size($file)
Definition: file.class.php:325
static exists($file)
Definition: file.class.php:97
static move($oldName, $newName)
Definition: file.class.php:279
static initCfg()
Definition: file.class.php:58
static webExists($file)
Definition: file.class.php:232
static humanSize($file, $sizeGiven=false)
Definition: file.class.php:339
static $searchFiles
Definition: file.class.php:27
static initTab(array &$vars, array $init)
static copy($oldName, $newName)
Definition: file.class.php:268
static init()
Definition: file.class.php:51
static name($file)
Definition: file.class.php:118
static getExt($file)
Definition: file.class.php:400
static getType($file)
Definition: file.class.php:413
static $saveCacheFiles
Definition: file.class.php:34
static date($file)
Definition: file.class.php:302
static nyroExists($prm)
Definition: file.class.php:137
static loadCfg($className, $searchParent=true)
static fetch($file, array $vars=array())
Definition: file.class.php:441
static createDir($path, $chmod=0777)
Definition: file.class.php:291
static $cfg
Definition: file.class.php:19
static write($file, $content)
Definition: file.class.php:256
static initCache()
Definition: file.class.php:66
static multipleDelete($pattern, $matchPattern=null)
Definition: file.class.php:386
static isLater($file1, $file2)
Definition: file.class.php:314
$f
Definition: list.php:6
static search($pattern, $matchPattern=null)
Definition: file.class.php:457
const NYROENV
Definition: admin.php:8
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13