1 | <?php |
||
21 | class Config implements \ArrayAccess |
||
22 | { |
||
23 | static private $require_cache = []; |
||
24 | |||
25 | static private function isolated_require($__FILE__) |
||
34 | |||
35 | /** |
||
36 | * An array of key/value where _key_ is a path to a config directory and _value_ is its weight. |
||
37 | * The array is sorted according to the weight of the paths. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | private $paths = []; |
||
42 | |||
43 | /** |
||
44 | * Callbacks to synthesize the configurations. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | private $synthesizers = []; |
||
49 | |||
50 | /** |
||
51 | * Synthesized configurations. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private $synthesized = []; |
||
56 | |||
57 | /** |
||
58 | * A cache to store and retrieve the synthesized configurations. |
||
59 | * |
||
60 | * @var Storage |
||
61 | */ |
||
62 | public $cache; |
||
63 | |||
64 | /** |
||
65 | * Initialize the {@link $paths}, {@link $synthesizers}, and {@link $cache} properties. |
||
66 | * |
||
67 | * @param array $paths An array of key/value pairs where _key_ is the path to a config |
||
68 | * directory and _value_ is the weight of that path. |
||
69 | * @param array $synthesizers |
||
70 | * @param Storage $cache A cache for synthesized configurations. |
||
71 | */ |
||
72 | public function __construct(array $paths, array $synthesizers = [], Storage $cache = null) |
||
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | * |
||
83 | * @throws OffsetNotWritable in attempt to set a configuration. |
||
84 | */ |
||
85 | public function offsetSet($offset, $value) |
||
89 | |||
90 | /** |
||
91 | * Checks if a config has been synthesized. |
||
92 | * |
||
93 | * @param string $id The identifier of the config. |
||
94 | * |
||
95 | * @return bool `true` if the config has been synthesized, `false` otherwise. |
||
96 | */ |
||
97 | public function offsetExists($id) |
||
101 | |||
102 | /** |
||
103 | * @inheritdoc |
||
104 | * |
||
105 | * @throws OffsetNotWritable in attempt to unset an offset. |
||
106 | */ |
||
107 | public function offsetUnset($offset) |
||
111 | |||
112 | /** |
||
113 | * Returns the specified synthesized configuration. |
||
114 | * |
||
115 | * @param string $id The identifier of the config. |
||
116 | * |
||
117 | * @return mixed |
||
118 | * |
||
119 | * @throws \InvalidArgumentException in attempt to obtain an undefined config. |
||
120 | */ |
||
121 | public function offsetGet($id) |
||
143 | |||
144 | /** |
||
145 | * @var string |
||
146 | */ |
||
147 | private $cache_key; |
||
148 | |||
149 | /** |
||
150 | * Build a cache key according to the current paths and the config name. |
||
151 | * |
||
152 | * @param string $name |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | private function get_cache_key($name) |
||
165 | |||
166 | /** |
||
167 | * Revokes the synthesized configs and the cache key. |
||
168 | * |
||
169 | * The method is usually called after the config paths have been modified. |
||
170 | */ |
||
171 | protected function revoke() |
||
176 | |||
177 | /** |
||
178 | * Adds a path or several paths to the config. |
||
179 | * |
||
180 | * Paths are sorted according to their weight. The order in which they were defined is |
||
181 | * preserved for paths with the same weight. |
||
182 | * |
||
183 | * <pre> |
||
184 | * <?php |
||
185 | * |
||
186 | * $config->add('/path/to/config', 10); |
||
187 | * $config->add([ |
||
188 | * |
||
189 | * '/path1/to/config' => 10, |
||
190 | * '/path2/to/config' => 10, |
||
191 | * '/path2/to/config' => -10 |
||
192 | * |
||
193 | * ]); |
||
194 | * </pre> |
||
195 | * |
||
196 | * @param string|array $path |
||
197 | * @param int $weight Weight of the path. The argument is discarded if `$path` is an array. |
||
198 | * |
||
199 | * @throws \InvalidArgumentException if the path is empty. |
||
200 | */ |
||
201 | public function add($path, $weight = 0) |
||
224 | |||
225 | /** |
||
226 | * Returns the fragments of a configuration. |
||
227 | * |
||
228 | * @param string $name Name of the configuration. |
||
229 | * |
||
230 | * @return array Where _key_ is the pathname to the fragment file and _value_ the value |
||
231 | * returned when the file was required. |
||
232 | */ |
||
233 | public function get_fragments($name) |
||
253 | |||
254 | /** |
||
255 | * Synthesize a configuration. |
||
256 | * |
||
257 | * @param string $name Name of the configuration to synthesize. |
||
258 | * @param string|array $synthesizer Callback for the synthesis. |
||
259 | * @param null|string $from If the configuration is a derivative $from is the name |
||
260 | * of the source configuration. |
||
261 | * |
||
262 | * @return mixed |
||
263 | */ |
||
264 | public function synthesize($name, $synthesizer, $from = null) |
||
293 | |||
294 | /** |
||
295 | * @param string $name |
||
296 | * @param callable $synthesizer |
||
297 | * |
||
298 | * @return mixed |
||
299 | */ |
||
300 | private function synthesize_for_real($name, $synthesizer) |
||
321 | } |
||
322 |