|
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\API\Controller\ObjectsController; |
|
19
|
|
|
use BEdita\Core\Model\Table\ObjectsBaseTable; |
|
20
|
|
|
use BEdita\Core\Model\Table\ObjectsTable; |
|
21
|
|
|
use Cake\Event\Event; |
|
22
|
|
|
use Cake\Event\EventListenerInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Attach placeholders behavior and component to relevant models and controllers, respectively, upon initialization. |
|
26
|
|
|
*/ |
|
27
|
|
|
class BootstrapEventHandler implements EventListenerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritDoc} |
|
31
|
|
|
* |
|
32
|
|
|
* @codeCoverageIgnore |
|
33
|
|
|
*/ |
|
34
|
|
|
public function implementedEvents(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
'Controller.initialize' => 'onControllerInitialize', |
|
38
|
|
|
'Model.initialize' => 'onModelInitialize', |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Attach placeholders component on BEdita API controllers. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \Cake\Event\Event $event Dispatched event. |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function onControllerInitialize(Event $event): void |
|
49
|
|
|
{ |
|
50
|
|
|
$controller = $event->getSubject(); |
|
51
|
|
|
if (!$controller instanceof ObjectsController) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$controller->loadComponent('BEdita/Placeholders.Placeholders'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Attach placeholders behavior on BEdita objects tables. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Cake\Event\Event $event Dispatched event. |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function onModelInitialize(Event $event): void |
|
65
|
|
|
{ |
|
66
|
|
|
$table = $event->getSubject(); |
|
67
|
|
|
if (!$table instanceof ObjectsTable && !$table instanceof ObjectsBaseTable) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$table |
|
72
|
|
|
->addBehavior('BEdita/Placeholders.Placeholders') |
|
73
|
|
|
->addBehavior('BEdita/Placeholders.Placeholded'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|