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
|
|
|
* Undocumented function |
56
|
|
|
* |
57
|
|
|
* @param Parser $parser |
58
|
|
|
* @param string $name |
59
|
|
|
* @param Type|string $type |
60
|
|
|
*/ |
61
|
21 |
|
public function __construct(Parser $parser, string $name, $type = null) |
62
|
|
|
{ |
63
|
21 |
|
$this->inflector = InflectorFactory::create()->build(); |
64
|
|
|
|
65
|
21 |
|
$this->name = $name; |
66
|
21 |
|
$this->studlyName = Str::studly($this->name); |
67
|
21 |
|
$this->lowerName = mb_strtolower($this->name); |
68
|
21 |
|
$this->lowerNamePlural = $this->inflector->pluralize($this->lowerName); |
69
|
21 |
|
$this->parser = $parser; |
70
|
|
|
|
71
|
21 |
|
if ($type instanceof Type) { |
72
|
7 |
|
$this->type = $type; |
73
|
14 |
|
} elseif (!$type) { |
74
|
14 |
|
$this->type = $parser->getSchema()->getType($name); |
75
|
|
|
} else { |
76
|
|
|
throw new Exception('Invalid model'); |
77
|
|
|
} |
78
|
21 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the base path (where composer.json is located) |
82
|
|
|
* |
83
|
|
|
* @param string $file The filename |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
7 |
|
public static function getBasePath(string $file = null): string |
87
|
|
|
{ |
88
|
7 |
|
$basepath = dirname(\Composer\Factory::getComposerFile()); |
89
|
7 |
|
if ($file) { |
90
|
7 |
|
$basepath .= '/' . $file; |
91
|
|
|
} |
92
|
7 |
|
return $basepath; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
abstract public function generate(): GeneratedCollection; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Stubs from a stub file. |
99
|
|
|
* |
100
|
|
|
* @param string $stubName The name for the stub file. |
101
|
|
|
* @param Callable $f |
102
|
|
|
* @return string |
103
|
|
|
* @see BaseGenerator::stubFile() |
104
|
|
|
* @throws \Safe\Exceptions\FilesystemException |
105
|
|
|
*/ |
106
|
21 |
|
public function stubToString(string $stubName, callable $f = null): string |
107
|
|
|
{ |
108
|
21 |
|
$stub = \Safe\file_get_contents($this->stubDir . "/$stubName.stub.php"); |
109
|
21 |
|
return $this->replaceStub($stub, $f); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Stubs a string. |
114
|
|
|
* |
115
|
|
|
* @param string $stub |
116
|
|
|
* @param callable $f |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
21 |
|
public function replaceStub(string $stub, callable $f = null): string |
120
|
|
|
{ |
121
|
21 |
|
$data = $this->replaceDummy($stub); |
122
|
21 |
|
if ($f) { |
123
|
19 |
|
$data = $f($data); |
124
|
|
|
} |
125
|
21 |
|
return $data; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Replaces common strings from the stubs |
130
|
|
|
* |
131
|
|
|
* @param string $str The string data to apply replaces |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
21 |
|
protected function replaceDummy(string $str) |
135
|
|
|
{ |
136
|
21 |
|
$str = str_replace( |
137
|
21 |
|
'DummyClass', |
138
|
21 |
|
$this->studlyName, |
139
|
21 |
|
$str |
140
|
|
|
); |
141
|
21 |
|
$str = str_replace( |
142
|
21 |
|
'DummyName', |
143
|
21 |
|
$this->name, |
144
|
21 |
|
$str |
145
|
|
|
); |
146
|
21 |
|
$str = str_replace( |
147
|
21 |
|
'dummynameplural', |
148
|
21 |
|
$this->lowerNamePlural, |
149
|
21 |
|
$str |
150
|
|
|
); |
151
|
21 |
|
$str = str_replace( |
152
|
21 |
|
'dummyname', |
153
|
21 |
|
$this->lowerName, |
154
|
21 |
|
$str |
155
|
|
|
); |
156
|
21 |
|
return $str; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the value of name |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getName() |
165
|
|
|
{ |
166
|
|
|
return $this->name; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the value of studlyName |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getStudlyName() |
175
|
|
|
{ |
176
|
|
|
return $this->studlyName; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|