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
209 lines (192 loc) · 6.3 KB
/
Copy pathAbstractBaseModule.php
File metadata and controls
209 lines (192 loc) · 6.3 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
<?php
namespace RebelCode\Modular\Module;
use ArrayAccess;
use Dhii\Data\Container\ContainerFactoryInterface;
use Dhii\Data\Container\NormalizeContainerCapableTrait;
use Dhii\Exception\CreateInternalExceptionCapableTrait;
use Dhii\Exception\CreateInvalidArgumentExceptionCapableTrait;
use Dhii\Exception\CreateRuntimeExceptionCapableTrait;
use Dhii\Exception\InternalException;
use Dhii\Factory\Exception\CouldNotMakeExceptionInterface;
use Dhii\Factory\Exception\FactoryExceptionInterface;
use Dhii\Factory\FactoryInterface;
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 Psr\Container\ContainerInterface;
use RuntimeException;
use stdClass;
/**
* 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 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 functionality for creating invalid-argument exceptions.
*
* @since [*next-version*]
*/
use CreateInvalidArgumentExceptionCapableTrait;
/*
* Provides functionality for creating internal exceptions.
*
* @since [*next-version*]
*/
use CreateInternalExceptionCapableTrait;
/*
* Provides functionality for creating runtime exceptions.
*
* @since [*next-version*]
*/
use CreateRuntimeExceptionCapableTrait;
/*
* Provides string translating functionality.
*
* @since [*next-version*]
*/
use StringTranslatingTrait;
/**
* The factory to use for creating containers.
*
* @since [*next-version*]
*
* @var ContainerFactoryInterface
*/
protected $containerFactory;
/**
* Initializes the module with all required information.
*
* @since [*next-version*]
*
* @param ContainerFactoryInterface $containerFactory The container factory.
* @param string|Stringable $key The module key.
* @param string[]|Stringable[] $dependencies The module dependencies.
* @param array|ArrayAccess|stdClass|ContainerInterface $config The module config.
*/
protected function _initModule(ContainerFactoryInterface $containerFactory, $key, $dependencies = [], $config = [])
{
$this->_setKey($key);
$this->_setDependencies($dependencies);
$this->_setConfig($config);
$this->_setContainerFactory($containerFactory);
}
/**
* Retrieves the container factory associated with this module.
*
* @since [*next-version*]
*
* @return ContainerFactoryInterface The container factory instance.
*/
protected function _getContainerFactory()
{
return $this->containerFactory;
}
/**
* Sets the container factory for this module.
*
* @since [*next-version*]
*
* @param ContainerFactoryInterface $containerFactory The container factory instance.
*/
protected function _setContainerFactory(ContainerFactoryInterface $containerFactory)
{
$this->containerFactory = $containerFactory;
}
/**
* Creates a container instance using the container factory.
*
* @since [*next-version*]
*
* @param array $definitions
*
* @return ContainerInterface The created container instance.
*
* @throws CouldNotMakeExceptionInterface If the factory failed to create the exception.
* @throws FactoryExceptionInterface If the factory encountered an error.
*/
protected function _createContainer($definitions = [])
{
return $this->_getContainerFactory()->make(['definitions' => $definitions]);
}
/**
* 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.
*
* @return array|ArrayAccess|stdClass|ContainerInterface The config.
*
* @throws InternalException If an exception was thrown by the PHP config file.
* @throws InvalidArgumentException If the config retrieved from the PHP config file is not a valid container.
*/
protected function _loadPhpConfigFile($filePath)
{
try {
if (!file_exists($filePath) || !is_readable($filePath)) {
throw $this->_createRuntimeException(
$this->__('Config file does not exist or not readable'),
null,
null
);
}
$config = require $filePath;
} catch (Exception $exception) {
throw $this->_createInternalException(
$this->__('The PHP config file triggered an exception'),
null,
null
);
}
try {
return $this->_normalizeContainer($config);
} catch (InvalidArgumentException $exception) {
throw $this->_createInvalidArgumentException(
$this->__('The config retrieved from the PHP config file is not a valid container'),
null,
null,
$config
);
}
}
}