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 GraphQLMutationGenerator 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.mutations'); |
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 Mutations '.$this->commandData->config->mHumanPlural.' already exist; Skipping'); |
45
|
|
|
|
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->fileContents = preg_replace('/(type Mutation {)(.+?[^}])(})/is', '$1$2'.str_replace('\\', '\\\\', $this->templateData).'$3', $this->fileContents); |
50
|
|
|
|
51
|
|
|
file_put_contents($this->fileName, $this->fileContents); |
52
|
|
|
|
53
|
|
|
$this->commandData->commandComment("\nGraphQL Mutations created"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function rollback() |
57
|
|
|
{ |
58
|
|
|
$strings = [ |
59
|
|
|
'create', |
60
|
|
|
'update', |
61
|
|
|
'delete', |
62
|
|
|
]; |
63
|
|
|
$model = $this->commandData->config->mHuman; |
64
|
|
|
|
65
|
|
|
foreach ($strings as $string) { |
66
|
|
|
if (Str::contains($this->fileContents, $string.$model)) { |
67
|
|
|
$this->fileContents = preg_replace('/(\s)+('.$string.$model.'\()(.+?)(\):.+?)(\))/is', '', $this->fileContents); |
68
|
|
|
|
69
|
|
|
file_put_contents($this->fileName, $this->fileContents); |
70
|
|
|
$this->commandData->commandComment('GraphQL '.ucfirst($string).' Mutation deleted'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function generateSchema() |
76
|
|
|
{ |
77
|
|
|
$schema = []; |
78
|
|
|
foreach ($this->commandData->fields as $field) { |
79
|
|
|
if (true === in_array($field->name, ['created_at', 'updated_at', 'id'])) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
$field_type = ucfirst($field->fieldType).(Str::contains($field->validations, 'required') ? '!' : ''); |
83
|
|
|
|
84
|
|
|
$schema[] = $field->name.': '.$field_type; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return ['$SCHEMA$' => implode("\n\t\t", $schema)]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|