-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathace.php
More file actions
202 lines (156 loc) · 4.91 KB
/
ace.php
File metadata and controls
202 lines (156 loc) · 4.91 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env php
<?php declare(strict_types=1);
define('BASE_PATH', __DIR__);
require BASE_PATH . '/vendor/autoload.php';
(new \ACE\Support\Env(BASE_PATH))->load();
require_once BASE_PATH . '/ace/Support/boot.php';
use ACE\Database\DatabaseDriverInterface;
use ACE\Database\DbmlParser;
use ACE\Database\CodeGenerator;
$args = $argv;
array_shift($args);
if (empty($args)) {
showHelp();
exit(0);
}
$command = $args[0];
switch ($command) {
case 'api':
$dbmlPath = $args[1] ?? BASE_PATH . '/database/schema.dbml';
generateApi($dbmlPath);
break;
case 'migrate':
runMigrations();
break;
case 'serve':
startServer();
break;
default:
echo "Error: Unknown command '{$command}'\n\n";
showHelp();
exit(1);
}
function showHelp(): void
{
echo <<<HELP
ACE Framework - Schema-First API Generator
Usage:
./ace <command> [options]
Commands:
api [path] Generate complete API from DBML schema
Default path: database/schema.dbml
migrate Run database migrations
serve Start development server
Examples:
./ace api Generate from default schema
./ace api database/custom.dbml Generate from custom schema
./ace migrate Run all pending migrations
./ace serve Start server on port 8080
HELP;
}
function generateApi(string $dbmlPath): void
{
if (!file_exists($dbmlPath)) {
exit("Error: DBML file not found: {$dbmlPath}\n");
}
echo "========================================\n";
echo "ACE - API Generator\n";
echo "========================================\n\n";
echo "Reading: {$dbmlPath}\n\n";
$dbmlContent = file_get_contents($dbmlPath);
$parser = new DbmlParser();
$schema = $parser->parse($dbmlContent);
$tableCount = count($schema['tables']);
$relationCount = count($schema['relationships']);
echo "Found: {$tableCount} tables, {$relationCount} relationships\n\n";
foreach ($schema['tables'] as $tableName => $tableData) {
$columnCount = count($tableData['columns']);
echo " • {$tableName} ({$columnCount} columns)\n";
}
echo "\nGenerating...\n\n";
$generator = new CodeGenerator();
$generator->generate($schema, $parser);
echo "\n========================================\n";
echo "✓ Complete!\n";
echo "========================================\n\n";
echo "Next steps:\n";
echo " ./ace migrate # Create database tables\n";
echo " ./ace serve # Start server\n\n";
}
function runMigrations(): void
{
echo "Running migrations...\n\n";
$migrationPath = BASE_PATH . '/database/migrations';
if (!is_dir($migrationPath)) {
echo "No migrations directory found.\n";
return;
}
$db = getDbConnection();
ensureMigrationsTable($db);
$files = glob($migrationPath . '/*.php');
if (empty($files)) {
echo "No migration files found.\n";
return;
}
sort($files);
$executed = $db->query("SELECT migration FROM migrations")->fetchAll(\PDO::FETCH_COLUMN);
$ranCount = 0;
foreach ($files as $file) {
$filename = basename($file);
if (in_array($filename, $executed)) {
continue;
}
echo "Running: {$filename}... ";
require_once $file;
$className = getClassNameFromFile($file);
if (!$className || !class_exists($className)) {
echo "SKIP (no class found)\n";
continue;
}
$migration = new $className();
$migration->up($db);
$db->exec("INSERT INTO migrations (migration, batch) VALUES ('{$filename}', 1)");
echo "OK\n";
$ranCount++;
}
echo "\n";
if ($ranCount > 0) {
echo "✓ {$ranCount} migrations executed\n";
} else {
echo "✓ Nothing to migrate\n";
}
}
function ensureMigrationsTable(DatabaseDriverInterface $db): void
{
$db->exec("CREATE TABLE IF NOT EXISTS migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
batch INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
}
function getDbConnection(): DatabaseDriverInterface
{
$dbManager = app(\ACE\Database\Db::class);
return $dbManager->driver(env('DB_CONNECTION', 'mysql'));
}
function getClassNameFromFile(string $path): ?string
{
$contents = file_get_contents($path);
if (preg_match('/class\s+(\w+)/', $contents, $matches)) {
return $matches[1];
}
return null;
}
function startServer(): void
{
echo "Starting development server...\n";
echo "Server running at: http://localhost:8080\n";
echo "Press Ctrl+C to stop\n\n";
$publicPath = BASE_PATH . '/public';
if (!is_dir($publicPath)) {
exit("Error: Public directory not found\n");
}
chdir($publicPath);
passthru('php -S localhost:8080 2>&1');
}