1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
+----------------------------------------------------------------------+ |
5
|
|
|
| This file is part of the pinepain/container-extras PHP library. | |
6
|
|
|
| | |
7
|
|
|
| Copyright (c) 2015-2016 Bogdan Padalko <[email protected]> | |
8
|
|
|
| | |
9
|
|
|
| Licensed under the MIT license: http://opensource.org/licenses/MIT | |
10
|
|
|
| | |
11
|
|
|
| For the full copyright and license information, please view the | |
12
|
|
|
| LICENSE file that was distributed with this source or visit | |
13
|
|
|
| http://opensource.org/licenses/MIT | |
14
|
|
|
+----------------------------------------------------------------------+ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
namespace Pinepain\Container\Extras; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
use League\Container\ContainerInterface; |
22
|
|
|
use League\Container\Definition\ClassDefinition; |
23
|
|
|
use League\Container\Definition\DefinitionInterface; |
24
|
|
|
use Pinepain\Container\Extras\Exceptions\InvalidConfigException; |
25
|
|
|
use Traversable; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class ContainerConfigurator implements ContainerConfiguratorInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ContainerInterface |
32
|
|
|
*/ |
33
|
|
|
private $container; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ContainerInterface $container |
37
|
|
|
*/ |
38
|
12 |
|
public function __construct(ContainerInterface $container) |
39
|
|
|
{ |
40
|
12 |
|
$this->container = $container; |
41
|
12 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
12 |
|
public function configure($config) |
47
|
|
|
{ |
48
|
12 |
|
if (!is_array($config) && !($config instanceof Traversable)) { |
49
|
1 |
|
throw new InvalidConfigException( |
50
|
|
|
'You can only load definitions from an array or an object that implements Traversable interface.' |
51
|
1 |
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
11 |
|
foreach ($config as $alias => $options) { |
55
|
9 |
|
$this->configureOne($alias, $options); |
56
|
11 |
|
} |
57
|
11 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
9 |
|
public function configureOne($alias, $options) |
63
|
|
|
{ |
64
|
9 |
|
$concrete = $this->resolveConcrete($options); |
65
|
|
|
|
66
|
9 |
|
$share = is_array($options) && !empty($options['share']); |
67
|
|
|
|
68
|
|
|
// define in the container, with constructor arguments and method calls |
69
|
9 |
|
$definition = $this->container->add($alias, $concrete, $share); |
70
|
|
|
|
71
|
9 |
|
if ($definition instanceof DefinitionInterface) { |
72
|
3 |
|
$this->addDefinitionArguments($definition, $options); |
73
|
3 |
|
} |
74
|
|
|
|
75
|
9 |
|
if ($definition instanceof ClassDefinition) { |
76
|
2 |
|
$this->addDefinitionMethods($definition, $options); |
77
|
2 |
|
} |
78
|
9 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param DefinitionInterface $definition |
82
|
|
|
* @param $options |
83
|
|
|
*/ |
84
|
3 |
|
protected function addDefinitionArguments(DefinitionInterface $definition, $options) |
85
|
|
|
{ |
86
|
3 |
|
$arguments = []; |
87
|
|
|
|
88
|
3 |
|
if (is_array($options)) { |
89
|
3 |
|
$arguments = (array_key_exists('arguments', $options)) ? (array)$options['arguments'] : []; |
90
|
3 |
|
} |
91
|
|
|
|
92
|
3 |
|
$definition->withArguments($arguments); |
93
|
3 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param ClassDefinition $definition |
97
|
|
|
* @param $options |
98
|
|
|
*/ |
99
|
2 |
|
protected function addDefinitionMethods(ClassDefinition $definition, $options) |
100
|
|
|
{ |
101
|
2 |
|
$methods = []; |
102
|
|
|
|
103
|
2 |
|
if (is_array($options)) { |
104
|
2 |
|
$methods = (array_key_exists('methods', $options)) ? (array)$options['methods'] : []; |
105
|
2 |
|
} |
106
|
|
|
|
107
|
2 |
|
$definition->withMethodCalls($methods); |
108
|
2 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Resolves the concrete class |
112
|
|
|
* |
113
|
|
|
* @param mixed $concrete |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
9 |
|
protected function resolveConcrete($concrete) |
118
|
|
|
{ |
119
|
9 |
|
if (is_array($concrete)) { |
120
|
6 |
|
if (array_key_exists('definition', $concrete)) { |
121
|
1 |
|
$concrete = $concrete['definition']; |
122
|
6 |
|
} elseif (array_key_exists('class', $concrete)) { |
123
|
4 |
|
$concrete = $concrete['class']; |
124
|
4 |
|
} else { |
125
|
1 |
|
$concrete = null; |
126
|
|
|
} |
127
|
6 |
|
} |
128
|
|
|
|
129
|
|
|
// if the concrete doesn't have a class associated with it then it |
130
|
|
|
// must be either a Closure or arbitrary type so we just bind that |
131
|
9 |
|
return $concrete; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|