This repository was archived by the owner on Mar 27, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathAbstractBaseModule.php
More file actions
451 lines (411 loc) · 13.8 KB
/
Copy pathAbstractBaseModule.php
File metadata and controls
451 lines (411 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
namespace RebelCode\Modular\Module;
use ArrayAccess;
use Dhii\Config\ConfigFactoryInterface;
use Dhii\Data\Container\ContainerFactoryInterface;
use Dhii\Data\Container\ContainerGetCapableTrait;
use Dhii\Data\Container\ContainerGetPathCapableTrait;
use Dhii\Data\Container\ContainerHasCapableTrait;
use Dhii\Data\Container\CreateContainerExceptionCapableTrait;
use Dhii\Data\Container\CreateNotFoundExceptionCapableTrait;
use Dhii\Data\Container\NormalizeContainerCapableTrait;
use Dhii\Data\Container\NormalizeKeyCapableTrait;
use Dhii\Event\EventFactoryInterface;
use Dhii\Exception\CreateInternalExceptionCapableTrait;
use Dhii\Exception\CreateInvalidArgumentExceptionCapableTrait;
use Dhii\Exception\CreateOutOfRangeExceptionCapableTrait;
use Dhii\Exception\CreateRuntimeExceptionCapableTrait;
use Dhii\Exception\InternalException;
use Dhii\Factory\Exception\CouldNotMakeExceptionInterface;
use Dhii\Factory\Exception\FactoryExceptionInterface;
use Dhii\I18n\StringTranslatingTrait;
use Dhii\Modular\Module\DependenciesAwareInterface;
use Dhii\Modular\Module\ModuleInterface;
use Dhii\Util\Normalization\NormalizeIterableCapableTrait;
use Dhii\Util\Normalization\NormalizeStringCapableTrait;
use Dhii\Util\String\StringableInterface as Stringable;
use Exception;
use InvalidArgumentException;
use OutOfRangeException;
use Psr\Container\ContainerInterface;
use Psr\EventManager\EventManagerInterface;
use RebelCode\Modular\Events\EventsFunctionalityTrait;
use RuntimeException;
use stdClass;
use Traversable;
/**
* Common base functionality for modules.
*
* @since [*next-version*]
*/
abstract class AbstractBaseModule implements
ModuleInterface,
DependenciesAwareInterface
{
/*
* Provides common module functionality.
*
* @since [*next-version*]
*/
use ModuleTrait {
_getKey as public getKey;
_getDependencies as public getDependencies;
}
/*
* Provides functionality for retrieving a value for a path from any type of container hierarchy.
*
* @since [*next-version*]
*/
use ContainerGetPathCapableTrait;
/*
* Provides functionality for reading from any type of container.
*
* @since [*next-version*]
*/
use ContainerGetCapableTrait;
/*
* Provides functionality for key-checking any type of container.
*
* @since [*next-version*]
*/
use ContainerHasCapableTrait;
/*
* Provides key normalization functionality.
*
* @since [*next-version*]
*/
use NormalizeKeyCapableTrait;
/*
* Provides string normalization functionality.
*
* @since [*next-version*]
*/
use NormalizeStringCapableTrait;
/*
* Provides iterable normalization functionality.
*
* @since [*next-version*]
*/
use NormalizeIterableCapableTrait;
/*
* Provides container normalization functionality.
*
* @since [*next-version*]
*/
use NormalizeContainerCapableTrait;
/*
* Provides common functionality for events.
*
* @since [*next-version*]
*/
use EventsFunctionalityTrait;
/*
* Provides functionality for creating invalid-argument exceptions.
*
* @since [*next-version*]
*/
use CreateInvalidArgumentExceptionCapableTrait;
/*
* Provides functionality for creating out-of-range exceptions.
*
* @since [*next-version*]
*/
use CreateOutOfRangeExceptionCapableTrait;
/*
* Provides functionality for creating internal exceptions.
*
* @since [*next-version*]
*/
use CreateInternalExceptionCapableTrait;
/*
* Provides functionality for creating runtime exceptions.
*
* @since [*next-version*]
*/
use CreateRuntimeExceptionCapableTrait;
/*
* Provides functionality for creating container exceptions.
*
* @since [*next-version*]
*/
use CreateContainerExceptionCapableTrait;
/*
* Provides functionality for creating container not-found exceptions.
*
* @since [*next-version*]
*/
use CreateNotFoundExceptionCapableTrait;
/*
* Provides string translating functionality.
*
* @since [*next-version*]
*/
use StringTranslatingTrait;
/**
* The factory to use for creating containers.
*
* @since [*next-version*]
*
* @var ContainerFactoryInterface
*/
protected $containerFactory;
/**
* The factory to use for creating config containers.
*
* @since [*next-version*]
*
* @var ConfigFactoryInterface
*/
protected $configFactory;
/**
* The factory to use for creating composite containers.
*
* @since [*next-version*]
*
* @var ContainerFactoryInterface
*/
protected $compContainerFactory;
/**
* Initializes the module with all required information.
*
* @since [*next-version*]
*
* @param string|Stringable $key The module key.
* @param string[]|Stringable[] $dependencies The module dependencies.
* @param ConfigFactoryInterface $configFactory The config factory.
* @param ContainerFactoryInterface $containerFactory The container factory.
* @param ContainerFactoryInterface $compContainerFactory The composite container factory.
*/
protected function _initModule(
$key,
$dependencies,
ConfigFactoryInterface $configFactory,
ContainerFactoryInterface $containerFactory,
ContainerFactoryInterface $compContainerFactory
) {
$this->_setKey($key);
$this->_setDependencies($dependencies);
$this->_setConfigFactory($configFactory);
$this->_setContainerFactory($containerFactory);
$this->_setCompositeContainerFactory($compContainerFactory);
}
/**
* Initializes the module's event functionality.
*
* @since [*next-version*]
*
* @param EventManagerInterface|null $eventManager The event manager, or null.
* @param EventFactoryInterface|null $eventFactory The event factory, or null.
*/
protected function _initModuleEvents($eventManager, $eventFactory)
{
$this->_setEventManager($eventManager);
$this->_setEventFactory($eventFactory);
}
/**
* Creates the module setup container for the module.
*
* @since [*next-version*]
*
* @param array|ArrayAccess|stdClass|ContainerInterface $config The config data.
* @param array|ArrayAccess|stdClass|ContainerInterface $services The service definitions.
*
* @return ContainerInterface The created module setup container.
*/
protected function _setupContainer($config, $services)
{
return $this->_createCompositeContainer([
($config instanceof ContainerInterface) ? $config : $this->_createConfig($config),
($services instanceof ContainerInterface) ? $services : $this->_createContainer($services),
]);
}
/**
* Retrieves the container factory associated with this module.
*
* @since [*next-version*]
*
* @return ContainerFactoryInterface The container factory instance, if any.
*/
protected function _getContainerFactory()
{
return $this->containerFactory;
}
/**
* Sets the container factory for this module.
*
* @since [*next-version*]
*
* @param ContainerFactoryInterface $containerFactory The container factory instance or null.
*/
protected function _setContainerFactory($containerFactory)
{
if ($containerFactory !== null && !($containerFactory instanceof ContainerFactoryInterface)) {
throw $this->_createInvalidArgumentException(
$this->__('Argument is not a container factory'),
null,
null,
$containerFactory
);
}
$this->containerFactory = $containerFactory;
}
/**
* Retrieves the config container factory associated with this module.
*
* @since [*next-version*]
*
* @return ConfigFactoryInterface The config container factory instance, if any.
*/
protected function _getConfigFactory()
{
return $this->configFactory;
}
/**
* Sets the config container factory for this module.
*
* @since [*next-version*]
*
* @param ConfigFactoryInterface $configFactory The config container factory instance.
*/
protected function _setConfigFactory($configFactory)
{
if ($configFactory !== null && !($configFactory instanceof ConfigFactoryInterface)) {
throw $this->_createInvalidArgumentException(
$this->__('Argument is not a container factory'),
null,
null,
$configFactory
);
}
$this->configFactory = $configFactory;
}
/**
* Retrieves the composite container factory associated with this module.
*
* @since [*next-version*]
*
* @return ContainerFactoryInterface The composite container factory instance.
*/
protected function _getCompositeContainerFactory()
{
return $this->compContainerFactory;
}
/**
* Sets the composite container factory for this module.
*
* @since [*next-version*]
*
* @param ContainerFactoryInterface $compContainerFactory The composite container factory instance.
*/
protected function _setCompositeContainerFactory(ContainerFactoryInterface $compContainerFactory)
{
$this->compContainerFactory = $compContainerFactory;
}
/**
* Creates a container instance with the given service definitions.
*
* @since [*next-version*]
*
* @param callable[]|ArrayAccess|stdClass|ContainerInterface $definitions The service definitions.
* @param ContainerInterface|null $parent The parent container instance, if any.
*
* @throws CouldNotMakeExceptionInterface If the factory failed to create the exception.
* @throws FactoryExceptionInterface If the factory encountered an error.
* @throws RuntimeException If the container factory associated with this instance is null.
*
* @return ContainerInterface The created container instance.
*/
protected function _createContainer($definitions = [], ContainerInterface $parent = null)
{
$containerFactory = $this->_getContainerFactory();
if (!($containerFactory instanceof ContainerFactoryInterface)) {
throw $this->_createRuntimeException(
$this->__('Not a valid container factory instance'),
null,
null
);
}
return $containerFactory->make([
ContainerFactoryInterface::K_DATA => $definitions,
'parent' => $parent,
]);
}
/**
* Creates a config container instance with the given data.
*
* @since [*next-version*]
*
* @param callable[]|ArrayAccess|stdClass|ContainerInterface $data The config data.
*
* @throws CouldNotMakeExceptionInterface If the factory failed to create the exception.
* @throws FactoryExceptionInterface If the factory encountered an error.
* @throws RuntimeException If the container factory associated with this instance is null.
*
* @return ContainerInterface The created config container instance.
*/
protected function _createConfig($data = [])
{
return $this->_getConfigFactory()->make([
ConfigFactoryInterface::K_DATA => $data,
]);
}
/**
* Creates a composite container with the given children container instances.
*
* @since [*next-version*]
*
* @param ContainerInterface[]|Traversable $containers The children container instances.
*
* @return ContainerInterface The created composite container instance.
*/
protected function _createCompositeContainer($containers)
{
return $this->_getCompositeContainerFactory()->make(['containers' => $containers]);
}
/**
* Loads a PHP config file and returns the configuration.
*
* Since module systems have varying loading mechanisms, it is not safe to assume that the current working directory
* will be equivalent to the module's directory. Therefore, it is recommended to use absolute paths for the file
* path argument.
*
* @since [*next-version*]
*
* @param string|Stringable $filePath The path to the PHP config file. Absolute paths are recommended.
*
* @throws InternalException If an exception was thrown by the PHP config file.
* @throws OutOfRangeException If the config retrieved from the PHP config file is not a valid container.
* @throws RuntimeException If the config file could not be read.
*
* @return array|ArrayAccess|stdClass|ContainerInterface The config.
*/
protected function _loadPhpConfigFile($filePath)
{
if (!file_exists($filePath) || !is_readable($filePath)) {
throw $this->_createRuntimeException(
$this->__('Config file does not exist or not readable'),
null,
null
);
}
try {
$config = require $filePath;
} catch (Exception $exception) {
throw $this->_createInternalException(
$this->__('The PHP config file triggered an exception'),
null,
$exception
);
}
try {
return $this->_normalizeContainer($config);
} catch (InvalidArgumentException $exception) {
throw $this->_createOutOfRangeException(
$this->__('The config retrieved from the PHP config file is not a valid container'),
null,
null,
$config
);
}
}
}