Passed
Branch master (941c30)
by Jean-Christophe
08:38
created

UFileSystem::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
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 5
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
 * Ubiquity\utils\base$UFileSystem
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.2
14
 *
15
 */
16
class UFileSystem {
17
	use UFileSystemWriter;
18
19
	/**
20
	 * Find recursively pathnames matching a pattern
21
	 *
22
	 * @param string $pattern
23
	 * @param integer $flags
24
	 * @return array
25
	 */
26 26
	public static function glob_recursive($pattern, $flags = 0) {
27 26
		$files = \glob ( $pattern, $flags );
28 26
		foreach ( \glob ( \dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
29 20
			$files = \array_merge ( $files, self::glob_recursive ( $dir . '/' . \basename ( $pattern ), $flags ) );
30
		}
31 26
		return $files;
32
	}
33
34
	/**
35
	 * Deletes all files from a folder (not in subfolders)
36
	 *
37
	 * @param string $folder
38
	 * @param string $mask
39
	 */
40 2
	public static function deleteAllFilesFromFolder($folder, $mask = '*') {
41 2
		$files = \glob ( $folder . \DS . $mask );
42 2
		foreach ( $files as $file ) {
43 2
			if (\is_file ( $file ))
44 2
				\unlink ( $file );
45
		}
46 2
	}
47
48
	/**
49
	 * Deletes a file, in safe mode
50
	 *
51
	 * @param string $filename
52
	 * @return boolean
53
	 */
54 1
	public static function deleteFile($filename) {
55 1
		if (\file_exists ( $filename ))
56 1
			return \unlink ( $filename );
57
		return false;
58
	}
59
60
	/**
61
	 * Tests the existance and eventually creates a directory
62
	 *
63
	 * @param string $dir
64
	 * @param int $mode
65
	 * @param boolean $recursive
66
	 * @return boolean
67
	 */
68 35
	public static function safeMkdir($dir, $mode = 0777, $recursive = true) {
69 35
		if (! \is_dir ( $dir ))
70 10
			return \mkdir ( $dir, $mode, $recursive );
71 30
		return true;
72
	}
73
74
	/**
75
	 * Cleans a directory path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR
76
	 *
77
	 * @param string $path
78
	 * @return string
79
	 */
80 13
	public static function cleanPathname($path) {
81 13
		if (UString::isNotNull ( $path )) {
82 13
			if (\DS === "/")
83 13
				$path = \str_replace ( "\\", \DS, $path );
84
			else
85
				$path = \str_replace ( "/", \DS, $path );
86 13
			$path = \str_replace ( \DS . \DS, \DS, $path );
87 13
			if (! UString::endswith ( $path, \DS )) {
88 4
				$path = $path . \DS;
89
			}
90
		}
91 13
		return $path;
92
	}
93
94
	/**
95
	 * Cleans a file path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR
96
	 *
97
	 * @param string $path
98
	 * @return string
99
	 */
100 15
	public static function cleanFilePathname($path) {
101 15
		if (UString::isNotNull ( $path )) {
102 15
			if (\DS === "/")
103 15
				$path = \str_replace ( "\\", \DS, $path );
104
			else
105
				$path = \str_replace ( "/", \DS, $path );
106 15
			$path = \str_replace ( \DS . \DS, \DS, $path );
107
		}
108 15
		return $path;
109
	}
110
111
	/**
112
	 * Try to require a file, in safe mode
113
	 *
114
	 * @param string $file
115
	 * @return boolean
116
	 */
117
	public static function tryToRequire($file) {
118
		if (\file_exists ( $file )) {
119
			require_once ($file);
120
			return true;
121
		}
122
		return false;
123
	}
124
125
	/**
126
	 * Gets file modification time
127
	 *
128
	 * @param string $filename
129
	 * @return number
130
	 */
131 1
	public static function lastModified($filename) {
132 1
		return \filemtime ( $filename );
133
	}
134
135
	/**
136
	 * Reads entire file into a string in safe mode
137
	 *
138
	 * @param string $filename
139
	 * @return string|boolean
140
	 */
141 2
	public static function load($filename) {
142 2
		if (\file_exists ( $filename )) {
143 1
			return \file_get_contents ( $filename );
144
		}
145 1
		return false;
146
	}
147
148
	/**
149
	 * Returns the directory base on ROOT, corresponding to a namespace
150
	 *
151
	 * @param string $ns
152
	 * @return string
153
	 */
154 3
	public static function getDirFromNamespace($ns) {
155 3
		return \ROOT . \DS . str_replace ( "\\", \DS, $ns );
156
	}
157
158
	/**
159
	 * Deletes recursivly a folder and its content
160
	 *
161
	 * @param string $dir
162
	 * @return boolean
163
	 */
164 2
	public static function delTree($dir) {
165 2
		$files = array_diff ( scandir ( $dir ), array ('.','..' ) );
166 2
		foreach ( $files as $file ) {
167 2
			(is_dir ( "$dir/$file" )) ? self::delTree ( "$dir/$file" ) : unlink ( "$dir/$file" );
168
		}
169 2
		return rmdir ( $dir );
170
	}
171
172
	/**
173
	 * Returns the lines of a file in an array
174
	 *
175
	 * @param string $filename
176
	 * @param boolean $reverse
177
	 * @param null|int $maxLines
178
	 * @param callback $lineCallback
179
	 * @return array
180
	 */
181 2
	public static function getLines($filename, $reverse = false, $maxLines = null, $lineCallback = null) {
182 2
		if (file_exists ( $filename )) {
183 2
			if ($reverse && isset ( $maxLines )) {
184 2
				$result = [ ];
185 2
				$fl = fopen ( $filename, "r" );
186 2
				for($x_pos = 0, $ln = 0, $lines = [ ]; fseek ( $fl, $x_pos, SEEK_END ) !== - 1; $x_pos --) {
187 2
					$char = fgetc ( $fl );
188 2
					if ($char === "\n") {
189 2
						if (is_callable ( $lineCallback )) {
190 2
							$lineCallback ( $result, $lines [$ln] );
191
						} else {
192
							$result [] = $lines [$ln];
193
						}
194 2
						if (isset ( $maxLines ) && sizeof ( $result ) >= $maxLines) {
195 2
							fclose ( $fl );
196 2
							return $result;
197
						}
198 2
						$ln ++;
199 2
						continue;
200
					}
201 2
					$lines [$ln] = $char . ((array_key_exists ( $ln, $lines )) ? $lines [$ln] : '');
202
				}
203
				fclose ( $fl );
204
				return $result;
205
			} else {
206 1
				return self::getLinesByLine ( $filename, $reverse, $maxLines, $lineCallback );
207
			}
208
		}
209
		return [ ];
210
	}
211
212 1
	protected static function getLinesByLine($filename, $reverse, $maxLines, $lineCallback) {
213 1
		$result = [ ];
214 1
		$handle = fopen ( $filename, "r" );
215 1
		if ($handle) {
216 1
			while ( ($line = fgets ( $handle )) !== false ) {
217 1
				if (is_callable ( $lineCallback )) {
218 1
					$lineCallback ( $result, $line );
219
				} else {
220
					$result [] = $line;
221
				}
222 1
				if (isset ( $maxLines ) && sizeof ( $result ) >= $maxLines) {
223 1
					fclose ( $handle );
224 1
					if (is_array ( $result ) && $reverse) {
225
						$result = array_reverse ( $result );
226
					}
227 1
					return $result;
228
				}
229
			}
230 1
			fclose ( $handle );
231
		} else {
232
			// error opening the file.
233
		}
234 1
		if ($reverse) {
235 1
			$result = array_reverse ( $result );
236
		}
237 1
		return $result;
238
	}
239
}
240