Completed
Push — master ( b5f91d...d46a17 )
by Jean-Christophe
01:26
created

FsUtils::safeMkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace micro\utils;
4
5
/**
6
 * @author jc
7
 * @version 1.0.0.1
8
 */
9
class FsUtils {
10
11
	public static function glob_recursive($pattern, $flags=0) {
12
		$files=glob($pattern, $flags);
13
		foreach ( glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir ) {
14
			$files=array_merge($files, self::glob_recursive($dir . '/' . basename($pattern), $flags));
15
		}
16
		return $files;
17
	}
18
19
	public static function deleteAllFilesFromFolder($folder) {
20
		$files=glob($folder . '/*');
21
		foreach ( $files as $file ) {
22
			if (is_file($file))
23
				unlink($file);
24
		}
25
	}
26
27
	public static function safeMkdir($dir){
28
		if(!is_dir($dir))
29
			return mkdir($dir,0777,true);
30
		return true;
31
	}
32
33
	public static function cleanPathname($path){
34
		if(StrUtils::isNotNull($path)){
35
			if(DS==="/")
36
				$path=\str_replace("\\", DS, $path);
37
			else
38
				$path=\str_replace("/", DS, $path);
39
			$path=\str_replace(DS.DS, DS, $path);
40
			if(!StrUtils::endswith($path, DS)){
41
				$path=$path.DS;
42
			}
43
		}
44
		return $path;
45
	}
46
47
	public static function openReplaceInTemplateFile($source,$keyAndValues){
48
		if(file_exists($source)){
49
			$str=file_get_contents($source);
50
			return self::replaceFromTemplate($str, $keyAndValues);
51
		}
52
		return false;
53
	}
54
55
	public static function openReplaceWriteFromTemplateFile($source,$destination,$keyAndValues){
56
		if(($str=self::openReplaceInTemplateFile($source, $keyAndValues))){
57
			return file_put_contents($destination,$str,LOCK_EX);
58
		}
59
		return false;
60
	}
61
62
	public static function replaceFromTemplate($content,$keyAndValues){
63
		array_walk($keyAndValues, function(&$item){if(is_array($item)) $item=implode("\n", $item);});
64
		$str=str_replace(array_keys($keyAndValues), array_values($keyAndValues), $content);
65
		return $str;
66
	}
67
68
	public static function replaceWriteFromContent($content,$destination,$keyAndValues){
69
		return file_put_contents($destination,self::replaceFromTemplate($content, $keyAndValues),LOCK_EX);
70
	}
71
}
72