1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/manager package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
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 Puli\Manager\Api\Module; |
13
|
|
|
|
14
|
|
|
use Puli\Manager\Api\Config\Config; |
15
|
|
|
use Puli\Manager\Api\InvalidConfigException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The module file of the root module. |
19
|
|
|
* |
20
|
|
|
* You can pass a base configuration to the constructor that the module's |
21
|
|
|
* configuration will inherit. |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class RootModuleFile extends ModuleFile |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Config |
31
|
|
|
*/ |
32
|
|
|
private $config; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private $moduleOrder = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var InstallInfo[] |
41
|
|
|
*/ |
42
|
|
|
private $installInfos = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string[] |
46
|
|
|
*/ |
47
|
|
|
private $pluginClasses = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates a new root module file. |
51
|
|
|
* |
52
|
|
|
* The file's configuration will inherit its settings from the base |
53
|
|
|
* configuration passed to the constructor. |
54
|
|
|
* |
55
|
|
|
* @param string|null $moduleName The module name. Optional. |
56
|
|
|
* @param string|null $path The path where the configuration is |
57
|
|
|
* stored or `null` if this configuration is |
58
|
|
|
* not stored on the file system. |
59
|
|
|
* @param Config|null $baseConfig The configuration that the module will |
60
|
|
|
* inherit its configuration values from. |
61
|
|
|
*/ |
62
|
498 |
|
public function __construct($moduleName = null, $path = null, Config $baseConfig = null) |
63
|
|
|
{ |
64
|
498 |
|
parent::__construct($moduleName, $path); |
65
|
|
|
|
66
|
498 |
|
$this->config = new Config($baseConfig); |
67
|
498 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the configuration of the module. |
71
|
|
|
* |
72
|
|
|
* @return Config The configuration. |
73
|
|
|
*/ |
74
|
340 |
|
public function getConfig() |
75
|
|
|
{ |
76
|
340 |
|
return $this->config; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the order in which modules should be loaded. |
81
|
|
|
* |
82
|
|
|
* If modules contain path mappings for the same resource paths, this |
83
|
|
|
* setting can be used to specify in which order these modules should be |
84
|
|
|
* loaded. Alternatively, you can use {@link setOverriddenModules()} to |
85
|
|
|
* mark one of the modules to override the other one. |
86
|
|
|
* |
87
|
|
|
* @return string[] A list of module names. |
88
|
|
|
*/ |
89
|
81 |
|
public function getModuleOrder() |
90
|
|
|
{ |
91
|
81 |
|
return $this->moduleOrder; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets the order in which modules should be loaded. |
96
|
|
|
* |
97
|
|
|
* If modules contain path mappings for the same resource paths, this |
98
|
|
|
* setting can be used to specify in which order these modules should be |
99
|
|
|
* loaded. Alternatively, you can use {@link setOverriddenModules()} to |
100
|
|
|
* mark one of the modules to override the other one. |
101
|
|
|
* |
102
|
|
|
* @param string[] $moduleOrder A list of module names. |
103
|
|
|
*/ |
104
|
4 |
|
public function setModuleOrder(array $moduleOrder) |
105
|
|
|
{ |
106
|
4 |
|
$this->moduleOrder = $moduleOrder; |
107
|
4 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets the install infos of all installed modules. |
111
|
|
|
* |
112
|
|
|
* @param InstallInfo[] The install infos. |
113
|
|
|
*/ |
114
|
2 |
|
public function setInstallInfos(array $installInfos) |
115
|
|
|
{ |
116
|
2 |
|
$this->installInfos = array(); |
117
|
|
|
|
118
|
2 |
|
foreach ($installInfos as $installInfo) { |
119
|
2 |
|
$this->addInstallInfo($installInfo); |
120
|
|
|
} |
121
|
2 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Adds install info for an installed module. |
125
|
|
|
* |
126
|
|
|
* @param InstallInfo $installInfo The install info. |
127
|
|
|
*/ |
128
|
172 |
|
public function addInstallInfo(InstallInfo $installInfo) |
129
|
|
|
{ |
130
|
172 |
|
$this->installInfos[$installInfo->getModuleName()] = $installInfo; |
131
|
172 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Removes the install info of an installed module. |
135
|
|
|
* |
136
|
|
|
* @param string $moduleName The module name. |
137
|
|
|
*/ |
138
|
10 |
|
public function removeInstallInfo($moduleName) |
139
|
|
|
{ |
140
|
10 |
|
unset($this->installInfos[$moduleName]); |
141
|
10 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Removes all install infos. |
145
|
|
|
*/ |
146
|
1 |
|
public function clearInstallInfos() |
147
|
|
|
{ |
148
|
1 |
|
$this->installInfos = array(); |
149
|
1 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the install info of an installed module. |
153
|
|
|
* |
154
|
|
|
* @param string $moduleName The module name. |
155
|
|
|
* |
156
|
|
|
* @return InstallInfo The install info. |
157
|
|
|
* |
158
|
|
|
* @throws NoSuchModuleException If no module is installed with that name. |
159
|
|
|
*/ |
160
|
9 |
|
public function getInstallInfo($moduleName) |
161
|
|
|
{ |
162
|
9 |
|
if (!isset($this->installInfos[$moduleName])) { |
163
|
1 |
|
throw new NoSuchModuleException(sprintf( |
164
|
1 |
|
'Could not get install info: The module "%s" is not installed.', |
165
|
|
|
$moduleName |
166
|
|
|
)); |
167
|
|
|
} |
168
|
|
|
|
169
|
8 |
|
return $this->installInfos[$moduleName]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the install infos of all installed modules. |
174
|
|
|
* |
175
|
|
|
* @return InstallInfo[] The install infos. |
176
|
|
|
*/ |
177
|
66 |
|
public function getInstallInfos() |
178
|
|
|
{ |
179
|
|
|
// The module names as array keys are for internal use only |
180
|
66 |
|
return array_values($this->installInfos); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns whether an install info with a given name exists. |
185
|
|
|
* |
186
|
|
|
* @param string $moduleName The name of the module. |
187
|
|
|
* |
188
|
|
|
* @return bool Whether install info with that name exists. |
189
|
|
|
*/ |
190
|
10 |
|
public function hasInstallInfo($moduleName) |
191
|
|
|
{ |
192
|
10 |
|
return isset($this->installInfos[$moduleName]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns whether the module file contains any install infos. |
197
|
|
|
* |
198
|
|
|
* @return bool Returns `true` if the file contains install infos and |
199
|
|
|
* `false` otherwise. |
200
|
|
|
*/ |
201
|
2 |
|
public function hasInstallInfos() |
202
|
|
|
{ |
203
|
2 |
|
return count($this->installInfos) > 0; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns the plugin classes. |
208
|
|
|
* |
209
|
|
|
* @return string[] The fully qualified plugin class names. |
|
|
|
|
210
|
|
|
* |
211
|
|
|
* @see setPluginClasses() |
212
|
|
|
*/ |
213
|
57 |
|
public function getPluginClasses() |
214
|
|
|
{ |
215
|
57 |
|
return array_keys($this->pluginClasses); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Sets the plugin classes. |
220
|
|
|
* |
221
|
|
|
* The plugin classes must be fully-qualified class names that implement |
222
|
|
|
* {@link Puli\Manager\Api\PuliPlugin}. If a class is not |
223
|
|
|
* found or does not implement that interface, an exception is thrown. |
224
|
|
|
* |
225
|
|
|
* The plugin classes must not have required parameters in their constructor |
226
|
|
|
* so that they can be successfully instantiated. If a constructor has |
227
|
|
|
* required parameters, an exception is thrown. |
228
|
|
|
* |
229
|
|
|
* Leading backslashes are removed from the fully-qualified class names. |
230
|
|
|
* |
231
|
|
|
* @param string[] $pluginClasses The fully qualified plugin class names. |
232
|
|
|
* |
233
|
|
|
* @throws InvalidConfigException If the class is not found, is not a class, |
234
|
|
|
* does not implement {@link PuliPlugin} |
235
|
|
|
* or has required constructor parameters. |
236
|
|
|
*/ |
237
|
36 |
|
public function setPluginClasses(array $pluginClasses) |
238
|
|
|
{ |
239
|
36 |
|
$this->pluginClasses = array(); |
240
|
|
|
|
241
|
36 |
|
$this->addPluginClasses($pluginClasses); |
242
|
36 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Adds multiple plugin classes. |
246
|
|
|
* |
247
|
|
|
* The plugin classes must be fully-qualified class names that implement |
248
|
|
|
* {@link Puli\Manager\Api\PuliPlugin}. If a class is not |
249
|
|
|
* found or does not implement that interface, an exception is thrown. |
250
|
|
|
* |
251
|
|
|
* The plugin classes must not have required parameters in their constructor |
252
|
|
|
* so that they can be successfully instantiated. If a constructor has |
253
|
|
|
* required parameters, an exception is thrown. |
254
|
|
|
* |
255
|
|
|
* Leading backslashes are removed from the fully-qualified class names. |
256
|
|
|
* |
257
|
|
|
* @param string[] $pluginClasses The fully qualified plugin class names. |
258
|
|
|
*/ |
259
|
37 |
|
public function addPluginClasses(array $pluginClasses) |
260
|
|
|
{ |
261
|
37 |
|
foreach ($pluginClasses as $pluginClass) { |
262
|
36 |
|
$this->addPluginClass($pluginClass); |
263
|
|
|
} |
264
|
37 |
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Adds a plugin class. |
268
|
|
|
* |
269
|
|
|
* The plugin class must be a fully-qualified class name that implements |
270
|
|
|
* {@link PuliPlugin}. If the class is not found or does not implement |
271
|
|
|
* that interface, an exception is thrown. |
272
|
|
|
* |
273
|
|
|
* The plugin class must not have required parameters in its constructor |
274
|
|
|
* so that it can be successfully instantiate. If the constructor has |
275
|
|
|
* required parameters, an exception is thrown. |
276
|
|
|
* |
277
|
|
|
* Leading backslashes are removed from the fully-qualified class name. |
278
|
|
|
* |
279
|
|
|
* @param string $pluginClass The fully qualified plugin class name. |
280
|
|
|
*/ |
281
|
57 |
|
public function addPluginClass($pluginClass) |
282
|
|
|
{ |
283
|
57 |
|
$this->pluginClasses[ltrim($pluginClass, '\\')] = true; |
284
|
57 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Removes a plugin class. |
288
|
|
|
* |
289
|
|
|
* If the plugin class has not been added, this method does nothing. This |
290
|
|
|
* method also does not validate whether the passed value is actually a |
291
|
|
|
* plugin class. |
292
|
|
|
* |
293
|
|
|
* Leading backslashes are removed from the fully-qualified class name. |
294
|
|
|
* |
295
|
|
|
* @param string $pluginClass The fully qualified plugin class name. |
296
|
|
|
*/ |
297
|
10 |
|
public function removePluginClass($pluginClass) |
298
|
|
|
{ |
299
|
10 |
|
unset($this->pluginClasses[ltrim($pluginClass, '\\')]); |
300
|
10 |
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Removes all plugin classes. |
304
|
|
|
*/ |
305
|
1 |
|
public function clearPluginClasses() |
306
|
|
|
{ |
307
|
1 |
|
$this->pluginClasses = array(); |
308
|
1 |
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Returns whether the configuration contains a plugin class. |
312
|
|
|
* |
313
|
|
|
* @param string $pluginClass The fully qualified plugin class name. |
314
|
|
|
* |
315
|
|
|
* @return bool Whether the configuration contains the plugin class. |
316
|
|
|
*/ |
317
|
14 |
|
public function hasPluginClass($pluginClass) |
318
|
|
|
{ |
319
|
14 |
|
return isset($this->pluginClasses[ltrim($pluginClass, '\\')]); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Returns whether the configuration contains any plugin classes. |
324
|
|
|
* |
325
|
|
|
* @return bool Whether the configuration contains any plugin classes. |
326
|
|
|
*/ |
327
|
2 |
|
public function hasPluginClasses() |
328
|
|
|
{ |
329
|
2 |
|
return count($this->pluginClasses) > 0; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.