Passed
Push — master ( 617c92...5e6e10 )
by Jean-Christophe
05:35
created

UFileSystem::safeMkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
use Ubiquity\utils\base\traits\UFileSystemWriter;
6
7
/**
8
 * File system utilities
9
 *
10
 * @author jcheron <[email protected]>"
11
 * @version 1.0.2
12
 */
13
class UFileSystem {
14
	use UFileSystemWriter;
15
16 7
	public static function glob_recursive($pattern, $flags = 0) {
17 7
		$files = \glob ( $pattern, $flags );
18 7
		foreach ( \glob ( \dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
19 4
			$files = \array_merge ( $files, self::glob_recursive ( $dir . '/' . \basename ( $pattern ), $flags ) );
20
		}
21 7
		return $files;
22
	}
23
24
	public static function deleteAllFilesFromFolder($folder) {
25
		$files = \glob ( $folder . '/*' );
26
		foreach ( $files as $file ) {
27
			if (\is_file ( $file ))
28
				\unlink ( $file );
29
		}
30
	}
31
32
	public static function deleteFile($filename) {
33
		if (\file_exists ( $filename ))
34
			return \unlink ( $filename );
35
		return false;
36
	}
37
38 4
	public static function safeMkdir($dir) {
39 4
		if (! \is_dir ( $dir ))
40 1
			return \mkdir ( $dir, 0777, true );
41 3
		return true;
42
	}
43
44 6
	public static function cleanPathname($path) {
45 6
		if (UString::isNotNull ( $path )) {
46 6
			if (\DS === "/")
47 6
				$path = \str_replace ( "\\", \DS, $path );
48
			else
49
				$path = \str_replace ( "/", \DS, $path );
50 6
			$path = \str_replace ( \DS . \DS, \DS, $path );
51 6
			if (! UString::endswith ( $path, \DS )) {
52 3
				$path = $path . \DS;
53
			}
54
		}
55 6
		return $path;
56
	}
57
58 4
	public static function cleanFilePathname($path) {
59 4
		if (UString::isNotNull ( $path )) {
60 4
			if (\DS === "/")
61 4
				$path = \str_replace ( "\\", \DS, $path );
62
			else
63
				$path = \str_replace ( "/", \DS, $path );
64 4
			$path = \str_replace ( \DS . \DS, \DS, $path );
65
		}
66 4
		return $path;
67
	}
68
69
	public static function tryToRequire($file) {
70
		if (\file_exists ( $file )) {
71
			require_once ($file);
72
			return true;
73
		}
74
		return false;
75
	}
76
77 1
	public static function lastModified($filename) {
78 1
		return \filemtime ( $filename );
79
	}
80
81 1
	public static function load($filename) {
82 1
		if (\file_exists ( $filename )) {
83 1
			return \file_get_contents ( $filename );
84
		}
85
		return false;
86
	}
87
88
	public static function getDirFromNamespace($ns) {
89
		return \ROOT . \DS . str_replace ( "\\", \DS, $ns );
90
	}
91
92
	public static function delTree($dir) {
93
		$files = array_diff ( scandir ( $dir ), array ('.','..' ) );
94
		foreach ( $files as $file ) {
95
			(is_dir ( "$dir/$file" )) ? self::delTree ( "$dir/$file" ) : unlink ( "$dir/$file" );
96
		}
97
		return rmdir ( $dir );
98
	}
99
100 1
	public static function getLines($filename, $reverse = false, $maxLines = null, $lineCallback = null) {
101 1
		if (file_exists ( $filename )) {
102
			$result = [ ];
103
			if ($reverse && isset ( $maxLines )) {
104
				$fl = fopen ( $filename, "r" );
105
				for($x_pos = 0, $ln = 0, $lines = [ ]; fseek ( $fl, $x_pos, SEEK_END ) !== - 1; $x_pos --) {
106
					$char = fgetc ( $fl );
107
					if ($char === "\n") {
108
						if (is_callable ( $lineCallback )) {
109
							$lineCallback ( $result, $lines [$ln] );
110
						} else {
111
							$result [] = $lines [$ln];
112
						}
113
						if (isset ( $maxLines ) && sizeof ( $result ) >= $maxLines) {
114
							fclose ( $fl );
115
							return $result;
116
						}
117
						$ln ++;
118
						continue;
119
					}
120
					$lines [$ln] = $char . ((array_key_exists ( $ln, $lines )) ? $lines [$ln] : '');
121
				}
122
				fclose ( $fl );
123
				return $result;
124
			} else {
125
				$handle = fopen ( $filename, "r" );
126
				if ($handle) {
127
					while ( ($line = fgets ( $handle )) !== false ) {
128
						if (is_callable ( $lineCallback )) {
129
							$lineCallback ( $result, $line );
130
						} else {
131
							$result [] = $line;
132
						}
133
						if (isset ( $maxLines ) && sizeof ( $result ) >= $maxLines) {
134
							fclose ( $handle );
135
							if (is_array ( $result )) {
136
								$result = array_reverse ( $result );
137
							}
138
							return $result;
139
						}
140
					}
141
					fclose ( $handle );
142
				} else {
143
					// error opening the file.
144
				}
145
				if ($reverse) {
146
					$result = array_reverse ( $result );
147
				}
148
				return $result;
149
			}
150
		}
151 1
		return [ ];
152
	}
153
}
154