-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlueprintModel.php
More file actions
424 lines (353 loc) · 9.83 KB
/
BlueprintModel.php
File metadata and controls
424 lines (353 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
namespace SyncEngine\Model;
use Symfony\Component\HttpFoundation\File\File;
use SyncEngine\Exception\InvalidParameterException;
use SyncEngine\Model\Abstract\AbstractModel;
use SyncEngine\Model\Abstract\ServiceModel;
use SyncEngine\Model\Interface\Configurable;
use SyncEngine\Model\Interface\Supervisable;
use SyncEngine\Model\Trait\Config;
use SyncEngine\Service\ModelImporter;
use SyncEngine\Service\ModelNormalizer;
use SyncEngine\Service\Locator\Blueprints;
use SyncEngine\Service\Tag\Cleaner\DiscardList;
use SyncEngine\Service\Tag\TagParser;
use SyncEngine\Structure\Data\ConfigData;
class BlueprintModel extends ServiceModel implements Configurable
{
use Config {
setConfig as private _setConfig;
getConfig as private _getConfig;
getConfigDependencies as private _getConfigDependencies;
}
const SERVICE = Blueprints::class;
private Supervisable $supervisable;
private File $file;
/**
* The version of this blueprint.
*
* @var string
*/
protected string $version;
/**
* The author of this blueprint.
*
* @var string
*/
protected string $author = '';
/**
* The type of blueprint, can be used for categorizing.
*
* @var string
*/
protected string $type = '';
/**
* The entity that this blueprint is made for.
*
* @var string
*/
protected string $entity;
/**
* Icon used in the interface.
*
* @var string
*/
protected string $icon = '';
/**
* Human-readable name used in the interface.
*
* @var string
*/
protected string $name;
/**
* Human-readable description used in the interface.
*
* @var string
*/
protected string $description = '';
/**
* Fields to configure blueprint.
*
* @var array
*/
protected array $fields = [];
/**
* Template to setup blueprint.
*
* @var array
*/
protected array $template;
public function __construct( array|string $blueprint = [] )
{
parent::__construct();
if ( $blueprint ) {
$this->version = $blueprint['version'] ?? '';
$this->author = $blueprint['author'] ?? '';
$this->type = $blueprint['type'] ?? '';
$this->entity = $blueprint['entity'] ?? '';
$this->icon = $blueprint['icon'] ?? '';
$this->name = $blueprint['name'] ?? '';
$this->description = $blueprint['description'] ?? '';
$this->fields = $blueprint['fields'] ?? [];
$this->template = $blueprint['template'] ?? [];
}
$this->init();
if ( ! $this->version || ! $this->name || ! $this->entity || ! $this->template ) {
throw new InvalidParameterException( 'Missing required Blueprint properties' );
}
}
/**
* Initialize the blueprint.
* Prevents the need to overwrite the constructor.
*/
protected function init(): void {}
/**
* @todo Better name?
*/
public function beforeUpdate(): void {}
public function parseConfig( $config ): array
{
return $config;
}
/**
* Update the entity managed by this blueprint.
*/
final public function update( $import = false ): void
{
$this->beforeUpdate();
$model = $this->getSupervisable();
$model->setSupervisor( $this );
if ( $import ) {
$this->importDependencies();
}
// Recreate dependency array from config.
if ( $model instanceof Configurable ) {
if ( method_exists( $model, 'fetchConfigDependencies' ) ) {
$model->fetchConfigDependencies();
}
// @todo Use interfaces?
if ( method_exists( $model, 'updateConfig' ) ) {
// Remove actual config and enforce blueprint class before storing in DB.
$model->updateConfig(
$this->clearConfig(
array_replace_recursive( $model->getConfig(), static::initBlueprintConfig() )
)
);
}
}
}
final public function importDependencies(): void
{
$template = $this->getParsedTemplate();
array_shift( $template );
if ( $template ) {
/** @var ModelImporter $importer */
foreach ( $template as $key => $value ) {
// Do not override existing config or data.
// @todo Improve importer to only import if non-existend yet but do recurse into dependencies.
unset( $value['config'] );
unset( $value['data'] );
$template[ $key ] = $value;
}
$this->getContainer()->get( ModelImporter::class )->import( $template );
}
}
public function getConfigDependencies( bool|array $recursive = false ): array {
/** @var ModelNormalizer $normalizer */
$normalizer = $this->getContainer()->get( ModelNormalizer::class );
$dependencies = $normalizer->getConfigDependencies(
$this->getBlueprintConfig(),
$this->getConfigFields(),
$recursive
);
foreach ( $this->getTemplate() as $ref => $template ) {
if ( empty( $template['_entity'] ) ) {
continue;
}
$dependencies = $normalizer->getEntityDependency( $template['_entity'], $ref, $dependencies );
}
return $dependencies;
}
/**
* Remove config except tagged as private (_)
*/
final public function clearConfig( $config )
{
foreach ( $config as $key => $value ) {
// Remove everything except private config.
if ( ! str_starts_with( $key, '_' ) ) {
unset( $config[ $key ] );
}
}
$blueprintConfig = $config['_blueprint'] ?? [];
if ( $blueprintConfig ) {
$config[ '_blueprint' ] = ConfigData::create( $blueprintConfig )->sanitize( $this->getConfigFields() );
}
return $config;
}
final public function getBlueprintConfig(): array
{
return $this->getSupervisable()->getConfig( '_blueprint' ) ?? static::initBlueprintConfig()['_blueprint'] ?? [ '_class' => static::_getClassLocator() ];
}
final public static function initBlueprintConfig(): array
{
return [ '_blueprint' => [ '_class' => static::_getClassLocator() ] ];
}
final public function setSupervisableConfig(): void
{
$supervisable = $this->getSupervisable();
$supervisable->setConfig(
array_merge(
$this->clearConfig( $supervisable->getConfig() ),
$this->getConfig()
)
);
}
/**
* This returns the parsed configuration based on the entity blueprint config and blueprint template.
*/
final public function getConfig( $key = null, $default = null ): mixed
{
if ( empty( $this->config ) ) {
$this->setConfig();
}
return $this->_getConfig( $key, $default );
}
/**
* The blueprint config is the actual parsed configuration based on the entity blueprint config and blueprint template.
*/
final public function setConfig(): void
{
$config = $this->getParsedTemplate( '_supervisable', 'config' );
$config = $this->parseConfig( $config );
$this->_setConfig( $config );
}
final public function getParsedTemplate( ?string $ref = null, ?string $property = null ): array
{
$config = $this->getBlueprintConfig();
$template = $this->getTemplate( $ref, $property );
if ( empty( $template ) ) {
// @todo Error.
return [];
}
return ( new TagParser( [ 'blueprint' => $config ], recurse: false ) )
->setCleanMode( new DiscardList( [ 'blueprint' ] ) )
->parseArray( $template );
}
final public function getVersion(): string
{
return $this->version;
}
final public function getFields(): array
{
return $this->fields;
}
final public function getTemplate( ?string $ref = null, ?string $property = null ): array
{
$template = $this->template;
if ( $ref ) {
if ( '_supervisable' === $ref ) {
// The first template item is the actual entity for this blueprint.
$template = reset( $template );
} else {
$template = $template[ $ref ] ?? [];
}
if ( $property ) {
$template = $template[ $property ] ?? [];
}
}
return $template;
}
final public function getType(): string
{
return $this->type;
}
final public function getEntity(): string
{
// @todo Parse entity name?
return ucfirst( $this->entity );
}
final public function getIcon(): string
{
return $this->icon ?: ( $this->isFromModule() ? $this->getModule()->getIcon() : '' );
}
final public function getName(): string
{
return $this->name;
}
final public function getDescription(): string
{
return $this->description;
}
final public function getAuthor(): string
{
return $this->author;
}
final public function setSupervisable( Supervisable $supervisable ): void
{
if ( ! $supervisable instanceof AbstractModel ) {
throw new InvalidParameterException( 'Incorrect Model' );
}
if ( $supervisable::getModelName() !== $this->getEntity() ) {
throw new InvalidParameterException( 'Incorrect Model Entity' );
}
$this->supervisable = $supervisable;
}
final public function getSupervisable(): ?Supervisable
{
return $this->supervisable ?? null;
}
final public function isFile(): bool
{
return ! empty( $this->file );
}
final public function setFile( File $file ): void
{
$this->file = $file;
}
final public function getFile(): ?File
{
return $this->file ?? null;
}
final public function normalize(): array
{
$props = [
'_class' => $this->getClassLocator(),
'type' => $this->getType(),
'entity' => $this->getEntity(),
'version' => $this->getVersion(),
'name' => $this->getName(),
'description' => $this->getDescription(),
'fields' => $this->getFields(),
'author' => $this->getAuthor(),
'icon' => $this->getIcon(),
];
if ( $this->isFromModule() ) {
$props['module'] = $this->getModule()->getName();
}
return $props;
}
/**
* Returns the base config for this blueprint to be used as dependencies in other blueprint templates.
*
* @param array $config
*
* @return array{ supervisor: string, config: array{ _blueprint: array{ _class: string } } }
*/
public static function getDependencyTemplate( array $config = [] ): array
{
return [
'supervisor' => static::getSupervisorRef(),
'config' => static::initBlueprintConfig(),
...$config,
];
}
final public static function getSupervisorRef(): ?string
{
return self::getModelName() . ':' . static::_getClassLocator();
}
final public function getClassLocator(): string
{
return ( $this->isFile() ) ? $this->getFile()->getFilename() : parent::getClassLocator();
}
}