-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp-mirror
116 lines (90 loc) · 2.55 KB
/
app-mirror
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env php
<?php
require __DIR__ . '/../bootstrap/autoload.php';
use MAKS\Velox\Backend\Config;
use MAKS\Velox\Frontend\Path;
if (!Config::get('cli.app-mirror.enabled', false)) {
echo 'Command is disabled!', PHP_EOL;
exit(1);
}
$fileSystem = new class {
public function link(string $target, string $link): bool
{
if (is_link($link)) {
if (is_dir($link) && PHP_OS == 'WINNT') {
rmdir($link);
} else {
unlink($link);
}
}
if (!file_exists(dirname($link))) {
mkdir(dirname($link), 0755, true);
}
return symlink($target, $link);
}
public function delete(string $path): bool
{
if (is_link($path)) {
unlink($path);
}
if (is_dir($path)) {
$files = scandir($path);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$this->delete($path . '/' . $file);
}
}
return rmdir($path);
}
if (file_exists($path)) {
return unlink($path);
}
return false;
}
public function copy(string $source, string $destination): bool
{
if (file_exists($destination)) {
$this->delete($destination);
}
if (is_dir($source)) {
mkdir($destination, 755, true);
$files = scandir($source);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$this->copy($source . '/' . $file, $destination . '/' . $file);
}
}
return true;
}
if (file_exists($source)) {
return copy($source, $destination);
}
return false;
}
public function getValidName($name, string $original): string
{
return !is_int($name)
? Path::resolve('/public/' . (string)$name)
: str_replace(
Config::get('global.paths.root'),
Config::get('global.paths.public'),
$original
);
}
};
$link = Config::get('cli.app-mirror.args.link', []);
$copy = Config::get('cli.app-mirror.args.copy', []);
foreach ($link as $name => $target) {
$fileSystem->link(
$target,
$fileSystem->getValidName($name, $target)
);
}
foreach ($copy as $name => $source) {
$fileSystem->copy(
$source,
$fileSystem->getValidName($name, $source)
);
}
echo 'OK!', PHP_EOL;
exit(0);