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 ) );
40 $files = \glob ( $folder . \DS . $mask );
41 foreach ( $files as $file ) {
42 if (\is_file ( $file ))
54 if (\file_exists ( $filename ))
55 return \unlink ( $filename );
67 public static function safeMkdir($dir, $mode = 0777, $recursive =
true) {
68 if (! \is_dir ( $dir ))
69 return \mkdir ( $dir, $mode, $recursive );
82 $path = \str_replace (
"\\", \DS, $path );
84 $path = \str_replace (
"/", \DS, $path );
85 $path = \str_replace ( \DS . \DS, \DS, $path );
90 if (\file_exists ( $path )) {
91 return \realpath ( $path );
105 $path = \str_replace (
"\\", \DS, $path );
107 $path = \str_replace (
"/", \DS, $path );
108 $path = \str_replace ( \DS . \DS, \DS, $path );
110 if (\file_exists ( $path )) {
111 return \realpath ( $path );
123 if (\file_exists ( $file )) {
124 require_once ($file);
137 return \filemtime ( $filename );
146 public static function load($filename) {
147 if (\file_exists ( $filename )) {
148 return \file_get_contents ( $filename );
160 return \ROOT . \DS . \str_replace (
"\\", \DS, $ns );
170 $files = \array_diff ( scandir ( $dir ), array (
'.',
'..' ) );
171 foreach ( $files as $file ) {
172 (\is_dir (
"$dir/$file" )) ? self::delTree (
"$dir/$file" ) : \unlink (
"$dir/$file" );
174 return \rmdir ( $dir );
186 public static function getLines($filename, $reverse =
false, $maxLines =
null, $lineCallback =
null) {
187 if (\file_exists ( $filename )) {
188 if ($reverse && isset ( $maxLines )) {
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] );
197 $result [] = $lines [$ln];
199 if (isset ( $maxLines ) && \count ( $result ) >= $maxLines) {
206 $lines [$ln] = $char . ($lines [$ln] ??
'');
225 public static function relativePath($from, $to, $separator = DIRECTORY_SEPARATOR) {
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 );
235 return str_pad (
"", \count ( $arTo ) * 3,
'..' . $separator ) . \implode ( $separator, $arFrom );
238 protected static function getLinesByLine($filename, $reverse, $maxLines, $lineCallback) {
240 $handle = \fopen ( $filename,
"r" );
242 while ( ($line = \fgets ( $handle )) !==
false ) {
243 if (\is_callable ( $lineCallback )) {
244 $lineCallback ( $result, $line );
248 if (isset ( $maxLines ) && \count ( $result ) >= $maxLines) {
250 if (is_array ( $result ) && $reverse) {
251 $result = \array_reverse ( $result );
261 $result = \array_reverse ( $result );
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)