1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\LaravelDto\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The DTOs generation manifest. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class Manifest |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The manifest filename. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $filename; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The manifest content. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $manifest = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Instantiate the class. |
29
|
|
|
* |
30
|
|
|
* @param string $filename |
31
|
|
|
*/ |
32
|
30 |
|
public function __construct(string $filename) |
33
|
|
|
{ |
34
|
30 |
|
$this->filename = $filename; |
35
|
30 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Write content in the manifest |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* @param mixed $value |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
9 |
|
public function write(string $key, $value): self |
45
|
|
|
{ |
46
|
9 |
|
$this->manifest = $this->read(); |
47
|
|
|
|
48
|
9 |
|
Arr::set($this->manifest, $key, $value); |
49
|
|
|
|
50
|
9 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve the manifest content |
55
|
|
|
* |
56
|
|
|
* @param string|null $key |
57
|
|
|
* @param mixed $default |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
12 |
|
public function read(string $key = null, $default = null) |
61
|
|
|
{ |
62
|
12 |
|
if (!file_exists($this->filename)) { |
63
|
12 |
|
file_put_contents($this->filename, '<?php return [];'); |
64
|
|
|
} |
65
|
|
|
|
66
|
12 |
|
if (empty($this->manifest)) { |
67
|
12 |
|
$this->manifest = require $this->filename; |
68
|
|
|
} |
69
|
|
|
|
70
|
12 |
|
return $key === null ? $this->manifest : Arr::get($this->manifest, $key, $default); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Save the manifest |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
9 |
|
public function save(): self |
79
|
|
|
{ |
80
|
9 |
|
file_put_contents($this->filename, '<?php return ' . var_export($this->read(), true) . ';'); |
81
|
|
|
|
82
|
9 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Delete the manifest |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
12 |
|
public function delete(): void |
91
|
|
|
{ |
92
|
12 |
|
if (file_exists($this->filename)) { |
93
|
12 |
|
unlink($this->filename); |
94
|
|
|
} |
95
|
12 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add the given model DTO |
99
|
|
|
* |
100
|
|
|
* @param string $model |
101
|
|
|
* @param string $dto |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
6 |
|
public function addDto(string $model, string $dto): self |
105
|
|
|
{ |
106
|
6 |
|
$this->write("{$model}.dto", $dto); |
107
|
|
|
|
108
|
6 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieve the given model DTO |
113
|
|
|
* |
114
|
|
|
* @param string $model |
115
|
|
|
* @return string|null |
116
|
|
|
*/ |
117
|
6 |
|
public function getDto(string $model): ?string |
118
|
|
|
{ |
119
|
6 |
|
return $this->read("{$model}.dto"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add the given model use statements |
124
|
|
|
* |
125
|
|
|
* @param string $model |
126
|
|
|
* @param string $name |
127
|
|
|
* @param string $use |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
9 |
|
public function addUseStatement(string $model, string $name, string $use): self |
131
|
|
|
{ |
132
|
9 |
|
$this->write("{$model}.use.{$name}", $use); |
133
|
|
|
|
134
|
9 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Retrieve the given model use statements |
139
|
|
|
* |
140
|
|
|
* @param string $model |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
9 |
|
public function getUseStatements(string $model): array |
144
|
|
|
{ |
145
|
9 |
|
return $this->read("{$model}.use", []); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add the given DTO that is being generated |
150
|
|
|
* |
151
|
|
|
* @param string $dto |
152
|
|
|
* @return self |
153
|
|
|
*/ |
154
|
6 |
|
public function addGeneratingDto(string $dto): self |
155
|
|
|
{ |
156
|
6 |
|
$this->write("generating.{$dto}", true); |
157
|
|
|
|
158
|
6 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Retrieve the DTO that is being generated now |
163
|
|
|
* |
164
|
|
|
* @return string|null |
165
|
|
|
*/ |
166
|
9 |
|
public function getGeneratingDto(): ?string |
167
|
|
|
{ |
168
|
9 |
|
if (empty($generating = $this->read('generating'))) { |
169
|
3 |
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
6 |
|
$dtos = array_keys($generating); |
173
|
|
|
|
174
|
6 |
|
return end($dtos); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Determine whether the given DTO is being generated now |
179
|
|
|
* |
180
|
|
|
* @param string $dto |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
3 |
|
public function isGenerating(string $dto): bool |
184
|
|
|
{ |
185
|
3 |
|
return $this->getGeneratingDto() === $dto; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Determine whether the given DTO is in the process of being generated |
190
|
|
|
* |
191
|
|
|
* @param string $dto |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
6 |
|
public function generating(string $dto): bool |
195
|
|
|
{ |
196
|
6 |
|
return $this->read("generating.{$dto}") !== null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Mark the latest generating DTO as generated |
201
|
|
|
* |
202
|
|
|
* @return self |
203
|
|
|
*/ |
204
|
6 |
|
public function finishGeneratingDto(): self |
205
|
|
|
{ |
206
|
6 |
|
if (!empty($this->read('generating'))) { |
207
|
6 |
|
$dto = $this->getGeneratingDto(); |
208
|
6 |
|
$this->write("generated.{$dto}", true); |
209
|
6 |
|
array_pop($this->manifest['generating']); |
210
|
|
|
} |
211
|
|
|
|
212
|
6 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Determine whether the given DTO has been generated |
217
|
|
|
* |
218
|
|
|
* @param string $dto |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
6 |
|
public function generated(string $dto): bool |
222
|
|
|
{ |
223
|
6 |
|
return $this->read("generated.{$dto}") !== null; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Add the given class as starting DTO |
228
|
|
|
* |
229
|
|
|
* @param string $dto |
230
|
|
|
* @return self |
231
|
|
|
*/ |
232
|
6 |
|
public function addStartingDto(string $dto): self |
233
|
|
|
{ |
234
|
6 |
|
if ($this->read('starting') === null) { |
235
|
6 |
|
$this->write('starting', $dto); |
236
|
|
|
} |
237
|
|
|
|
238
|
6 |
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Retrieve the starting DTO |
243
|
|
|
* |
244
|
|
|
* @return string|null |
245
|
|
|
*/ |
246
|
6 |
|
public function getStartingDto(): ?string |
247
|
|
|
{ |
248
|
6 |
|
return $this->read('starting'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Determine whether the given class is the starting DTO |
253
|
|
|
* |
254
|
|
|
* @param string $dto |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
6 |
|
public function isStartingDto(string $dto): bool |
258
|
|
|
{ |
259
|
6 |
|
return $this->getStartingDto() === $dto; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|