1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DecoratorsManager.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:Widgets! |
9
|
|
|
* @subpackage Managers |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 16.06.15 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Widgets\Managers; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
|
21
|
|
|
use IPub\Widgets\Decorators; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Registered widgets decorators manager |
25
|
|
|
* |
26
|
|
|
* @package iPublikuj:Widgets! |
27
|
|
|
* @subpackage Managers |
28
|
|
|
* |
29
|
|
|
* @author Adam Kadlec <[email protected]> |
30
|
|
|
*/ |
31
|
1 |
|
final class DecoratorsManager implements \ArrayAccess, \IteratorAggregate |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Implement nette smart magic |
35
|
|
|
*/ |
36
|
1 |
|
use Nette\SmartObject; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Decorators\IFactory[] |
40
|
|
|
*/ |
41
|
|
|
protected $decorators = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if a decorator is registered |
45
|
|
|
* |
46
|
|
|
* @param string $name |
47
|
|
|
* |
48
|
|
|
* @return boolean |
49
|
|
|
*/ |
50
|
|
|
public function has(string $name) : bool |
51
|
|
|
{ |
52
|
|
|
return isset($this->decorators[$name]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns a registered decorator class |
57
|
|
|
* |
58
|
|
|
* @param string $name |
59
|
|
|
* |
60
|
|
|
* @return Decorators\IFactory|NULL |
61
|
|
|
*/ |
62
|
|
|
public function get(string $name) : ?Decorators\IFactory |
63
|
|
|
{ |
64
|
|
|
return $this->has($name) ? $this->decorators[$name] : NULL; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Register a decorator component factory |
69
|
|
|
* |
70
|
|
|
* @param Decorators\IFactory $decorator |
71
|
|
|
* @param string $name |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function register(Decorators\IFactory $decorator, string $name) : void |
76
|
|
|
{ |
77
|
1 |
|
$this->unregister($name); |
78
|
|
|
|
79
|
1 |
|
$this->decorators[$name] = $decorator; |
80
|
|
|
|
81
|
1 |
|
krsort($this->decorators); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Un-registers a decorator |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function unregister(string $name) : void |
92
|
|
|
{ |
93
|
1 |
|
if (array_key_exists($name, $this->decorators)) { |
94
|
|
|
unset($this->decorators[$name]); |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function getIterator() |
102
|
|
|
{ |
103
|
|
|
return new \ArrayIterator($this->decorators); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Implements the \ArrayAccess |
108
|
|
|
* |
109
|
|
|
* @param string $offset |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function offsetExists($offset) |
114
|
|
|
{ |
115
|
|
|
return isset($this->decorators[$offset]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Implements the \ArrayAccess |
120
|
|
|
* |
121
|
|
|
* @param string $offset |
122
|
|
|
* |
123
|
|
|
* @return Decorators\IFactory |
124
|
|
|
*/ |
125
|
|
|
public function offsetGet($offset) |
126
|
|
|
{ |
127
|
|
|
return $this->decorators[$offset]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Implements the \ArrayAccess |
132
|
|
|
* |
133
|
|
|
* @param string $offset |
134
|
|
|
* @param Decorators\IFactory|NULL $value |
135
|
|
|
*/ |
136
|
|
|
public function offsetSet($offset, $value) |
137
|
|
|
{ |
138
|
|
|
$this->decorators[$offset] = $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Implements the \ArrayAccess |
143
|
|
|
* |
144
|
|
|
* @param string $offset |
145
|
|
|
*/ |
146
|
|
|
public function offsetUnset($offset) |
147
|
|
|
{ |
148
|
|
|
unset($this->decorators[$offset]); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|