|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Implementation of this file has been influenced by AuraPHP |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/auraphp/Aura.Di |
|
14
|
|
|
* @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Phalcon\Container\Config; |
|
20
|
|
|
|
|
21
|
|
|
use InvalidArgumentException; |
|
22
|
|
|
use Phalcon\Container\Config; |
|
23
|
|
|
use Phalcon\Container; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A collection of Container config instructions |
|
27
|
|
|
* |
|
28
|
|
|
* @property array $configs |
|
29
|
|
|
*/ |
|
30
|
|
|
class Collection extends Config |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Configs |
|
34
|
|
|
* |
|
35
|
|
|
* @var ConfigInterface[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $configs = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* __construct |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $configs A list of ContainerConfig classes to |
|
43
|
|
|
* instantiate and invoke for configuring the Container. |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function __construct(array $configs) |
|
46
|
|
|
{ |
|
47
|
2 |
|
foreach ($configs as $config) { |
|
48
|
2 |
|
$config = $this->getConfig($config); |
|
49
|
1 |
|
$this->configs[] = $config; |
|
50
|
|
|
} |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get config object from config class or return the object |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $config name of class to instantiate |
|
58
|
|
|
* |
|
59
|
|
|
* @return ConfigInterface |
|
60
|
|
|
* @throws InvalidArgumentException if invalid config |
|
61
|
|
|
*/ |
|
62
|
2 |
|
protected function getConfig($config): ConfigInterface |
|
63
|
|
|
{ |
|
64
|
2 |
|
if (is_string($config)) { |
|
65
|
2 |
|
$config = new $config(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
if (!$config instanceof ConfigInterface) { |
|
69
|
1 |
|
throw new InvalidArgumentException( |
|
70
|
1 |
|
'Container configs must implement ConfigInterface' |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $config; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Define params, setters, and services for each of the configs before the |
|
79
|
|
|
* Container is locked. |
|
80
|
|
|
* |
|
81
|
|
|
* @param Container $container The DI container. |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function define(Container $container): void |
|
84
|
|
|
{ |
|
85
|
1 |
|
foreach ($this->configs as $config) { |
|
86
|
1 |
|
$config->define($container); |
|
87
|
|
|
} |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Modify service objects for each config after the Container is locked. |
|
92
|
|
|
* |
|
93
|
|
|
* @param Container $container The DI container. |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function modify(Container $container): void |
|
96
|
|
|
{ |
|
97
|
1 |
|
foreach ($this->configs as $config) { |
|
98
|
1 |
|
$config->modify($container); |
|
99
|
|
|
} |
|
100
|
1 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|