|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PWWEB\Artomator\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use PWWEB\Artomator\Artomator; |
|
6
|
|
|
use Laracasts\Generators\Migrations\SchemaParser; |
|
7
|
|
|
use PWWEB\Artomator\Migrations\SyntaxBuilder; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
|
|
10
|
|
|
class ArtomatorQueryCommand extends Artomator |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The console command name. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name = 'artomator:query'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Create a new query class'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The type of class being generated. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $type = 'Query'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The package of class being generated. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $package = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the stub file for the generator. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getStub() |
|
46
|
|
|
{ |
|
47
|
|
|
$stub = 'query.stub'; |
|
48
|
|
|
$path = base_path() . config('artomator.stubPath'); |
|
49
|
|
|
$path = $path . $stub; |
|
50
|
|
|
|
|
51
|
|
|
if (file_exists($path) === true) { |
|
52
|
|
|
return $path; |
|
53
|
|
|
} else { |
|
54
|
|
|
return __DIR__ . '/Stubs/' . $stub; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the default namespace for the class. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $rootNamespace The class to return the namespace for. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
66
|
|
|
{ |
|
67
|
|
|
return $rootNamespace . '\GraphQL\Query'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Build the class with the given name. |
|
72
|
|
|
* |
|
73
|
|
|
* Remove the base query import if we are already in base namespace. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name The name of the Query to build. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function buildClass($name) |
|
80
|
|
|
{ |
|
81
|
|
|
$replace = []; |
|
82
|
|
|
$replace = $this->buildSchemaReplacements($replace); |
|
83
|
|
|
$replace = $this->buildModelReplacements($replace); |
|
84
|
|
|
|
|
85
|
|
|
return str_replace( |
|
86
|
|
|
array_keys($replace), |
|
87
|
|
|
array_values($replace), |
|
88
|
|
|
parent::buildClass($name) |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Build the schema replacement values. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $replace The existing replacements to append to. |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function buildSchemaReplacements(array $replace) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->option('schema') !== null) { |
|
102
|
|
|
$schema = $this->option('schema'); |
|
103
|
|
|
$schema = (new SchemaParser())->parse($schema); |
|
|
|
|
|
|
104
|
|
|
$syntax = new SyntaxBuilder(); |
|
105
|
|
|
$args = $syntax->createArgsSchema($schema); |
|
106
|
|
|
$resolves = $syntax->createResolvesSchema($schema); |
|
107
|
|
|
} else { |
|
108
|
|
|
$args = ""; |
|
109
|
|
|
$resolves = ""; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return array_merge( |
|
113
|
|
|
$replace, |
|
114
|
|
|
[ |
|
115
|
|
|
'{{schema_args}}' => $args, |
|
116
|
|
|
'{{schema_resolves}}' => $resolves, |
|
117
|
|
|
] |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get the console command options. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getOptions() |
|
127
|
|
|
{ |
|
128
|
|
|
return [ |
|
129
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource query for the given model.'], |
|
130
|
|
|
['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null], |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|