|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebinoViewLib\Component; |
|
4
|
|
|
|
|
5
|
|
|
use WebinoAppLib\Event\AppEvent; |
|
6
|
|
|
use WebinoAppLib\Event\DispatchEvent; |
|
7
|
|
|
use WebinoAppLib\Feature\HttpListener; |
|
8
|
|
|
use WebinoConfigLib\Feature\FeatureInterface; |
|
9
|
|
|
use WebinoDomLib\Dom\Config\SpecConfigAggregateInterface; |
|
10
|
|
|
use WebinoDomLib\Event\RenderEvent; |
|
11
|
|
|
use WebinoEventLib\ListenerAggregateTrait; |
|
12
|
|
|
use WebinoViewLib\Feature\NodeView; |
|
13
|
|
|
use WebinoViewLib\Feature\ViewListener; |
|
14
|
|
|
use Zend\EventManager\ListenerAggregateInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class AbstractBaseViewComponent |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractBaseViewComponent implements |
|
20
|
|
|
FeatureInterface, |
|
21
|
|
|
ListenerAggregateInterface, |
|
22
|
|
|
SpecConfigAggregateInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use ListenerAggregateTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param NodeView $node |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract public function configure(NodeView $node); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initialize listener |
|
33
|
|
|
* @TODO concept |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function init() |
|
36
|
|
|
{ |
|
37
|
|
|
// TODO refactor |
|
38
|
|
|
|
|
39
|
|
|
$callback = function (DispatchEvent $event) { |
|
40
|
|
|
// TODO |
|
41
|
|
|
(isset($this->x) && ($this instanceof OnDispatchInterface || method_exists($this, 'onDispatchState'))) |
|
42
|
|
|
and call_user_func($this->x, $event); |
|
|
|
|
|
|
43
|
|
|
}; |
|
44
|
|
|
|
|
45
|
|
|
// TODO |
|
46
|
|
|
($this instanceof OnDispatchInterface || method_exists($this, 'onDispatchState')) |
|
47
|
|
|
and $this->listen(AppEvent::DISPATCH, $callback, AppEvent::FINISH); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
($this instanceof OnRenderComponentInterface) |
|
50
|
|
|
and $this->listen(static::class, function (RenderEvent $event) { |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
// TODO redesign |
|
53
|
|
|
|
|
54
|
|
|
if (($this instanceof OnDispatchInterface) |
|
55
|
|
|
// TODO |
|
56
|
|
|
|| method_exists($this, 'onDispatchState') |
|
57
|
|
|
) { |
|
58
|
|
|
$this->x = function (DispatchEvent $dispatchEvent) use ($event) { |
|
59
|
|
|
|
|
60
|
|
|
// TODO |
|
61
|
|
|
($this instanceof OnDispatchInterface) and $this->onDispatch($dispatchEvent); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
// TODO |
|
64
|
|
|
method_exists($this, 'onDispatchState') and $this->onDispatchState($dispatchEvent); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
// TODO |
|
67
|
|
|
method_exists($this, 'setRenderEvent') and $this->setRenderEvent($event); |
|
|
|
|
|
|
68
|
|
|
$this->onRender($event); |
|
|
|
|
|
|
69
|
|
|
}; |
|
70
|
|
|
} else { |
|
71
|
|
|
// TODO concept |
|
72
|
|
|
method_exists($this, 'setRenderEvent') and $this->setRenderEvent($event); |
|
|
|
|
|
|
73
|
|
|
$this->onRender($event); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getSpecName() |
|
82
|
|
|
{ |
|
83
|
|
|
return static::class; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return NodeView |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getSpecConfig() |
|
90
|
|
|
{ |
|
91
|
|
|
$node = new NodeView($this->getSpecName()); |
|
92
|
|
|
$this->configure($node); |
|
93
|
|
|
|
|
94
|
|
|
($this instanceof OnRenderComponentInterface) |
|
95
|
|
|
and $node->setOptions(['trigger' => [static::class => static::class]]); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
return $node; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @TODO concept |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function toArray() |
|
105
|
|
|
{ |
|
106
|
|
|
$cfg = []; |
|
107
|
|
|
|
|
108
|
|
|
// TODO |
|
109
|
|
|
if (($this instanceof OnDispatchInterface) || method_exists($this, 'onDispatchState')) { |
|
110
|
|
|
$cfg += (new HttpListener(static::class))->toArray(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (($this instanceof OnRenderComponentInterface)) { |
|
114
|
|
|
$cfg += (new ViewListener(static::class))->toArray(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $cfg; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: