-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli
183 lines (146 loc) · 6.04 KB
/
cli
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace ZenithPHP;
use ZenithPHP\Core\Database\Migration;
use ZenithPHP\Core\Database\Schema;
use ZenithPHP\Core\Database\Database;
use ZenithPHP\Core\Http\InitEnv;
// Autoload necessary classes and environment configurations
require_once 'vendor/autoload.php';
require_once 'Core/src/Database/Migration.php';
require_once 'Core/src/Database/Schema.php';
class CLI
{
protected array $arguments;
public function __construct(array $argv)
{
$this->arguments = $argv;
$this->handle();
}
protected function handle()
{
$command = $this->arguments[1] ?? null;
$commands = [
'make:controller' => 'makeController',
'make:model' => 'makeModel',
'make:migration' => 'makeMigration',
'migrate' => 'runMigrations',
'migrate:rollback' => 'rollbackMigrations',
'migrate:fresh' => 'migrateFresh',
'run' => 'runProject',
];
if (array_key_exists($command, $commands)) {
$this->{$commands[$command]}();
} else {
echo "Invalid command. Use 'make:controller', 'make:model', 'make:migration', 'migrate', 'migrate:rollback', or 'run'.\n";
}
}
protected function makeController()
{
$controllerName = $this->arguments[2] ?? $this->prompt("Enter the name of the controller: ");
$path = "App/Controllers/{$controllerName}.php";
if (file_exists($path)) {
echo "Controller '{$controllerName}' already exists.\n";
return;
}
$template = "<?php\n\nnamespace ZenithPHP\App\Controllers;\n\nuse ZenithPHP\Core\Controller\Controller;\n\nclass {$controllerName} extends Controller\n{\n}\n";
file_put_contents($path, $template);
echo "Controller '{$controllerName}' created successfully.\n";
}
protected function makeModel()
{
$modelName = $this->arguments[2] ?? $this->prompt("Enter the name of the model: ");
$path = "App/Models/{$modelName}.php";
if (file_exists($path)) {
echo "Model '{$modelName}' already exists.\n";
return;
}
$template = "<?php\n\nnamespace ZenithPHP\App\Models;\n\nuse ZenithPHP\Core\Model\Model;\n\nclass {$modelName} extends Model\n{\n protected string \$table_name = '" . strtolower($modelName) . "s';\n}\n";
file_put_contents($path, $template);
echo "Model '{$modelName}' created successfully.\n";
}
protected function makeMigration()
{
$migrationName = $this->arguments[2] ?? $this->prompt("Enter the name of the migration: ");
$timestamp = date('Y_m_d_His');
$file = "Migrations/_{$timestamp}_{$migrationName}.php";
if (!file_exists('Migrations')) mkdir('Migrations', 0755, true);
$template = "<?php\n\nnamespace ZenithPHP\Migrations;\n\nuse ZenithPHP\Core\Database\Migration;\nuse ZenithPHP\Core\Database\Schema;\n\nclass _{$timestamp}_{$migrationName} extends Migration\n{\n public function up()\n {\n Schema::create('{$migrationName}', function (Schema \$table) {\n \$table->id();\n // Add columns\n });\n }\n\n public function down()\n {\n Schema::drop('{$migrationName}');\n }\n}\n";
file_put_contents($file, $template);
echo "Migration '{$migrationName}' created successfully at '{$file}'.\n";
}
protected function runMigrations()
{
$this->loadEnv();
$migrationFiles = glob('Migrations/*.php');
if (empty($migrationFiles)) {
echo "No migration files found.\n";
return;
}
foreach ($migrationFiles as $file) {
require_once $file;
$className = 'ZenithPHP\Migrations\\' . basename($file, '.php');
if (class_exists($className)) {
(new $className)->up();
echo "Migration '{$className}' executed successfully.\n";
} else {
echo "Class '{$className}' not found in '{$file}'.\n";
}
}
}
protected function rollbackMigrations()
{
$this->loadEnv();
$migrationFiles = array_reverse(glob('Migrations/*.php'));
if (empty($migrationFiles)) {
echo "No migration files found.\n";
return;
}
foreach ($migrationFiles as $file) {
require_once $file;
$className = 'ZenithPHP\Migrations\\' . basename($file, '.php');
if (class_exists($className)) {
(new $className)->down();
echo "Rolled back: " . basename($file) . "\n";
} else {
echo "Class '{$className}' not found in '{$file}'.\n";
}
}
}
protected function migrateFresh()
{
// \ZenithPHP\Core\Http\InitEnv::load();
$this->loadEnv();
$this->dropAllTables();
$this->runMigrations();
}
protected function dropAllTables()
{
$db = Database::connect();
$tables = $db->query("SHOW TABLES")->fetchAll(\PDO::FETCH_COLUMN);
foreach ($tables as $table) {
$db->exec("DROP TABLE IF EXISTS `{$table}`");
echo "Dropped table: {$table}\n";
}
}
protected function runProject()
{
$port = 8000;
$publicPath = __DIR__ . '/Public';
$routerPath = "$publicPath/router.php";
echo "Starting server on http://localhost:$port\n";
chdir($publicPath);
file_put_contents($routerPath, "<?php if (php_sapi_name() === 'cli-server') { \$filePath = __DIR__ . parse_url(\$_SERVER['REQUEST_URI'], PHP_URL_PATH); if (file_exists(\$filePath) && !is_dir(\$filePath)) return false; } require 'index.php';");
register_shutdown_function(fn() => unlink($routerPath) && print("Temporary router file deleted.\n"));
passthru("php -S localhost:$port router.php");
}
private function loadEnv()
{
InitEnv::load();
}
private function prompt(string $message): string
{
echo $message;
return trim(fgets(STDIN));
}
}
new CLI($argv);