Completed
Push — master ( 313ff4...885da7 )
by Jean-Christophe
02:12
created

UFileSystem::safeMkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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 deleteFile($filename){
29
		if (\file_exists($filename))
30
			return \unlink($filename);
31
		return false;
32
	}
33
34
	public static function safeMkdir($dir) {
35
		if (!\is_dir($dir))
36
			return \mkdir($dir, 0777, true);
37
		return true;
38
	}
39
40
	public static function cleanPathname($path) {
41
		if (UString::isNotNull($path)) {
42
			if (DS === "/")
43
				$path=\str_replace("\\", DS, $path);
44
			else
45
				$path=\str_replace("/", DS, $path);
46
			$path=\str_replace(DS . DS, DS, $path);
47
			if (!UString::endswith($path, DS)) {
48
				$path=$path . DS;
49
			}
50
		}
51
		return $path;
52
	}
53
54
	public static function openReplaceInTemplateFile($source, $keyAndValues) {
55
		if (\file_exists($source)) {
56
			$str=\file_get_contents($source);
57
			return self::replaceFromTemplate($str, $keyAndValues);
58
		}
59
		return false;
60
	}
61
62
	public static function openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues) {
63
		if (($str=self::openReplaceInTemplateFile($source, $keyAndValues))) {
64
			return \file_put_contents($destination, $str, LOCK_EX);
65
		}
66
		return false;
67
	}
68
69
	public static function replaceFromTemplate($content, $keyAndValues) {
70
		array_walk($keyAndValues, function (&$item) {
71
			if (\is_array($item))
72
				$item=\implode("\n", $item);
73
		});
74
		$str=\str_replace(array_keys($keyAndValues), array_values($keyAndValues), $content);
75
		return $str;
76
	}
77
78
	public static function replaceWriteFromContent($content, $destination, $keyAndValues) {
79
		return \file_put_contents($destination, self::replaceFromTemplate($content, $keyAndValues), LOCK_EX);
80
	}
81
82
	public static function tryToRequire($file) {
83
		if (\file_exists($file)) {
84
			require_once ($file);
85
			return true;
86
		}
87
		return false;
88
	}
89
90
	public static function lastModified($filename) {
91
		return \filemtime($filename);
92
	}
93
94
	public static function load($filename){
95
		if (\file_exists($filename)) {
96
			return \file_get_contents($filename);
97
		}
98
		return false;
99
	}
100
101
	public static function save($filename,$content,$flags=LOCK_EX){
102
		return \file_put_contents($filename, $content, $flags);
103
	}
104
}
105