1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Modelarium\GeneratedCollection; |
7
|
|
|
use Modelarium\GeneratedItem; |
8
|
|
|
use Modelarium\Laravel\Processor as LaravelProcessor; |
9
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
10
|
|
|
|
11
|
|
|
// use Formularium\FrameworkComposer; |
12
|
|
|
// use Formularium\Frontend\Blade\Framework as FrameworkBlade; |
13
|
|
|
// use Formularium\Frontend\Vue\Framework as FrameworkVue; |
14
|
|
|
|
15
|
|
|
class ModelariumScaffoldCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'modelarium:scaffold |
23
|
|
|
{name : The model name} |
24
|
|
|
{--framework=* : The frameworks to use} |
25
|
|
|
{--overwrite : overwrite files if they exist} |
26
|
|
|
{--lighthouse : use lighthouse directives} |
27
|
|
|
{--everything : make everything} |
28
|
|
|
{--model : make model} |
29
|
|
|
{--event : make event} |
30
|
|
|
{--migration : make migration} |
31
|
|
|
{--factory : make factory} |
32
|
|
|
{--seed : make seed} |
33
|
|
|
{--policy : make policy} |
34
|
|
|
{--frontend : make frontend files}'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The console command description. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $description = 'Creates scaffolding using Modelarium'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new command instance. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
parent::__construct(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Execute the console command. |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function handle() |
59
|
|
|
{ |
60
|
|
|
$name = $this->argument('name'); |
61
|
|
|
|
62
|
|
|
$processor = new LaravelProcessor(); |
63
|
|
|
|
64
|
|
|
// parse args |
65
|
|
|
$processor->setRunModel($this->option('model') || $this->option('everything')); |
66
|
|
|
$processor->setRunMigration($this->option('migration') || $this->option('everything')); |
67
|
|
|
$processor->setRunFactory($this->option('factory') || $this->option('everything')); |
68
|
|
|
$processor->setRunSeed($this->option('seed') || $this->option('everything')); |
69
|
|
|
$processor->setRunPolicy($this->option('policy') || $this->option('everything')); |
70
|
|
|
$processor->setRunEvent($this->option('event') || $this->option('everything')); |
71
|
|
|
|
72
|
|
|
$files = [ |
73
|
|
|
// todo __DIR__ . '/../../Graphql/definitions.graphql', |
74
|
|
|
__DIR__ . '/../../../Types/Graphql/scalars.graphql' |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
if ($this->option('lighthouse')) { |
78
|
|
|
// TODO: see issue #2 |
79
|
|
|
// generate lighthouse directives |
80
|
|
|
// $output = new BufferedOutput(); |
81
|
|
|
// $this->call('lighthouse:ide-helper'); |
82
|
|
|
// $output->fetch(); |
83
|
|
|
|
84
|
|
|
$files[] = base_path('schema-directives.graphql'); |
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
$files[] = base_path(__DIR__ . '/../../Graphql/definitionsLighthouse.graphql'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($name === '*' || $name === 'all') { |
90
|
|
|
$path = base_path('graphql'); |
91
|
|
|
$dir = \Safe\scandir($path); |
92
|
|
|
|
93
|
|
|
// parse directives from lighthouse |
94
|
|
|
$modelNames = array_diff($dir, array('.', '..')); |
95
|
|
|
|
96
|
|
|
foreach ($modelNames as $n) { |
97
|
|
|
if (mb_strpos($n, '.graphql') === false) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
$files[] = base_path('graphql/' . $n); |
101
|
|
|
} |
102
|
|
|
$processor->processFiles($files); |
103
|
|
|
} elseif (!$name || is_array($name)) { |
104
|
|
|
$this->error('Invalid name parameter'); |
105
|
|
|
return; |
106
|
|
|
} else { |
107
|
|
|
try { |
108
|
|
|
$data = \Safe\file_get_contents($this->getPathGraphql($name)); |
109
|
|
|
$processor->processString($data); |
110
|
|
|
} catch (\Safe\Exceptions\FilesystemException $e) { |
111
|
|
|
$this->error("Cannot open model $name"); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->writeFiles( |
116
|
|
|
$processor->getCollection(), |
117
|
|
|
base_path(), |
118
|
|
|
(bool)$this->option('overwrite') |
119
|
|
|
); |
120
|
|
|
$this->info('Finished. You might want to run `composer dump-autoload`'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function frontend(): void |
124
|
|
|
{ |
125
|
|
|
// TODO |
126
|
|
|
// // setup stuff |
127
|
|
|
// $frameworks = $this->option('framework'); |
128
|
|
|
// FrameworkComposer::set($frameworks); |
129
|
|
|
|
130
|
|
|
// /** |
131
|
|
|
// * @var FrameworkVue $vue |
132
|
|
|
// */ |
133
|
|
|
// $vue = FrameworkComposer::getByName('Vue'); |
134
|
|
|
// $blade = FrameworkComposer::getByName('Blade'); |
135
|
|
|
|
136
|
|
|
// if ($this->hasOption('stubdir')) { |
137
|
|
|
// $this->stubDir = $this->option('stubdir'); |
138
|
|
|
// } |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
// if ($this->option('frontend') || $this->option('everything')) { |
143
|
|
|
// if ($vue) { |
144
|
|
|
// $this->makeVueScaffold(); |
145
|
|
|
// $this->makeVue($vue, 'Base', 'viewable'); |
146
|
|
|
// $this->makeVue($vue, 'Item', 'viewable'); |
147
|
|
|
// $this->makeVue($vue, 'ListPage', 'viewable'); |
148
|
|
|
// $this->makeVue($vue, 'ShowPage', 'viewable'); |
149
|
|
|
// $this->makeVue($vue, 'EditPage', 'editable'); |
150
|
|
|
// $this->line('Generated Vue'); |
151
|
|
|
// } elseif ($blade) { |
152
|
|
|
// $this->makeBlade($blade, 'show', 'viewable'); |
153
|
|
|
// $this->makeBlade($blade, 'index', 'viewable'); |
154
|
|
|
// $this->makeBlade($blade, 'form', 'editable'); |
155
|
|
|
// $this->line('Generated Blade'); |
156
|
|
|
// } else { |
157
|
|
|
// // TODO: react? |
158
|
|
|
// } |
159
|
|
|
// } |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function getPathGraphql(string $name): string |
163
|
|
|
{ |
164
|
|
|
return base_path('graphql/' . $name . '.graphql'); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function writeFiles(GeneratedCollection $collection, string $basepath, bool $overwrite = true): self |
168
|
|
|
{ |
169
|
|
|
foreach ($collection as $element) { |
170
|
|
|
/** |
171
|
|
|
* @var GeneratedItem $element |
172
|
|
|
*/ |
173
|
|
|
$path = $basepath . '/' . $element->filename; |
174
|
|
|
$this->writeFile( |
175
|
|
|
$path, |
176
|
|
|
($element->onlyIfNewFile ? false : $overwrite), |
177
|
|
|
$element->contents |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Takes a stub file and generates the target file with replacements. |
185
|
|
|
* |
186
|
|
|
* @param string $targetPath The path for the stub file. |
187
|
|
|
* @param boolean $overwrite |
188
|
|
|
* @param string $data The data to write |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
protected function writeFile(string $targetPath, bool $overwrite, string $data) |
192
|
|
|
{ |
193
|
|
|
if (file_exists($targetPath) && !$overwrite) { |
194
|
|
|
$this->comment("File $targetPath already exists, not overwriting."); |
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$dir = dirname($targetPath); |
199
|
|
|
if (!is_dir($dir)) { |
200
|
|
|
\Safe\mkdir($dir, 0777, true); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$ret = \Safe\file_put_contents($targetPath, $data); |
204
|
|
|
if (!$ret) { |
205
|
|
|
$this->error("Cannot write to $targetPath"); |
206
|
|
|
return; |
207
|
|
|
} |
208
|
|
|
$this->line("Wrote $targetPath"); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|