1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* BEdita, API-first content management framework |
6
|
|
|
* Copyright 2022 Atlas Srl, Chialab Srl |
7
|
|
|
* |
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace BEdita\Placeholders\Event; |
17
|
|
|
|
18
|
|
|
use BEdita\Core\Model\Entity\ObjectType; |
19
|
|
|
use Cake\Event\Event; |
20
|
|
|
use Cake\Event\EventListenerInterface; |
21
|
|
|
use Cake\ORM\Locator\LocatorAwareTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Modify generated JSON schema for object types to mark fields where placeholders are read from. |
25
|
|
|
*/ |
26
|
|
|
class JsonSchemaEventHandler implements EventListenerInterface |
27
|
|
|
{ |
28
|
|
|
use LocatorAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritDoc |
32
|
|
|
*/ |
33
|
|
|
public function implementedEvents(): array |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'ObjectType.getSchema' => 'onGetSchema', |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Modify object type schema by marking fields where placeholders are read from. |
42
|
|
|
* |
43
|
|
|
* @param \Cake\Event\Event $event Dispatched event. |
44
|
|
|
* @param array $schema Automatically generated JSON schema. |
45
|
|
|
* @param \BEdita\Core\Model\Entity\ObjectType $objectType Object type. |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function onGetSchema(Event $event, array $schema, ObjectType $objectType): array |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$table = $this->getTableLocator()->get($objectType->table); |
51
|
|
|
if (!$table->hasBehavior('Placeholders')) { |
52
|
|
|
return $schema; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$behavior = $table->getBehavior('Placeholders'); |
56
|
|
|
|
57
|
|
|
// Mark fields where placeholders are read from. |
58
|
|
|
$fields = $behavior->getConfig('fields', []); |
59
|
|
|
foreach ($fields as $field) { |
60
|
|
|
if (!isset($schema['properties'][$field])) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
$schema['properties'][$field]['placeholders'] = true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Mark relevant relations as read only. |
67
|
|
|
$relations = [$behavior->getConfig('relation')]; |
68
|
|
|
if ($table->hasBehavior('Placeholded')) { |
69
|
|
|
array_push($relations, ...$table->getBehavior('Placeholded')->getConfig('relations', [])); |
70
|
|
|
} |
71
|
|
|
foreach ($relations as $relName) { |
72
|
|
|
if (!isset($schema['relations'][$relName])) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
$schema['relations'][$relName]['readonly'] = true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $schema; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.