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 Modelarium\Laravel\Targets\ModelGenerator; |
10
|
|
|
use Modelarium\Options; |
11
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
12
|
|
|
|
13
|
|
|
class ModelariumScaffoldCommand extends Command |
14
|
|
|
{ |
15
|
|
|
use WriterTrait; |
|
|
|
|
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. Use "*" or "all" for all models} |
24
|
|
|
{--framework=* : The frameworks to use} |
25
|
|
|
{--modelDir= : directory to create models. default: app/Models} |
26
|
|
|
{--overwrite : overwrite files if they exist} |
27
|
|
|
{--lighthouse : use lighthouse directives} |
28
|
|
|
{--everything : make everything} |
29
|
|
|
{--model : make model} |
30
|
|
|
{--event : make event} |
31
|
|
|
{--migration : make migration} |
32
|
|
|
{--factory : make factory} |
33
|
|
|
{--seed : make seed} |
34
|
|
|
{--policy : make policy}'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The console command description. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $description = 'Creates scaffolding using Modelarium'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Options |
45
|
|
|
*/ |
46
|
|
|
protected $modelariumOptions = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new command instance. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
parent::__construct(); |
56
|
|
|
|
57
|
|
|
// read from Options() |
58
|
|
|
$this->modelariumOptions = new Options(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Execute the console command. |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function handle() |
67
|
|
|
{ |
68
|
|
|
$name = $this->argument('name'); |
69
|
|
|
|
70
|
|
|
$processor = new LaravelProcessor(); |
71
|
|
|
|
72
|
|
|
// parse args |
73
|
|
|
$processor->setRunModel($this->option('model') || $this->option('everything')); |
74
|
|
|
$processor->setRunMigration($this->option('migration') || $this->option('everything')); |
75
|
|
|
$processor->setRunFactory($this->option('factory') || $this->option('everything')); |
76
|
|
|
$processor->setRunSeed($this->option('seed') || $this->option('everything')); |
77
|
|
|
$processor->setRunPolicy($this->option('policy') || $this->option('everything')); |
78
|
|
|
$processor->setRunEvent($this->option('event') || $this->option('everything')); |
79
|
|
|
|
80
|
|
|
if ($this->option('modelDir') && is_string($this->option('modelDir'))) { |
81
|
|
|
ModelGenerator::setModelDir($this->option('modelDir')); |
82
|
|
|
} elseif ($this->modelariumOptions->getOption('modelarium', 'modelDir')) { |
83
|
|
|
ModelGenerator::setModelDir($this->modelariumOptions->getOption('modelarium', 'modelDir')); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// this generates the missing types and fills them, but it loses the directives. |
87
|
|
|
// if ($this->option('lighthouse')) { |
88
|
|
|
// $output = new BufferedOutput(); |
89
|
|
|
// $this->runCommand( |
90
|
|
|
// 'lighthouse:print', |
91
|
|
|
// [], |
92
|
|
|
// $output |
93
|
|
|
// ); |
94
|
|
|
// $processor->processString($output->fetch()); |
95
|
|
|
// SchemaPrinter::printFilteredSchema( |
96
|
|
|
// $schema, |
97
|
|
|
// static function ($type) : bool { |
98
|
|
|
// return ! Directive::isSpecifiedDirective($type); |
99
|
|
|
// }, |
100
|
|
|
// static function ($type) : bool { |
101
|
|
|
// return ! Type::isBuiltInType($type); |
102
|
|
|
// }, |
103
|
|
|
// $options |
104
|
|
|
// ); |
105
|
|
|
// } else { |
106
|
|
|
|
107
|
|
|
$files = [ |
108
|
|
|
__DIR__ . '/../../../Types/Graphql/scalars.graphql' |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
if ($this->option('lighthouse') ?? $this->modelariumOptions->getOption('modelarium', 'lighthouse')) { |
112
|
|
|
$files[] = __DIR__ . '/../../Graphql/definitionsLighthouse.graphql'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$path = base_path('graphql'); |
|
|
|
|
116
|
|
|
$dir = \Safe\scandir($path); |
117
|
|
|
|
118
|
|
|
// parse directives from lighthouse |
119
|
|
|
$modelNames = array_diff($dir, array('.', '..')); |
120
|
|
|
|
121
|
|
|
foreach ($modelNames as $n) { |
122
|
|
|
if (mb_strpos($n, '.graphql') === false) { |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
$files[] = base_path('graphql/' . $n); |
126
|
|
|
} |
127
|
|
|
$processor->processFiles($files); |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
$files = $processor->getCollection(); |
131
|
|
|
if ($name && $name !== '*' && $name !== 'all') { |
132
|
|
|
$files = $files->filter( |
133
|
|
|
function (GeneratedItem $g) use ($name) { |
134
|
|
|
if (is_array($name)) { |
135
|
|
|
throw new \Exception('Arrays not supported yet'); |
136
|
|
|
} else { |
137
|
|
|
return mb_stripos($g->filename, $name); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->writeFiles( |
144
|
|
|
$files, |
145
|
|
|
base_path(), |
146
|
|
|
(bool)$this->option('overwrite') |
147
|
|
|
); |
148
|
|
|
$this->info('Finished scaffolding. You might want to run `composer dump-autoload`'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function getPathGraphql(string $name): string |
152
|
|
|
{ |
153
|
|
|
return base_path('graphql/' . $name . '.graphql'); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|