1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Modules\Cli\Tasks\Traits; |
13
|
|
|
|
14
|
|
|
use Phalcon\Db\Adapter\AdapterInterface; |
15
|
|
|
use Phalcon\Db\Column; |
16
|
|
|
use Phalcon\Db\ColumnInterface; |
17
|
|
|
use Zemit\Cli\Dispatcher; |
18
|
|
|
use Zemit\Support\Helper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait DescribesTrait |
22
|
|
|
* |
23
|
|
|
* This trait provides methods to describe columns, references, and indexes of a database table. |
24
|
|
|
* |
25
|
|
|
* @property Dispatcher $dispatcher |
26
|
|
|
*/ |
27
|
|
|
trait ScaffoldTrait |
28
|
|
|
{ |
29
|
|
|
// Paths & directories |
30
|
|
|
protected ?string $namespace = null; |
31
|
|
|
|
32
|
|
|
protected string $directory = './'; |
33
|
|
|
protected string $srcDirectory = 'src/'; |
34
|
|
|
protected string $testsDirectory = 'tests/'; |
35
|
|
|
|
36
|
|
|
protected string $enumsDirectory = 'Enums/'; |
37
|
|
|
protected string $modelsDirectory = 'Models/'; |
38
|
|
|
protected string $abstractsDirectory = 'Abstracts/'; |
39
|
|
|
protected string $interfacesDirectory = 'Interfaces/'; |
40
|
|
|
protected string $controllersDirectory = 'Controllers/'; |
41
|
|
|
|
42
|
|
|
protected ?array $whitelistedTables = null; |
43
|
|
|
protected ?array $excludedTables = null; |
44
|
|
|
|
45
|
|
|
public string $licenseStamp = <<<PHP |
46
|
|
|
/** |
47
|
|
|
* This file is part of the Zemit Framework. |
48
|
|
|
* |
49
|
|
|
* (c) Zemit Team <[email protected]> |
50
|
|
|
* |
51
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
52
|
|
|
* file that was distributed with this source code. |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
PHP; |
56
|
|
|
|
57
|
|
|
public string $strictTypes = <<<PHP |
58
|
|
|
declare(strict_types=1); |
59
|
|
|
|
60
|
|
|
PHP; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieves the license stamp. |
64
|
|
|
* @return string|null The license stamp, or null if there is no license. |
65
|
|
|
*/ |
66
|
|
|
public function getLicenseStamp(): ?string |
67
|
|
|
{ |
68
|
|
|
return $this->isNoLicense()? '' : $this->dispatcher->getParam('license') ?? $this->licenseStamp; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieves the value of the 'strictTypes' property. |
73
|
|
|
* |
74
|
|
|
* @return string|null The value of the 'strictTypes' property, or null if the 'no-strict-types' parameter is set. |
75
|
|
|
*/ |
76
|
|
|
public function getStrictTypes(): ?string |
77
|
|
|
{ |
78
|
|
|
return $this->isNoStrictTypes()? '' : $this->strictTypes; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks if the given table is whitelisted. |
83
|
|
|
* @param string $table The table name to check. |
84
|
|
|
* @return bool Returns true if the table is whitelisted, false otherwise. |
85
|
|
|
*/ |
86
|
|
|
public function isWhitelistedTable(string $table): bool |
87
|
|
|
{ |
88
|
|
|
if (!isset($this->whitelistedTables)) { |
89
|
|
|
$this->whitelistedTables = array_filter(explode(',', $this->dispatcher->getParam('table') ?? '')); |
90
|
|
|
} |
91
|
|
|
return empty($this->whitelistedTables) || in_array($table, $this->whitelistedTables); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Determines if a table is excluded. |
96
|
|
|
* @param string $table The name of the table to check. |
97
|
|
|
* @return bool Returns true if the table is excluded, false otherwise. |
98
|
|
|
*/ |
99
|
|
|
public function isExcludedTable(string $table): bool |
100
|
|
|
{ |
101
|
|
|
if (!isset($this->excludedTables)) { |
102
|
|
|
$this->excludedTables = array_filter(explode(',', $this->dispatcher->getParam('exclude') ?? '')); |
103
|
|
|
} |
104
|
|
|
return !empty($this->excludedTables) && in_array($table, $this->excludedTables); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Method for --no-controllers |
108
|
|
|
public function isNoControllers(): bool |
109
|
|
|
{ |
110
|
|
|
return $this->dispatcher->getParameter('noControllers'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Method for --no-interfaces |
114
|
|
|
public function isNoInterfaces(): bool |
115
|
|
|
{ |
116
|
|
|
return $this->dispatcher->getParameter('noInterfaces'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Method for --no-abstracts |
120
|
|
|
public function isNoAbstracts(): bool |
121
|
|
|
{ |
122
|
|
|
return $this->dispatcher->getParameter('noAbstracts'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Method for --no-models |
126
|
|
|
public function isNoModels(): bool |
127
|
|
|
{ |
128
|
|
|
return $this->dispatcher->getParameter('noModels'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Method for --no-enums |
132
|
|
|
public function isNoEnums(): bool |
133
|
|
|
{ |
134
|
|
|
return $this->dispatcher->getParameter('noEnums'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Method for --no-strict-types |
138
|
|
|
public function isNoStrictTypes(): bool |
139
|
|
|
{ |
140
|
|
|
return $this->dispatcher->getParameter('noStrictTypes'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Method for --no-license |
144
|
|
|
public function isNoLicense(): bool |
145
|
|
|
{ |
146
|
|
|
return $this->dispatcher->getParameter('noLicense'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Method for --no-comments |
150
|
|
|
public function isNoComments(): bool |
151
|
|
|
{ |
152
|
|
|
return $this->dispatcher->getParameter('noComments'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Method for --no-get-set-methods |
156
|
|
|
public function isNoGetSetMethods(): bool |
157
|
|
|
{ |
158
|
|
|
return $this->dispatcher->getParameter('noGetSetMethods'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// Method for --no-validations |
162
|
|
|
public function isNoValidations(): bool |
163
|
|
|
{ |
164
|
|
|
return $this->dispatcher->getParameter('noValidations'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Method for --no-relationships |
168
|
|
|
public function isNoRelationships(): bool |
169
|
|
|
{ |
170
|
|
|
return $this->dispatcher->getParameter('noRelationships'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Method for --no-column-map |
174
|
|
|
public function isNoColumnMap(): bool |
175
|
|
|
{ |
176
|
|
|
return $this->dispatcher->getParameter('noColumnMap'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Method for --no-set-source |
180
|
|
|
public function isNoSetSource(): bool |
181
|
|
|
{ |
182
|
|
|
return $this->dispatcher->getParameter('noSetSource'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// Method for --no-typings |
186
|
|
|
public function isNoTypings(): bool |
187
|
|
|
{ |
188
|
|
|
return $this->dispatcher->getParameter('noTypings'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Method for --granular-typings |
192
|
|
|
public function isGranularTypings(): bool |
193
|
|
|
{ |
194
|
|
|
return $this->dispatcher->getParameter('granularTypings'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Method for --add-raw-value-type |
198
|
|
|
public function isAddRawValueType(): bool |
199
|
|
|
{ |
200
|
|
|
return $this->dispatcher->getParameter('addRawValueType'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// Method for --protected-properties |
204
|
|
|
public function isProtectedProperties(): bool |
205
|
|
|
{ |
206
|
|
|
return $this->dispatcher->getParameter('protectedProperties'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Determines if a given path is an absolute path. |
211
|
|
|
* @param string $path The path to be checked. (default: null) |
212
|
|
|
* @return bool Returns true if the path is an absolute path, false otherwise. |
213
|
|
|
*/ |
214
|
|
|
public function isAbsolutePath(string $path = ''): bool |
215
|
|
|
{ |
216
|
|
|
return str_starts_with($path, '/'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Retrieves the absolute file or directory path. |
221
|
|
|
* |
222
|
|
|
* @param string $path The relative or absolute path to the file or directory. |
223
|
|
|
* @param string $fullPath The full path including directory for the file or directory. |
224
|
|
|
* |
225
|
|
|
* @return string The absolute file or directory path. If the given path is absolute, it will be returned as is. |
226
|
|
|
* Otherwise, the full path including directory will be returned. |
227
|
|
|
*/ |
228
|
|
|
public function absolutePathOr(string $path = '', string $fullPath = ''): string |
229
|
|
|
{ |
230
|
|
|
return $this->isAbsolutePath($path)? $path : $fullPath; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Retrieves the directory path for a given file or directory path. |
235
|
|
|
* |
236
|
|
|
* @param string $path The relative or absolute path to the file or directory. |
237
|
|
|
* |
238
|
|
|
* @return string The absolute directory path for the given file or directory path. |
239
|
|
|
*/ |
240
|
|
|
public function getDirectory(string $path = ''): string |
241
|
|
|
{ |
242
|
|
|
$fullPath = ($this->dispatcher->getParam('directory') ?? $this->directory) . '/' . $path; |
243
|
|
|
return $this->absolutePathOr($path, $fullPath); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function getSrcDirectory(string $path = ''): string |
247
|
|
|
{ |
248
|
|
|
$fullPath = $this->getDirectory($this->dispatcher->getParam('srcDir') ?? $this->srcDirectory) . $path; |
249
|
|
|
return $this->absolutePathOr($path, $fullPath); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// Tests Directory |
253
|
|
|
public function getTestsDirectory(string $path = ''): string |
254
|
|
|
{ |
255
|
|
|
$fullPath = $this->getDirectory($this->dispatcher->getParam('testsDir') ?? $this->testsDirectory) . $path; |
256
|
|
|
return $this->absolutePathOr($path, $fullPath); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// Controllers Directory |
260
|
|
|
public function getControllersDirectory(string $path = ''): string |
261
|
|
|
{ |
262
|
|
|
$fullPath = $this->getSrcDirectory($this->dispatcher->getParam('controllersDir') ?? $this->controllersDirectory) . $path; |
263
|
|
|
return $this->absolutePathOr($path, $fullPath); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
// Models Directory |
267
|
|
|
public function getModelsDirectory(string $path = ''): string |
268
|
|
|
{ |
269
|
|
|
$fullPath = $this->getSrcDirectory($this->dispatcher->getParam('modelsDir') ?? $this->modelsDirectory) . $path; |
270
|
|
|
return $this->absolutePathOr($path, $fullPath); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
// Models Interfaces Directory |
274
|
|
|
public function getModelsInterfacesDirectory(string $path = ''): string |
275
|
|
|
{ |
276
|
|
|
$fullPath = $this->getModelsDirectory($this->dispatcher->getParam('interfacesDir') ?? $this->interfacesDirectory) . $path; |
277
|
|
|
return $this->absolutePathOr($path, $fullPath); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// Models Enum Directory |
281
|
|
|
public function getEnumsDirectory(string $path = ''): string |
282
|
|
|
{ |
283
|
|
|
$fullPath = $this->getModelsDirectory($this->dispatcher->getParam('enumsDir') ?? $this->enumsDirectory) . $path; |
284
|
|
|
return $this->absolutePathOr($path, $fullPath); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
// Models Abstracts Directory |
288
|
|
|
public function getAbstractsDirectory(string $path = ''): string |
289
|
|
|
{ |
290
|
|
|
$fullPath = $this->getModelsDirectory($this->dispatcher->getParam('abstractsDir') ?? $this->abstractsDirectory) . $path; |
291
|
|
|
return $this->absolutePathOr($path, $fullPath); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Models Abstracts Interfaces Directory |
295
|
|
|
public function getAbstractsInterfacesDirectory(string $path = ''): string |
296
|
|
|
{ |
297
|
|
|
$fullPath = $this->getAbstractsDirectory($this->dispatcher->getParam('interfaceDir') ?? $this->interfacesDirectory) . $path; |
298
|
|
|
return $this->absolutePathOr($path, $fullPath); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
// Models Tests Directory |
302
|
|
|
public function getModelsTestsDirectory(string $path = ''): string |
303
|
|
|
{ |
304
|
|
|
$fullPath = $this->getTestsDirectory($this->dispatcher->getParam('modelsDir') ?? $this->modelsDirectory) . $path; |
305
|
|
|
return $this->absolutePathOr($path, $fullPath); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Converts a file system path to a PHP namespace. |
310
|
|
|
* @param string $path The file system path to be converted. |
311
|
|
|
* @return string The converted PHP namespace. |
312
|
|
|
*/ |
313
|
|
|
public function getNamespaceFromPath(string $path): string |
314
|
|
|
{ |
315
|
|
|
$baseNamespace = ($this->dispatcher->getParam('namespace') ?? $this->namespace); |
316
|
|
|
$namespace = $baseNamespace . '\\' . |
317
|
|
|
Helper::camelize( |
318
|
|
|
Helper::uncamelize( |
319
|
|
|
str_replace( |
320
|
|
|
'/', |
321
|
|
|
'\\', |
322
|
|
|
ltrim($path, isset($baseNamespace)? $this->getSrcDirectory() : '') |
323
|
|
|
) |
324
|
|
|
) |
325
|
|
|
); |
326
|
|
|
return trim(preg_replace('/\\\\+/', '\\', $namespace), '\\'); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Default namespace |
330
|
|
|
public function getNamespace(): string |
331
|
|
|
{ |
332
|
|
|
return $this->getNamespaceFromPath($this->getDirectory()); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// Controllers Namespace |
336
|
|
|
public function getControllersNamespace(): string |
337
|
|
|
{ |
338
|
|
|
return $this->getNamespaceFromPath($this->getControllersDirectory()); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function getEnumsNamespace(): string |
342
|
|
|
{ |
343
|
|
|
return $this->getNamespaceFromPath($this->getEnumsDirectory()); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
// Models Namespace |
347
|
|
|
public function getModelsNamespace(): string |
348
|
|
|
{ |
349
|
|
|
return $this->getNamespaceFromPath($this->getModelsDirectory()); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
// Abstract Namespace |
353
|
|
|
public function getAbstractsNamespace(): string |
354
|
|
|
{ |
355
|
|
|
return $this->getNamespaceFromPath($this->getAbstractsDirectory()); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// Models Interfaces Namespace |
359
|
|
|
public function getModelsInterfacesNamespace(): string |
360
|
|
|
{ |
361
|
|
|
return $this->getNamespaceFromPath($this->getModelsInterfacesDirectory()); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// Models Abstracts Interfaces Namespace |
365
|
|
|
public function getAbstractsInterfacesNamespace(): string |
366
|
|
|
{ |
367
|
|
|
return $this->getNamespaceFromPath($this->getAbstractsInterfacesDirectory()); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
// Models Tests Namespace |
371
|
|
|
public function getModelsTestsNamespace(): string |
372
|
|
|
{ |
373
|
|
|
return $this->getNamespaceFromPath($this->getModelsTestsDirectory()); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|