Completed
Push — master ( fad9f9...8658c1 )
by Jean-Christophe
01:44
created

UFileSystem::glob_recursive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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