-
Notifications
You must be signed in to change notification settings - Fork 0
/
psc
153 lines (128 loc) · 4.29 KB
/
psc
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
define('VERSION', '1.5.7');
$configFile = __DIR__ . DIRECTORY_SEPARATOR . 'config.php';
if (!file_exists($configFile))
printl("Error! config.php is required to use PSC CLI!", 'black', true, 'red');
require_once($configFile);
function printl(string $text, string $textColor = 'default', bool $end = false, string $bgColor = '')
{
$textColors = [
'default' => '39m', 'red' => '31m', 'green' => '32m', 'yellow' => '33m', 'blue' => '34m',
'magenta' => '35m', 'cyan' => '36m', 'lightRed' => '91m', 'lightGreen' => '92m',
'lightYellow' => '93m', 'lightCyan' => '96m', 'white' => '97m', 'black' => '30m'
];
$tCode = $textColors[$textColor] ?? $textColor;
$backgroundColors = [
'black' => '40m', 'red' => '41m', 'green' => '42m', 'yellow' => '43m', 'blue' => '44m',
'magenta' => '45m', 'cyan' => '46m', 'lightGray' => '47m'
];
$bgCode = null;
if ($bgColor) $bgCode = $backgroundColors[$bgColor] ?? $bgColor;
echo "\e[{$tCode}" . ($bgCode ? "\e[{$bgCode}" : '') . "{$text} \e[0m\n";
if ($end) die;
}
function newFolder(string $path)
{
try {
mkdir($path);
} catch (\Exception $e) {
printl("Error when try to create directory: '$path'", 'red');
printl($e, 'black', true, 'red');
}
}
function newFile(string $path, string $content)
{
try {
file_put_contents($path, $content);
} catch (\Exception $e) {
printl("Error when try to write file: '$path'", 'red');
printl($e, 'black', true, 'red');
}
}
function interpreter($args)
{
$cmd = $args[1];
if (!function_exists($cmd)) {
printl("'$cmd' is not a valid PSC command", 'lightRed');
printl("Use 'help' to see a list of available commands.", 'black', true, 'yellow');
}
call_user_func($cmd, $args);
}
function create($args)
{
$itemType = $args[2] ?? null;
$itemName = $args[3] ?? null;
if (!$itemType) {
printl('You must specify a item to be created', 'red');
printl('E.g.: create model users', 'black', true, 'yellow');
}
if (!$itemName) {
printl('You must specify a name to item', 'red');
printl('E.g.: create controller main', 'black', true, 'yellow');
}
$supportedTypes = ['Model', 'Controller', 'Library', 'Class'];
$itemType[0] = strtoupper($itemType[0]);
$itemName[0] = strtoupper($itemName[0]);
if (!in_array($itemType, $supportedTypes))
printl("'$itemType' is not a supported item to create", 'red', true);
$folder = '';
if ($itemType == 'Class') {
$x = explode('/', $itemName);
if (count($x) > 1) {
$itemName = end($x);
unset($x[count($x) - 1]);
foreach ($x as $subF) {
$folder .= $subF . DS;
}
}
} else {
$folder = $itemType . 's' . DS;
if ($itemType[strlen($itemType) - 1] == 'y') $folder = substr($itemType, 0, strlen($itemType) - 1) . 'ies' . DS;
}
$namespace = '';
if ($folder) {
$namespace = str_replace('/', '\\', $folder);
if ($namespace[strlen($namespace) - 1] == '\\') $namespace = substr($namespace, 0, strlen($namespace) - 1);
$namespace = "namespace $namespace;";
}
$txt =
"<?php
$namespace
use _core\PSC;
class $itemName extends PSC
{
public function __construct()
{
parent::__construct();
}
}
";
if (!file_exists(SOURCEPATH . $folder)) newFolder(SOURCEPATH . $folder);
if (file_exists(SOURCEPATH . $folder . "$itemName.php"))
printl("$itemType '$itemName' already exists!", 'red', true);
newFile(SOURCEPATH . $folder . "$itemName.php", $txt);
printl("Success!", 'black', false, 'green');
}
function help($args)
{
$commands = [
'create' => [
'Create a class based on specified type inside respective folder',
"E.g.: create <controller || model || library> <name>"
],
'help' => ['Show this command list'],
'version' => ['Show PSC current version']
];
foreach ($commands as $name => $data) {
$description = $data[0];
$additional = $data[1] ?? null;
printl("$name - $description", 'yellow');
if ($additional) printl($additional);
printl('');
}
}
function version($args)
{
printl(VERSION);
}
interpreter($argv);