|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PWWEB\Artomator\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Illuminate\Console\GeneratorCommand; |
|
8
|
|
|
use Laracasts\Generators\Migrations\SchemaParser; |
|
9
|
|
|
use PWWEB\Artomator\Migrations\SyntaxBuilder; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
|
|
12
|
|
|
class ArtomatorQueryCommand extends GeneratorCommand |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The console command name. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $name = 'artomator:query'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The console command description. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $description = 'Create a new query class'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The type of class being generated. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $type = 'Query'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The package of class being generated. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $package = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the stub file for the generator. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getStub() |
|
48
|
|
|
{ |
|
49
|
|
|
$stub = 'query.stub'; |
|
50
|
|
|
$path = base_path() . config('artomator.stubPath'); |
|
51
|
|
|
$path = $path . $stub; |
|
52
|
|
|
|
|
53
|
|
|
if (file_exists($path) === true) { |
|
54
|
|
|
return $path; |
|
55
|
|
|
} else { |
|
56
|
|
|
return __DIR__ . '/Stubs/' . $stub; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the default namespace for the class. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $rootNamespace The class to return the namespace for. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
68
|
|
|
{ |
|
69
|
|
|
return $rootNamespace . '\GraphQL\Query'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Build the class with the given name. |
|
74
|
|
|
* |
|
75
|
|
|
* Remove the base query import if we are already in base namespace. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $name The name of the Query to build. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function buildClass($name) |
|
82
|
|
|
{ |
|
83
|
|
|
$replace = []; |
|
84
|
|
|
$replace = $this->buildSchemaReplacements($replace); |
|
85
|
|
|
$replace = $this->buildModelReplacements($replace); |
|
86
|
|
|
|
|
87
|
|
|
return str_replace( |
|
88
|
|
|
array_keys($replace), |
|
89
|
|
|
array_values($replace), |
|
90
|
|
|
parent::buildClass($name) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Build the schema replacement values. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $replace The existing replacements to append to. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function buildSchemaReplacements(array $replace) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->option('schema') !== false) { |
|
104
|
|
|
$schema = $this->option('schema') |
|
105
|
|
|
$schema = (new SchemaParser())->parse($schema); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$syntax = new SyntaxBuilder(); |
|
109
|
|
|
|
|
110
|
|
|
return array_merge( |
|
111
|
|
|
$replace, |
|
112
|
|
|
[ |
|
113
|
|
|
'{{schema_args}}' => $syntax->createArgsSchema($schema), |
|
114
|
|
|
'{{schema_resolves}}' => $syntax->createResolvesSchema($schema), |
|
115
|
|
|
] |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Build the model replacement values. |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $replace The existing replacement to append to. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function buildModelReplacements(array $replace) |
|
127
|
|
|
{ |
|
128
|
|
|
$modelClass = $this->parseModel((string) $this->option('model')); |
|
129
|
|
|
|
|
130
|
|
|
return array_merge( |
|
131
|
|
|
$replace, |
|
132
|
|
|
[ |
|
133
|
|
|
'DummyFullModelClass' => $modelClass, |
|
134
|
|
|
'DummyModelClass' => class_basename($modelClass), |
|
135
|
|
|
'DummyPluralModelClass' => Str::pluralStudly(class_basename($modelClass)), |
|
136
|
|
|
'DummyModelVariable' => lcfirst(class_basename($modelClass)), |
|
137
|
|
|
'DummyPackageVariable' => lcfirst($this->package) . ".", |
|
138
|
|
|
'DummyPackagePlaceholder' => config('app.name'), |
|
139
|
|
|
'DummyCopyrightPlaceholder' => config('artomator.copyright'), |
|
140
|
|
|
'DummyLicensePlaceholder' => config('artomator.license'), |
|
141
|
|
|
'DummyAuthorPlaceholder' => $this->parseAuthors(config('artomator.authors')), |
|
142
|
|
|
] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the formatted author(s) from the config file. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string[] $authors Authors array. |
|
150
|
|
|
* |
|
151
|
|
|
* @return string Formmated string of authors. |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function parseAuthors($authors) |
|
154
|
|
|
{ |
|
155
|
|
|
if (is_array($authors) === false and is_string($authors) === false) { |
|
156
|
|
|
throw new InvalidArgumentException('Authors must be an array of strings or a string.'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$formatted = ''; |
|
160
|
|
|
|
|
161
|
|
|
if (is_array($authors) === true) { |
|
162
|
|
|
if (is_string($authors[0]) === false) { |
|
163
|
|
|
throw new InvalidArgumentException('The array of authors must be strings.'); |
|
164
|
|
|
} |
|
165
|
|
|
$formatted .= array_shift($authors); |
|
166
|
|
|
|
|
167
|
|
|
foreach ($authors as $author) { |
|
168
|
|
|
if (is_string($author) === false) { |
|
169
|
|
|
throw new InvalidArgumentException('The array of authors must be strings.'); |
|
170
|
|
|
} |
|
171
|
|
|
$formatted .= "\n * @author " . $author; |
|
172
|
|
|
} |
|
173
|
|
|
} else { |
|
174
|
|
|
$formatted .= $authors; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $formatted; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Get the fully-qualified model class name. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $model The name of the model to get the FQN for. |
|
184
|
|
|
* |
|
185
|
|
|
* @return string |
|
186
|
|
|
* |
|
187
|
|
|
* @throws \InvalidArgumentException |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function parseModel($model) |
|
190
|
|
|
{ |
|
191
|
|
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model) === true) { |
|
192
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this->package = (strstr($model, '/', true) ?? null); |
|
196
|
|
|
|
|
197
|
|
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
|
198
|
|
|
|
|
199
|
|
|
if (Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace()) === false) { |
|
200
|
|
|
$model = $rootNamespace . 'Models\\' . $model; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $model; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the console command options. |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function getOptions() |
|
212
|
|
|
{ |
|
213
|
|
|
return [ |
|
214
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource query for the given model.'], |
|
215
|
|
|
['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null], |
|
216
|
|
|
]; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|