Passed
Push — master ( 4211b3...817f31 )
by Jean-Christophe
05:14
created

UFileSystem::getLinesByLine()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.1317

Importance

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