|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\base\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UFileSystem; |
|
6
|
|
|
|
|
7
|
|
|
trait UFileSystemWriter { |
|
8
|
|
|
|
|
9
|
7 |
|
public static function openReplaceInTemplateFile($source, $keyAndValues) { |
|
10
|
7 |
|
if (\file_exists($source)) { |
|
11
|
7 |
|
$str=\file_get_contents($source); |
|
12
|
7 |
|
return self::replaceFromTemplate($str, $keyAndValues); |
|
13
|
|
|
} |
|
14
|
|
|
return false; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
7 |
|
public static function openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues) { |
|
18
|
7 |
|
if (($str=self::openReplaceInTemplateFile($source, $keyAndValues))) { |
|
19
|
7 |
|
return \file_put_contents($destination, $str, LOCK_EX); |
|
20
|
|
|
} |
|
21
|
|
|
return false; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
7 |
|
public static function replaceFromTemplate($content, $keyAndValues) { |
|
25
|
7 |
|
\array_walk($keyAndValues, function (&$item) { |
|
26
|
7 |
|
if (\is_array($item)) |
|
27
|
|
|
$item=\implode("\n", $item); |
|
28
|
|
|
}); |
|
29
|
7 |
|
$str=\str_replace(\array_keys($keyAndValues), \array_values($keyAndValues), $content); |
|
30
|
7 |
|
return $str; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public static function replaceWriteFromContent($content, $destination, $keyAndValues) { |
|
34
|
1 |
|
return \file_put_contents($destination, self::replaceFromTemplate($content, $keyAndValues), LOCK_EX); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
9 |
|
public static function save($filename,$content,$flags=LOCK_EX){ |
|
38
|
9 |
|
return \file_put_contents($filename, $content, $flags); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
8 |
|
public static function xcopy($source, $dest, $permissions = 0755){ |
|
42
|
8 |
|
if (\is_link($source)) { |
|
43
|
|
|
return \symlink(\readlink($source), $dest); |
|
44
|
|
|
} |
|
45
|
8 |
|
if (\is_file($source)) { |
|
46
|
8 |
|
return \copy($source, $dest); |
|
47
|
|
|
} |
|
48
|
8 |
|
if (!\is_dir($dest)) { |
|
49
|
3 |
|
\mkdir($dest, $permissions,true); |
|
50
|
|
|
} |
|
51
|
8 |
|
$dir = \dir($source); |
|
52
|
8 |
|
while (false !== $entry = $dir->read()) { |
|
53
|
8 |
|
if ($entry == '.' || $entry == '..') { |
|
54
|
8 |
|
continue; |
|
55
|
|
|
} |
|
56
|
8 |
|
self::xcopy("$source/$entry", "$dest/$entry", $permissions); |
|
57
|
|
|
} |
|
58
|
8 |
|
$dir->close(); |
|
59
|
8 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function xmove(string $source,string $dest, int $permission=0755):bool { |
|
63
|
|
|
if (\is_link($source)) { |
|
64
|
|
|
if( \symlink(\readlink($source), $dest)){ |
|
65
|
|
|
return \unlink($source); |
|
66
|
|
|
} |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
if(self::xcopy($source, $dest,$permission)){ |
|
70
|
|
|
if(\is_dir($source)){ |
|
71
|
|
|
return UFileSystem::delTree($source); |
|
72
|
|
|
} |
|
73
|
|
|
return \unlink($source); |
|
74
|
|
|
} |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|