|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Targets; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Inflector\InflectorFactory; |
|
6
|
|
|
use GraphQL\Type\Definition\Type; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Modelarium\Exception\Exception; |
|
9
|
|
|
use Modelarium\GeneratedCollection; |
|
10
|
|
|
use Modelarium\Parser; |
|
11
|
|
|
|
|
12
|
|
|
abstract class BaseGenerator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name = ''; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $studlyName = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $lowerName = ''; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $lowerNamePlural = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \Doctrine\Inflector\Inflector |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $inflector = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Parser |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $parser = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Type |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $type = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param Parser $parser |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @param Type|string $type |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(Parser $parser, string $name, $type = null) |
|
60
|
|
|
{ |
|
61
|
21 |
|
$this->inflector = InflectorFactory::create()->build(); |
|
62
|
|
|
|
|
63
|
21 |
|
$this->parser = $parser; |
|
64
|
|
|
$this->setName($name); |
|
65
|
21 |
|
|
|
66
|
21 |
|
if ($type instanceof Type) { |
|
67
|
21 |
|
$this->type = $type; |
|
68
|
21 |
|
} elseif (!$type) { |
|
69
|
21 |
|
$this->type = $parser->getSchema()->getType($name); |
|
70
|
|
|
} else { |
|
71
|
21 |
|
throw new Exception('Invalid model'); |
|
72
|
7 |
|
} |
|
73
|
14 |
|
} |
|
74
|
14 |
|
|
|
75
|
|
|
protected function setName(string $name): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->name = $name; |
|
78
|
21 |
|
$this->studlyName = Str::studly($this->name); |
|
79
|
|
|
$this->lowerName = mb_strtolower($this->name); |
|
80
|
|
|
$this->lowerNamePlural = $this->inflector->pluralize($this->lowerName); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function splitClassName(string $fullclass): array |
|
84
|
|
|
{ |
|
85
|
|
|
$classTokens = explode('\\', $fullclass); |
|
86
|
7 |
|
$className = array_pop($classTokens); |
|
87
|
|
|
$classNamespace = implode('\\', $classTokens); |
|
88
|
7 |
|
$relativePath = implode('/', $classTokens); |
|
89
|
7 |
|
return [$classNamespace, $className, $relativePath]; |
|
90
|
7 |
|
} |
|
91
|
|
|
|
|
92
|
7 |
|
/** |
|
93
|
|
|
* Returns the base path (where composer.json is located) |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $file The filename |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function getBasePath(string $file = null): string |
|
99
|
|
|
{ |
|
100
|
|
|
$basepath = dirname(\Composer\Factory::getComposerFile()); |
|
101
|
|
|
if ($file) { |
|
102
|
|
|
$basepath .= '/' . $file; |
|
103
|
|
|
} |
|
104
|
|
|
return $basepath; |
|
105
|
|
|
} |
|
106
|
21 |
|
|
|
107
|
|
|
abstract public function generate(): GeneratedCollection; |
|
108
|
21 |
|
|
|
109
|
21 |
|
/** |
|
110
|
|
|
* Stubs from a stub file. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $stubName The name for the stub file. |
|
113
|
|
|
* @param Callable $f |
|
114
|
|
|
* @return string |
|
115
|
|
|
* @see BaseGenerator::stubFile() |
|
116
|
|
|
* @throws \Safe\Exceptions\FilesystemException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function stubToString(string $stubName, callable $f = null): string |
|
119
|
21 |
|
{ |
|
120
|
|
|
$stub = \Safe\file_get_contents($this->stubDir . "/$stubName.stub.php"); |
|
121
|
21 |
|
return $this->replaceStub($stub, $f); |
|
122
|
21 |
|
} |
|
123
|
19 |
|
|
|
124
|
|
|
/** |
|
125
|
21 |
|
* Stubs a string. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $stub |
|
128
|
|
|
* @param callable $f |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function replaceStub(string $stub, callable $f = null): string |
|
132
|
|
|
{ |
|
133
|
|
|
$data = $this->replaceDummy($stub); |
|
134
|
21 |
|
if ($f) { |
|
135
|
|
|
$data = $f($data); |
|
136
|
21 |
|
} |
|
137
|
21 |
|
return $data; |
|
138
|
21 |
|
} |
|
139
|
21 |
|
|
|
140
|
|
|
/** |
|
141
|
21 |
|
* Replaces common strings from the stubs |
|
142
|
21 |
|
* |
|
143
|
21 |
|
* @param string $str The string data to apply replaces |
|
144
|
21 |
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
21 |
|
protected function replaceDummy(string $str) |
|
147
|
21 |
|
{ |
|
148
|
21 |
|
$str = str_replace( |
|
149
|
21 |
|
'DummyClass', |
|
150
|
|
|
$this->studlyName, |
|
151
|
21 |
|
$str |
|
152
|
21 |
|
); |
|
153
|
21 |
|
$str = str_replace( |
|
154
|
21 |
|
'DummyName', |
|
155
|
|
|
$this->name, |
|
156
|
21 |
|
$str |
|
157
|
|
|
); |
|
158
|
|
|
$str = str_replace( |
|
159
|
|
|
'dummynameplural', |
|
160
|
|
|
$this->lowerNamePlural, |
|
161
|
|
|
$str |
|
162
|
|
|
); |
|
163
|
|
|
$str = str_replace( |
|
164
|
|
|
'dummyname', |
|
165
|
|
|
$this->lowerName, |
|
166
|
|
|
$str |
|
167
|
|
|
); |
|
168
|
|
|
return $str; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Get the value of name |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getName() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->name; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the value of studlyName |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getStudlyName() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->studlyName; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|