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
|
|
|
* Command Data. |
13
|
|
|
* |
14
|
|
|
* @var CommandData |
15
|
|
|
*/ |
16
|
|
|
private $commandData; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Filename. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $fileName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* File Contents. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $fileContents; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Template Data. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $templateData; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param CommandData $commandData Command Data. |
43
|
|
|
*/ |
44
|
|
|
public function __construct(CommandData $commandData) |
45
|
|
|
{ |
46
|
|
|
$this->commandData = $commandData; |
47
|
|
|
$this->fileName = $commandData->config->pathGraphQL; |
48
|
|
|
$this->fileContents = file_get_contents($this->fileName); |
49
|
|
|
$this->templateData = get_artomator_template('graphql.mutations'); |
50
|
|
|
$this->templateData = fill_template($this->commandData->dynamicVars, $this->templateData); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Generate. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function generate() |
59
|
|
|
{ |
60
|
|
|
if (true === Str::contains($this->fileContents, $this->templateData)) { |
61
|
|
|
$this->commandData->commandObj->info('GraphQL Mutations '.$this->commandData->config->mHumanPlural.' already exist; Skipping'); |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (false === str::contains($this->fileContents, 'type Mutation {')) { |
67
|
|
|
$this->fileContents = preg_replace('/(type Query {)(.+?[^}])(})/is', '$1$2$3'."\n\ntype Mutation { }", $this->fileContents); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->fileContents = preg_replace('/(type Mutation {)(.+?[^}])(})/is', '$1$2'.str_replace('\\', '\\\\', $this->templateData).'$3', $this->fileContents); |
71
|
|
|
|
72
|
|
|
file_put_contents($this->fileName, $this->fileContents); |
73
|
|
|
|
74
|
|
|
$this->commandData->commandComment("\nGraphQL Mutations created"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Rollback. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function rollback() |
83
|
|
|
{ |
84
|
|
|
$strings = [ |
85
|
|
|
'create', |
86
|
|
|
'update', |
87
|
|
|
'delete', |
88
|
|
|
]; |
89
|
|
|
$model = $this->commandData->config->gHuman; |
90
|
|
|
|
91
|
|
|
foreach ($strings as $string) { |
92
|
|
|
if (true === Str::contains($this->fileContents, $string.$model)) { |
93
|
|
|
$this->fileContents = preg_replace('/(\s)+('.$string.$model.'\()(.+?)(\):.+?)(\))/is', '', $this->fileContents); |
94
|
|
|
|
95
|
|
|
file_put_contents($this->fileName, $this->fileContents); |
96
|
|
|
$this->commandData->commandComment('GraphQL '.ucfirst($string).' Mutation deleted'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|