|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\Bundle\HookBundle\Collector; |
|
13
|
|
|
|
|
14
|
|
|
use Zikula\Bundle\HookBundle\HookProviderInterface; |
|
15
|
|
|
use Zikula\Bundle\HookBundle\HookSelfAllowedProviderInterface; |
|
16
|
|
|
use Zikula\Bundle\HookBundle\HookSubscriberInterface; |
|
17
|
|
|
|
|
18
|
|
|
class HookCollector implements HookCollectorInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var HookProviderInterface[] |
|
22
|
|
|
* e.g. [<areaName> => <serviceObject>] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $providerHooks = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
* e.g. [<moduleName> => [<areaName> => <serviceObject>, <areaName> => <serviceObject>, ...]] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $providersByOwner = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var HookSubscriberInterface[] |
|
34
|
|
|
* e.g. [<areaName> => <serviceObject>] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $subscriberHooks = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
* e.g. [<moduleName> => [<areaName> => <serviceObject>, <areaName> => <serviceObject>, ...]] |
|
41
|
|
|
*/ |
|
42
|
|
|
private $subscribersByOwner = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function addProvider($areaName, $serviceId, HookProviderInterface $service) |
|
48
|
|
|
{ |
|
49
|
|
|
if (isset($this->providerHooks[$areaName])) { |
|
50
|
|
|
throw new \InvalidArgumentException('Attempting to register a hook provider with a duplicate area name. (' . $areaName . ')'); |
|
51
|
|
|
} |
|
52
|
|
|
$service->setServiceId($serviceId); |
|
53
|
|
|
$this->providerHooks[$areaName] = $service; |
|
54
|
|
|
$this->providersByOwner[$service->getOwner()][$areaName] = $service; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getProvider($areaName) |
|
61
|
|
|
{ |
|
62
|
|
|
return isset($this->providerHooks[$areaName]) ? $this->providerHooks[$areaName] : null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function hasProvider($areaName) |
|
69
|
|
|
{ |
|
70
|
|
|
return isset($this->providerHooks[$areaName]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getProviders() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->providerHooks; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getProviderAreas() |
|
85
|
|
|
{ |
|
86
|
|
|
return array_keys($this->providerHooks); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getProviderAreasByOwner($owner) |
|
93
|
|
|
{ |
|
94
|
|
|
return isset($this->providersByOwner[$owner]) ? array_keys($this->providersByOwner[$owner]) : []; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function addSubscriber($areaName, HookSubscriberInterface $service) |
|
101
|
|
|
{ |
|
102
|
|
|
if (isset($this->subscriberHooks[$areaName])) { |
|
103
|
|
|
throw new \InvalidArgumentException('Attempting to register a hook subscriber with a duplicate area name. (' . $areaName . ')'); |
|
104
|
|
|
} |
|
105
|
|
|
$this->subscriberHooks[$areaName] = $service; |
|
106
|
|
|
$this->subscribersByOwner[$service->getOwner()][$areaName] = $service; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getSubscriber($areaName) |
|
113
|
|
|
{ |
|
114
|
|
|
return isset($this->subscriberHooks[$areaName]) ? $this->subscriberHooks[$areaName] : null; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
|
|
public function hasSubscriber($areaName) |
|
121
|
|
|
{ |
|
122
|
|
|
return isset($this->subscriberHooks[$areaName]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getSubscribers() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->subscriberHooks; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getSubscriberAreas() |
|
137
|
|
|
{ |
|
138
|
|
|
return array_keys($this->subscriberHooks); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getSubscriberAreasByOwner($owner) |
|
145
|
|
|
{ |
|
146
|
|
|
return isset($this->subscribersByOwner[$owner]) ? array_keys($this->subscribersByOwner[$owner]) : []; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function isCapable($moduleName, $type = self::HOOK_SUBSCRIBER) |
|
153
|
|
|
{ |
|
154
|
|
View Code Duplication |
if (!in_array($type, [self::HOOK_SUBSCRIBER, self::HOOK_PROVIDER, self::HOOK_SUBSCRIBE_OWN])) { |
|
|
|
|
|
|
155
|
|
|
throw new \InvalidArgumentException('Only hook_provider, hook_subscriber and subscriber_own are valid values.'); |
|
156
|
|
|
} |
|
157
|
|
|
if (self::HOOK_SUBSCRIBE_OWN == $type) { |
|
158
|
|
|
return $this->containsSelfAllowedProvider($moduleName); |
|
159
|
|
|
} |
|
160
|
|
|
$variable = substr($type, 5) . 'sByOwner'; |
|
161
|
|
|
$array = $this->$variable; |
|
162
|
|
|
|
|
163
|
|
|
return isset($array[$moduleName]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* {@inheritdoc} |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getOwnersCapableOf($type = self::HOOK_SUBSCRIBER) |
|
170
|
|
|
{ |
|
171
|
|
View Code Duplication |
if (!in_array($type, [self::HOOK_SUBSCRIBER, self::HOOK_PROVIDER])) { |
|
|
|
|
|
|
172
|
|
|
throw new \InvalidArgumentException('Only hook_provider and hook_subscriber are valid values.'); |
|
173
|
|
|
} |
|
174
|
|
|
$variable = substr($type, 5) . 'sByOwner'; |
|
175
|
|
|
$array = $this->$variable; |
|
176
|
|
|
|
|
177
|
|
|
return array_keys($array); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Does $moduleName contain at least one SelfAllowedProvider? |
|
182
|
|
|
* @param $moduleName |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
private function containsSelfAllowedProvider($moduleName) |
|
186
|
|
|
{ |
|
187
|
|
|
if (isset($this->providersByOwner[$moduleName])) { |
|
188
|
|
|
foreach ($this->providersByOwner[$moduleName] as $provider) { |
|
189
|
|
|
if ($provider instanceof HookSelfAllowedProviderInterface) { |
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return false; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.