Passed
Push — master ( a647b4...0830ab )
by Jean-Christophe
01:30
created

UFileSystem::replaceFromTemplate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A UFileSystem::getDirFromNamespace() 0 2 1
1
<?php
2
namespace Ubiquity\utils\base;
3
4
use Ubiquity\utils\base\traits\UFileSystemWriter;
5
6
/**
7
 * File system utilities
8
 * @author jcheron <[email protected]>"
9
 * @version 1.0.2
10
 */
11
class UFileSystem {
12
	use UFileSystemWriter;
13
14
	public static function glob_recursive($pattern, $flags=0) {
15
		$files=\glob($pattern, $flags);
16
		foreach ( \glob(\dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir ) {
17
			$files=\array_merge($files, self::glob_recursive($dir . '/' . \basename($pattern), $flags));
18
		}
19
		return $files;
20
	}
21
22
	public static function deleteAllFilesFromFolder($folder) {
23
		$files=\glob($folder . '/*');
24
		foreach ( $files as $file ) {
25
			if (\is_file($file))
26
				\unlink($file);
27
		}
28
	}
29
30
	public static function deleteFile($filename){
31
		if (\file_exists($filename))
32
			return \unlink($filename);
33
		return false;
34
	}
35
36
	public static function safeMkdir($dir) {
37
		if (!\is_dir($dir))
38
			return \mkdir($dir, 0777, true);
39
		return true;
40
	}
41
42
	public static function cleanPathname($path) {
43
		if (UString::isNotNull($path)) {
44
			if (\DS === "/")
45
				$path=\str_replace("\\", \DS, $path);
46
			else
47
				$path=\str_replace("/", \DS, $path);
48
			$path=\str_replace(\DS . \DS, \DS, $path);
49
			if (!UString::endswith($path, \DS)) {
50
				$path=$path . \DS;
51
			}
52
		}
53
		return $path;
54
	}
55
	
56
	public static function cleanFilePathname($path) {
57
		if (UString::isNotNull($path)) {
58
			if (\DS === "/")
59
				$path=\str_replace("\\", \DS, $path);
60
			else
61
				$path=\str_replace("/", \DS, $path);
62
			$path=\str_replace(\DS . \DS, \DS, $path);
63
		}
64
		return $path;
65
	}
66
67
	public static function tryToRequire($file) {
68
		if (\file_exists($file)) {
69
			require_once ($file);
70
			return true;
71
		}
72
		return false;
73
	}
74
75
	public static function lastModified($filename) {
76
		return \filemtime($filename);
77
	}
78
79
	public static function load($filename){
80
		if (\file_exists($filename)) {
81
			return \file_get_contents($filename);
82
		}
83
		return false;
84
	}
85
86
	public static function getDirFromNamespace($ns){
87
		return \ROOT . \DS . str_replace ( "\\", \DS, $ns );
88
	}
89
	
90
	public static function delTree($dir) {
91
		$files = array_diff(scandir($dir), array('.','..'));
92
		foreach ($files as $file) {
93
			(is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
94
		}
95
		return rmdir($dir);
96
	}
97
	
98
	public static function getLines($filename,$reverse=false,$maxLines=null,$lineCallback=null){
99
		if(file_exists($filename)){
100
			$result=[];
101
			if($reverse && isset($maxLines)){
102
				$fl = fopen($filename, "r");
103
				 for($x_pos = 0, $ln = 0,$lines=[]; fseek($fl, $x_pos, SEEK_END) !== -1; $x_pos--) {
1 ignored issue
show
Bug introduced by
It seems like $fl can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
				 for($x_pos = 0, $ln = 0,$lines=[]; fseek(/** @scrutinizer ignore-type */ $fl, $x_pos, SEEK_END) !== -1; $x_pos--) {
Loading history...
104
					 $char = fgetc($fl);
1 ignored issue
show
Bug introduced by
It seems like $fl can also be of type false; however, parameter $handle of fgetc() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
					 $char = fgetc(/** @scrutinizer ignore-type */ $fl);
Loading history...
105
					 if ($char === "\n") {
106
					 	if(is_callable($lineCallback)){
107
					 		$lineCallback($result,$lines[$ln]);
108
					 	}else{
109
					 		$result[]=$lines[$ln];
110
					 	}
111
					 	if(isset($maxLines) && sizeof($result)>=$maxLines){
112
					 		fclose($fl);
1 ignored issue
show
Bug introduced by
It seems like $fl can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
					 		fclose(/** @scrutinizer ignore-type */ $fl);
Loading history...
113
					 		return $result;
114
					 	}
115
					 $ln++;
116
					 continue;
117
					 }
118
					 $lines[$ln] = $char . ((array_key_exists($ln, $lines)) ? $lines[$ln] : '');
119
				 }
120
				 fclose($fl);
121
				 return $result;
122
			 }else{
123
				$handle = fopen($filename, "r");
124
				if ($handle) {
1 ignored issue
show
introduced by
$handle is of type false|resource, thus it always evaluated to false.
Loading history...
125
					while (($line = fgets($handle)) !== false) {
126
						if(is_callable($lineCallback)){
127
							$lineCallback($result,$line);
128
						}else{
129
							$result[]=$line;
130
						}
131
						if(isset($maxLines) && sizeof($result)>=$maxLines){
132
							fclose($handle);
133
							if(is_array($result)){
134
								$result=array_reverse($result);
135
							}
136
							return $result;
137
						}
138
					}
139
					fclose($handle);
140
				} else {
141
					// error opening the file.
142
				}
143
				if($reverse){
144
					$result=array_reverse($result);
145
				}
146
				return $result;
147
			}
148
		}
149
		return [];
150
	}
151
}
152