-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactory
More file actions
209 lines (169 loc) · 5.23 KB
/
factory
File metadata and controls
209 lines (169 loc) · 5.23 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
203
204
205
206
207
208
209
#!/usr/bin/env php
<?php
if (php_sapi_name() !== 'cli') {
exit("This script must be run from the command line.\n");
}
$commands = $argv;
array_shift($commands); // حذف نام فایل php
if (empty($commands)) {
$logo = <<<LOGO
______ _ ______ _
| ____| | | ____| | |
| |__ | | __ _ _ __ ___ | |__ _ __ __ _ _ __ ___ _____ _____ _ __| | __
| __| | |/ _` | '__/ _ \ | __| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
| | | | (_| | | | __/ | | | | | (_| | | | | | | __/\ V V / (_) | | | <
|_| |_|\__,_|_| \___| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
Flare Framework
LOGO;
$red = "\033[1;31m";
$blue = "\033[1;34m";
$clear = "\033[2J\033[;H"; // پاک کردن صفحه
$reset = "\033[0m";
for ($i = 0; $i < 6; $i++) {
echo $clear;
echo ($i % 2 === 0 ? $red : $blue) . $logo . $reset;
usleep(400000); // 0.4 ثانیه چشمک
}
echo "\n";
echo <<<USAGE
Usage:
php factory make:model ClassName
php factory make:controller ClassName
php factory make:migration TableName
php factory migrate
USAGE;
exit;
}
$action = $commands[0];
$className = isset($commands[1]) ? ucfirst($commands[1]) : null;
switch ($action) {
case 'make:model':
case 'make:controller':
case 'make:migration':
if (!$className) {
exit("Error: Class or Table name is required for {$action}.\n");
}
break;
}
switch ($action) {
case 'make:model':
createModel($className);
break;
case 'make:controller':
createController($className);
break;
case 'make:migration':
createMigration($className);
break;
case 'migrate':
runMigrations();
break;
default:
exit("Unknown command: {$action}\n");
}
function createModel($name)
{
$directory = __DIR__ . "/Flare/Models";
$filePath = "$directory/{$name}.php";
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
if (file_exists($filePath)) {
exit("Model {$name} already exists.\n");
}
$tableName = strtolower($name);
$stub = <<<PHP
<?php
namespace Models;
use MysqliDb;
class {$name}
{
protected static \$tableName = '{$tableName}';
protected \$db;
public function __construct()
{
global \$db;
\$this->db = \$db;
}
}
PHP;
file_put_contents($filePath, $stub);
echo "✅ Model {$name} created successfully at Flare/Models/{$name}.php\n";
}
function createController($name)
{
$directory = __DIR__ . "/Flare/Controllers";
$filePath = "$directory/{$name}Controller.php";
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
if (file_exists($filePath)) {
exit("Controller {$name} already exists.\n");
}
$stub = <<<PHP
<?php
namespace Controllers;
class {$name}Controller
{
// تعریف کنترلر
}
PHP;
file_put_contents($filePath, $stub);
echo "✅ Controller {$name} created successfully at Flare/Controllers/{$name}Controller.php\n";
}
function createMigration($name)
{
$directory = __DIR__ . "/Flare/Migrations";
$filePath = "$directory/" . date('Y_m_d_His') . "_create_" . strtolower($name) . "_table.php";
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
$tableName = strtolower($name);
$stub = <<<PHP
<?php
require_once __DIR__ . '/../app/cli-config.php';
\$table = \$db->prefix . 'users';
if (!\$db->tableExists(\{$name})) {
\$query = "
CREATE TABLE IF NOT EXISTS `{\$table}` (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(150) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`token` VARCHAR(255) DEFAULT NULL,
`photo` VARCHAR(255) DEFAULT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
";
\$result = \$db->mysqli()->query(\$query);
if (\$result===true) {
echo "✅ Table '\$table' created successfully.\\n";
} else {
echo "❌ Failed to create table '\$table'.\\n";
}
} else {
echo "⚠️ Table '\$table' already exists.\\n";
}
PHP;
file_put_contents($filePath, $stub);
echo "✅ Migration created: {$filePath}\n";
}
function runMigrations()
{
$migrationsPath = __DIR__ . "/Flare/Migrations";
if (!is_dir($migrationsPath)) {
exit("No migrations folder found at {$migrationsPath}\n");
}
$files = glob($migrationsPath . "/*.php");
if (empty($files)) {
echo "No migration files found.\n";
return;
}
sort($files);
foreach ($files as $file) {
echo "🔄 Running migration: " . basename($file) . "\n";
include $file;
}
echo "✅ All migrations executed.\n";
}