Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UFileSystem.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\base;
4
6
17
25 public static function glob_recursive($pattern, $flags = 0) {
26 $files = \glob ( $pattern, $flags );
27 foreach ( \glob ( \dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
28 $files = \array_merge ( $files, self::glob_recursive ( $dir . '/' . \basename ( $pattern ), $flags ) );
29 }
30 return $files;
31 }
32
39 public static function deleteAllFilesFromFolder($folder, $mask = '*') {
40 $files = \glob ( $folder . \DS . $mask );
41 foreach ( $files as $file ) {
42 if (\is_file ( $file ))
43 \unlink ( $file );
44 }
45 }
46
53 public static function deleteFile($filename) {
54 if (\file_exists ( $filename ))
55 return \unlink ( $filename );
56 return false;
57 }
58
67 public static function safeMkdir($dir, $mode = 0777, $recursive = true) {
68 if (! \is_dir ( $dir ))
69 return \mkdir ( $dir, $mode, $recursive );
70 return true;
71 }
72
79 public static function cleanPathname($path) {
80 if (UString::isNotNull ( $path )) {
81 if (\DS === "/")
82 $path = \str_replace ( "\\", \DS, $path );
83 else
84 $path = \str_replace ( "/", \DS, $path );
85 $path = \str_replace ( \DS . \DS, \DS, $path );
86 if (! UString::endswith ( $path, \DS )) {
87 $path = $path . \DS;
88 }
89 }
90 if (\file_exists ( $path )) {
91 return \realpath ( $path );
92 }
93 return $path;
94 }
95
102 public static function cleanFilePathname($path) {
103 if (UString::isNotNull ( $path )) {
104 if (\DS === "/")
105 $path = \str_replace ( "\\", \DS, $path );
106 else
107 $path = \str_replace ( "/", \DS, $path );
108 $path = \str_replace ( \DS . \DS, \DS, $path );
109 }
110 if (\file_exists ( $path )) {
111 return \realpath ( $path );
112 }
113 return $path;
114 }
115
122 public static function tryToRequire($file) {
123 if (\file_exists ( $file )) {
124 require_once ($file);
125 return true;
126 }
127 return false;
128 }
129
136 public static function lastModified($filename) {
137 return \filemtime ( $filename );
138 }
139
146 public static function load($filename) {
147 if (\file_exists ( $filename )) {
148 return \file_get_contents ( $filename );
149 }
150 return false;
151 }
152
159 public static function getDirFromNamespace($ns) {
160 return \ROOT . \DS . \str_replace ( "\\", \DS, $ns );
161 }
162
169 public static function delTree($dir) {
170 $files = \array_diff ( scandir ( $dir ), array ('.','..' ) );
171 foreach ( $files as $file ) {
172 (\is_dir ( "$dir/$file" )) ? self::delTree ( "$dir/$file" ) : \unlink ( "$dir/$file" );
173 }
174 return \rmdir ( $dir );
175 }
176
186 public static function getLines($filename, $reverse = false, $maxLines = null, $lineCallback = null) {
187 if (\file_exists ( $filename )) {
188 if ($reverse && isset ( $maxLines )) {
189 $result = [ ];
190 $fl = \fopen ( $filename, "r" );
191 for($x_pos = 0, $ln = 0, $lines = [ ]; \fseek ( $fl, $x_pos, SEEK_END ) !== - 1; $x_pos --) {
192 $char = \fgetc ( $fl );
193 if ($char === "\n") {
194 if (\is_callable ( $lineCallback )) {
195 $lineCallback ( $result, $lines [$ln] );
196 } else {
197 $result [] = $lines [$ln];
198 }
199 if (isset ( $maxLines ) && \count ( $result ) >= $maxLines) {
200 \fclose ( $fl );
201 return $result;
202 }
203 $ln ++;
204 continue;
205 }
206 $lines [$ln] = $char . ($lines [$ln] ?? '');
207 }
208 \fclose ( $fl );
209 return $result;
210 } else {
211 return self::getLinesByLine ( $filename, $reverse, $maxLines, $lineCallback );
212 }
213 }
214 return [ ];
215 }
216
225 public static function relativePath($from, $to, $separator = DIRECTORY_SEPARATOR) {
226 $from = self::cleanPathname ( $from );
227 $to = self::cleanPathname ( $to );
228
229 $arFrom = \explode ( $separator, \rtrim ( $from, $separator ) );
230 $arTo = \explode ( $separator, \rtrim ( $to, $separator ) );
231 while ( \count ( $arFrom ) && \count ( $arTo ) && ($arFrom [0] == $arTo [0]) ) {
232 \array_shift ( $arFrom );
233 \array_shift ( $arTo );
234 }
235 return str_pad ( "", \count ( $arTo ) * 3, '..' . $separator ) . \implode ( $separator, $arFrom );
236 }
237
238 protected static function getLinesByLine($filename, $reverse, $maxLines, $lineCallback) {
239 $result = [ ];
240 $handle = \fopen ( $filename, "r" );
241 if ($handle) {
242 while ( ($line = \fgets ( $handle )) !== false ) {
243 if (\is_callable ( $lineCallback )) {
244 $lineCallback ( $result, $line );
245 } else {
246 $result [] = $line;
247 }
248 if (isset ( $maxLines ) && \count ( $result ) >= $maxLines) {
249 \fclose ( $handle );
250 if (is_array ( $result ) && $reverse) {
251 $result = \array_reverse ( $result );
252 }
253 return $result;
254 }
255 }
256 \fclose ( $handle );
257 } else {
258 // error opening the file.
259 }
260 if ($reverse) {
261 $result = \array_reverse ( $result );
262 }
263 return $result;
264 }
265}
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
static tryToRequire($file)
Try to require a file, in safe mode.
static load($filename)
Reads entire file into a string in safe mode.
static getLines($filename, $reverse=false, $maxLines=null, $lineCallback=null)
Returns the lines of a file in an array.
static glob_recursive($pattern, $flags=0)
Find recursively pathnames matching a pattern.
static getDirFromNamespace($ns)
Returns the directory base on ROOT, corresponding to a namespace.
static cleanFilePathname($path)
Cleans a file path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR.
static delTree($dir)
Deletes recursivly a folder and its content.
static getLinesByLine($filename, $reverse, $maxLines, $lineCallback)
static deleteFile($filename)
Deletes a file, in safe mode.
static safeMkdir($dir, $mode=0777, $recursive=true)
Tests the existance and eventually creates a directory.
static lastModified($filename)
Gets file modification time.
static cleanPathname($path)
Cleans a directory path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR.
static deleteAllFilesFromFolder($folder, $mask=' *')
Deletes all files from a folder (not in subfolders)
static relativePath($from, $to, $separator=DIRECTORY_SEPARATOR)
Returns relative path between two sources.
static endswith($hay, $needle)
Definition UString.php:34