Ubiquity  2.0.3
php rapid development framework
UFileSystem.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\base;
4 
10 class UFileSystem {
11 
12  public static function glob_recursive($pattern, $flags=0) {
13  $files=\glob($pattern, $flags);
14  foreach ( \glob(\dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir ) {
15  $files=\array_merge($files, self::glob_recursive($dir . '/' . \basename($pattern), $flags));
16  }
17  return $files;
18  }
19 
20  public static function deleteAllFilesFromFolder($folder) {
21  $files=\glob($folder . '/*');
22  foreach ( $files as $file ) {
23  if (\is_file($file))
24  \unlink($file);
25  }
26  }
27 
28  public static function deleteFile($filename){
29  if (\file_exists($filename))
30  return \unlink($filename);
31  return false;
32  }
33 
34  public static function safeMkdir($dir) {
35  if (!\is_dir($dir))
36  return \mkdir($dir, 0777, true);
37  return true;
38  }
39 
40  public static function cleanPathname($path) {
41  if (UString::isNotNull($path)) {
42  if (DS === "/")
43  $path=\str_replace("\\", DS, $path);
44  else
45  $path=\str_replace("/", DS, $path);
46  $path=\str_replace(DS . DS, DS, $path);
47  if (!UString::endswith($path, DS)) {
48  $path=$path . DS;
49  }
50  }
51  return $path;
52  }
53 
54  public static function cleanFilePathname($path) {
55  if (UString::isNotNull($path)) {
56  if (DS === "/")
57  $path=\str_replace("\\", DS, $path);
58  else
59  $path=\str_replace("/", DS, $path);
60  $path=\str_replace(DS . DS, DS, $path);
61  }
62  return $path;
63  }
64 
65  public static function openReplaceInTemplateFile($source, $keyAndValues) {
66  if (\file_exists($source)) {
67  $str=\file_get_contents($source);
68  return self::replaceFromTemplate($str, $keyAndValues);
69  }
70  return false;
71  }
72 
73  public static function openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues) {
74  if (($str=self::openReplaceInTemplateFile($source, $keyAndValues))) {
75  return \file_put_contents($destination, $str, LOCK_EX);
76  }
77  return false;
78  }
79 
80  public static function replaceFromTemplate($content, $keyAndValues) {
81  array_walk($keyAndValues, function (&$item) {
82  if (\is_array($item))
83  $item=\implode("\n", $item);
84  });
85  $str=\str_replace(array_keys($keyAndValues), array_values($keyAndValues), $content);
86  return $str;
87  }
88 
89  public static function replaceWriteFromContent($content, $destination, $keyAndValues) {
90  return \file_put_contents($destination, self::replaceFromTemplate($content, $keyAndValues), LOCK_EX);
91  }
92 
93  public static function tryToRequire($file) {
94  if (\file_exists($file)) {
95  require_once ($file);
96  return true;
97  }
98  return false;
99  }
100 
101  public static function lastModified($filename) {
102  return \filemtime($filename);
103  }
104 
105  public static function load($filename){
106  if (\file_exists($filename)) {
107  return \file_get_contents($filename);
108  }
109  return false;
110  }
111 
112  public static function save($filename,$content,$flags=LOCK_EX){
113  return \file_put_contents($filename, $content, $flags);
114  }
115 
116  public static function getDirFromNamespace($ns){
117  return ROOT . DS . str_replace ( "\\", DS, $ns );
118  }
119 }
$source
Project: doxygen-php-filters Author: Alex Schickedanz (AbcAeffchen) Date: 05.03.2015 License: GPL v2...
Definition: traits.php:10
static glob_recursive($pattern, $flags=0)
Definition: UFileSystem.php:12
static lastModified($filename)
static openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues)
Definition: UFileSystem.php:73
static openReplaceInTemplateFile($source, $keyAndValues)
Definition: UFileSystem.php:65
static replaceWriteFromContent($content, $destination, $keyAndValues)
Definition: UFileSystem.php:89
static endswith($hay, $needle)
Definition: UString.php:16
File system utilities.
Definition: UFileSystem.php:10
static deleteFile($filename)
Definition: UFileSystem.php:28
static replaceFromTemplate($content, $keyAndValues)
Definition: UFileSystem.php:80
static deleteAllFilesFromFolder($folder)
Definition: UFileSystem.php:20
static save($filename, $content, $flags=LOCK_EX)