-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
106 lines (81 loc) · 3.09 KB
/
setup.php
File metadata and controls
106 lines (81 loc) · 3.09 KB
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
<?php
final class Setup {
private static string $rootPath;
public static int $outSideVendor = 3;
public static function init() : void{
self::$rootPath = dirname(__DIR__, self::$outSideVendor)
. DIRECTORY_SEPARATOR;
}
public static function getRootPath(): string {
return self::$rootPath;
}
public static function createDirectories(array $directories) : void {
foreach ($directories as $dir) {
$path = self::$rootPath . $dir;
if (!is_dir($path)) {
if (mkdir($path, 0755, true)) {
echo "[*] Created directory: $path\n";
} else {
echo "[!] Failed to create directory: $path\n";
}
}
}
}
public static function createFiles(array $files) : void {
foreach ($files as $file => $content) {
$path = self::$rootPath . $file;
if (!file_exists($path)) {
if (file_put_contents($path, $content) !== false) {
echo "[*] Created file: $path\n";
} else {
echo "[!] Failed to create file: $path\n";
}
}
}
}
public static function updateComposerJson(): void {
$composerFile = self::$rootPath . 'composer.json';
if (!file_exists($composerFile)) {
echo "[!] composer.json not found!\n";
return;
}
$composerData = json_decode(file_get_contents($composerFile), true);
if (!isset($composerData['autoload']['psr-4'])) {
$composerData['autoload']['psr-4'] = [];
}
if (!isset($composerData['autoload']['psr-4']['Command\\'])) {
$composerData['autoload']['psr-4']['Command\\'] = "command";
}
file_put_contents($composerFile, json_encode($composerData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
echo "[*] composer.json updated!\n";
exec("composer dump-autoload");
}
}
Setup::init();
$rootPath = Setup::getRootPath();
$directories = [
'command',
'config',
];
$files = [
'config/cli.php' => "<?php\n\nreturn [\n 'CommandNamespace' => \"\\Command\",\n 'CommandDirectory' => \"/command\",\n ];",
'cool' => "#!/usr/bin/env php\n<?php\n\n" .
"use Configuration\\Config;\n" .
"use CoolCLI\\CoolCLI;\n\n" .
"require_once 'vendor/autoload.php';\n\n" .
"\$commandNamespace = Config::get('cli.CommandNamespace');\n" .
"\$commandDirectory = __DIR__ . Config::get('cli.CommandDirectory');\n\n" .
"CoolCLI::registerAllCommands(\$commandNamespace, \$commandDirectory);\n\n" .
"\$command = \$argv[1] ?? null;\n" .
"\$argument = \$argv[2] ?? null;\n" .
"\$option = \$argv[3] ?? null;\n\n" .
"CoolCLI::run(\$command, \$argument, \$option);\n"
];
Setup::createDirectories($directories);
Setup::createFiles($files);
Setup::updateComposerJson();
$scriptPath = __FILE__;
if (file_exists($scriptPath)) {
unlink($scriptPath);
echo "[*] Setup is completed !: $scriptPath\n";
}