1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PWWEB\Artomator\Generators\GraphQL; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use InfyOm\Generator\Generators\BaseGenerator; |
7
|
|
|
use PWWEB\Artomator\Common\CommandData; |
8
|
|
|
|
9
|
|
|
class GraphQLInputGenerator extends BaseGenerator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var CommandData |
13
|
|
|
*/ |
14
|
|
|
private $commandData; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $fileName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $fileContents; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $templateData; |
30
|
|
|
|
31
|
|
|
public function __construct(CommandData $commandData) |
32
|
|
|
{ |
33
|
|
|
$this->commandData = $commandData; |
34
|
|
|
$this->fileName = $commandData->config->pathGraphQL; |
35
|
|
|
$this->fileContents = file_get_contents($this->fileName); |
36
|
|
|
$this->templateData = get_artomator_template('graphql.inputs'); |
37
|
|
|
$this->templateData = fill_template($this->commandData->dynamicVars, $this->templateData); |
38
|
|
|
$this->templateData = fill_template($this->generateSchema(), $this->templateData); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function generate() |
42
|
|
|
{ |
43
|
|
|
if (true === Str::contains($this->fileContents, $this->templateData)) { |
44
|
|
|
$this->commandData->commandObj->info('GraphQL Inputs '.$this->commandData->config->mHumanPlural.' already exist; Skipping'); |
45
|
|
|
|
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->fileContents .= $this->templateData; |
50
|
|
|
|
51
|
|
|
file_put_contents($this->fileName, $this->fileContents); |
52
|
|
|
|
53
|
|
|
$this->commandData->commandComment("\nGraphQL Inputs created"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function rollback() |
57
|
|
|
{ |
58
|
|
|
if (Str::contains($this->fileContents, $this->templateData)) { |
59
|
|
|
file_put_contents($this->fileName, str_replace($this->templateData, '', $this->fileContents)); |
60
|
|
|
$this->commandData->commandComment('GraphQL Inputs deleted'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function generateSchema() |
65
|
|
|
{ |
66
|
|
|
$schema = []; |
67
|
|
|
foreach ($this->commandData->fields as $field) { |
68
|
|
|
if ($field->isFillable) { |
69
|
|
|
if ('foreignId' === $field->fieldType) { |
70
|
|
|
continue; |
71
|
|
|
} else { |
72
|
|
|
$field_type = ucfirst($field->fieldType); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$field_type .= (Str::contains($field->validations, 'required') ? '!' : ''); |
76
|
|
|
|
77
|
|
|
$schema[] = $field->name.': '.$field_type; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
$schema = array_merge($schema, $this->generateRelations()); |
81
|
|
|
$schema = implode(infy_nl_tab(1, 1), $schema); |
82
|
|
|
$create_schema = str_replace('$TYPE$', 'Create', $schema); |
83
|
|
|
$update_schema = str_replace('$TYPE$', 'Update', $schema); |
84
|
|
|
$upsert_schema = str_replace('$TYPE$', 'Upsert', $schema); |
85
|
|
|
|
86
|
|
|
return [ |
87
|
|
|
'$CREATE_SCHEMA$' => $create_schema, |
88
|
|
|
'$UPDATE_SCHEMA$' => $update_schema, |
89
|
|
|
'$UPSERT_SCHEMA$' => $upsert_schema, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function generateRelations() |
94
|
|
|
{ |
95
|
|
|
$relations = []; |
96
|
|
|
|
97
|
|
|
$count = 1; |
98
|
|
|
$fieldsArr = []; |
99
|
|
|
foreach ($this->commandData->relations as $relation) { |
100
|
|
|
$field = (isset($relation->inputs[0])) ? $relation->inputs[0] : null; |
101
|
|
|
|
102
|
|
|
$relationShipText = $field; |
103
|
|
|
if (in_array($field, $fieldsArr)) { |
104
|
|
|
$relationShipText = $relationShipText.'_'.$count; |
105
|
|
|
$count++; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$relationText = $this->getRelationFunctionText($relation, $relationShipText); |
109
|
|
|
if (false === empty($relationText)) { |
110
|
|
|
$fieldsArr[] = $field; |
111
|
|
|
$relations[] = $relationText; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $relations; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function getRelationFunctionText($relationship, $relationText = null) |
119
|
|
|
{ |
120
|
|
|
extract($this->prepareRelationship($relationship, $relationText)); |
121
|
|
|
|
122
|
|
|
if (false === empty($functionName)) { |
123
|
|
|
return $this->generateRelation($functionName, $template); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return ''; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function generateRelation($functionName, $template) |
130
|
|
|
{ |
131
|
|
|
$template = str_replace('$FUNCTION_NAME$', $functionName, $template); |
132
|
|
|
$template = str_replace('$RELATION_GRAPHQL_NAME$', ucfirst(Str::singular($functionName)), $template); |
133
|
|
|
|
134
|
|
|
return $template; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function prepareRelationship($relationship, $relationText = null) |
138
|
|
|
{ |
139
|
|
|
$singularRelation = (false === empty($relationship->relationName)) ? $relationship->relationName : Str::camel(Str::singular($relationText)); |
140
|
|
|
$pluralRelation = (false === empty($relationship->relationName)) ? $relationship->relationName : Str::camel(Str::plural($relationText)); |
141
|
|
|
|
142
|
|
|
switch ($relationship->type) { |
143
|
|
|
case '1t1': |
144
|
|
|
$functionName = $singularRelation; |
145
|
|
|
$template = '$FUNCTION_NAME$: $TYPE$$RELATION_GRAPHQL_NAME$'; |
146
|
|
|
$templateFile = ''; |
147
|
|
|
break; |
148
|
|
|
case '1tm': |
149
|
|
|
$functionName = $pluralRelation; |
150
|
|
|
$template = '$FUNCTION_NAME$: $TYPE$$RELATION_GRAPHQL_NAME$HasMany'; |
151
|
|
|
$templateFile = 'hasMany'; |
152
|
|
|
break; |
153
|
|
|
case 'mt1': |
154
|
|
|
if (false === empty($relationship->relationName)) { |
155
|
|
|
$singularRelation = $relationship->relationName; |
156
|
|
|
} elseif (isset($relationship->inputs[1])) { |
157
|
|
|
$singularRelation = Str::camel(str_replace('_id', '', strtolower($relationship->inputs[1]))); |
158
|
|
|
} |
159
|
|
|
$functionName = $singularRelation; |
160
|
|
|
$template = '$FUNCTION_NAME$: $TYPE$$RELATION_GRAPHQL_NAME$BelongsTo'; |
161
|
|
|
$templateFile = 'belongsTo'; |
162
|
|
|
break; |
163
|
|
|
case 'mtm': |
164
|
|
|
$functionName = $pluralRelation; |
165
|
|
|
$template = '$FUNCTION_NAME$: $TYPE$$RELATION_GRAPHQL_NAME$BelongsToMany'; |
166
|
|
|
$templateFile = 'belongsToMany'; |
167
|
|
|
break; |
168
|
|
|
case 'hmt': |
169
|
|
|
$functionName = $pluralRelation; |
170
|
|
|
$template = ''; |
171
|
|
|
$templateFile = ''; |
172
|
|
|
break; |
173
|
|
|
default: |
174
|
|
|
$functionName = ''; |
175
|
|
|
$template = ''; |
176
|
|
|
$templateFile = ''; |
177
|
|
|
break; |
178
|
|
|
} |
179
|
|
|
if (false === empty($templateFile)) { |
180
|
|
|
$templateFile = get_artomator_template('graphql.relations.'.$templateFile); |
181
|
|
|
$templateFile = str_replace('$RELATION_GRAPHQL_NAME$', ucfirst(Str::singular($functionName)), $templateFile); |
182
|
|
|
|
183
|
|
|
if (false === Str::contains($this->fileContents, $templateFile) && false === Str::contains($this->templateData, $templateFile)) { |
184
|
|
|
$this->templateData .= $templateFile; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return compact('functionName', 'template'); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|