-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakeModelCommand.php
More file actions
127 lines (97 loc) · 3.2 KB
/
Copy pathMakeModelCommand.php
File metadata and controls
127 lines (97 loc) · 3.2 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
<?php
declare(strict_types=1);
namespace Zero\Lib\Console\Commands;
use Zero\Lib\Console\Command\CommandInterface;
use Zero\Lib\Console\Support\Filesystem;
use Zero\Lib\Support\Str;
use Zero\Lib\Template;
final class MakeModelCommand implements CommandInterface
{
public function getName(): string
{
return 'make:model';
}
public function getDescription(): string
{
return 'Generate an Eloquent-like model class (optionally with a migration)';
}
public function getUsage(): string
{
return 'php zero make:model Name [-m|--migration] [--force]';
}
public function execute(array $argv): int
{
$parsed = $this->parseArguments($argv);
$name = $parsed['name'];
$force = $parsed['force'];
$withMigration = $parsed['migration'];
if ($name === null) {
\Zero\Lib\Log::channel('internal')->error("Usage: {$this->getUsage()}");
return 1;
}
$className = Str::studly($name);
$path = app_path('models/' . $className . '.php');
if (file_exists($path) && ! $force) {
\Zero\Lib\Log::channel('internal')->error("Model {$className} already exists. Use --force to overwrite.");
return 1;
}
Filesystem::ensureDirectory(dirname($path));
$contents = Template::render('model.tmpl', [
'class' => $className,
]);
file_put_contents($path, $contents);
\Zero\Lib\Log::channel('internal')->info("Model created: {$path}");
if ($withMigration) {
$status = $this->createMigration($className, $force);
if ($status !== 0) {
\Zero\Lib\Log::channel('internal')->error("Failed to create migration for model {$className}.");
return $status;
}
}
return 0;
}
/**
* Parse CLI arguments to extract the model name and flags.
*/
private function parseArguments(array $argv): array
{
$name = null;
$force = false;
$migration = false;
foreach (array_slice($argv, 2) as $argument) {
if ($argument === '--force') {
$force = true;
continue;
}
if ($argument === '-m' || $argument === '--migration') {
$migration = true;
continue;
}
if ($name === null && str_starts_with($argument, '-') === false) {
$name = $argument;
}
}
return [
'name' => $name,
'force' => $force,
'migration' => $migration,
];
}
/**
* Generate a migration using the registered command when requested.
*/
private function createMigration(string $className, bool $force): int
{
$table = Str::snake($className);
if (! str_ends_with($table, 's')) {
$table .= 's';
}
$migrationName = 'create_' . $table . '_table';
$command = new MakeMigrationCommand();
$arguments = ['zero', 'make:migration', $migrationName];
if ($force) {
$arguments[] = '--force';
}
return $command->execute($arguments);
}
}