|
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
|
|
|
/* Based on League\Container\Container v1.x class (https://github.com/thephpleague/container/blob/1.x/src/Container.php) |
|
18
|
|
|
* which is authored by Phil Bennett (https://github.com/philipobenito) |
|
19
|
|
|
* and other contributors (https://github.com/thephpleague/container/contributors). |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
namespace Pinepain\Container\Extras; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
use League\Container\ContainerInterface; |
|
27
|
|
|
use League\Container\Definition\ClassDefinition; |
|
28
|
|
|
use League\Container\Definition\DefinitionInterface; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class LeagueContainerConfigurator extends AbstractContainerConfigurator |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var ContainerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $container; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ContainerInterface $container |
|
40
|
|
|
*/ |
|
41
|
10 |
|
public function __construct(ContainerInterface $container) |
|
42
|
|
|
{ |
|
43
|
10 |
|
$this->container = $container; |
|
44
|
10 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
10 |
|
protected function configureMany($config) |
|
50
|
|
|
{ |
|
51
|
10 |
|
foreach ($config as $alias => $options) { |
|
52
|
9 |
|
$this->configureOne($alias, $options); |
|
53
|
10 |
|
} |
|
54
|
10 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
9 |
|
public function configureOne($alias, $options) |
|
60
|
|
|
{ |
|
61
|
9 |
|
$concrete = $this->resolveConcrete($options); |
|
62
|
|
|
|
|
63
|
9 |
|
$share = is_array($options) && !empty($options['share']); |
|
64
|
|
|
|
|
65
|
|
|
// define in the container, with constructor arguments and method calls |
|
66
|
9 |
|
$definition = $this->container->add($alias, $concrete, $share); |
|
67
|
|
|
|
|
68
|
9 |
|
if ($definition instanceof DefinitionInterface) { |
|
69
|
3 |
|
$this->addDefinitionArguments($definition, $options); |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
9 |
|
if ($definition instanceof ClassDefinition) { |
|
73
|
2 |
|
$this->addDefinitionMethods($definition, $options); |
|
74
|
2 |
|
} |
|
75
|
9 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param DefinitionInterface $definition |
|
79
|
|
|
* @param $options |
|
80
|
|
|
*/ |
|
81
|
3 |
|
protected function addDefinitionArguments(DefinitionInterface $definition, $options) |
|
82
|
|
|
{ |
|
83
|
3 |
|
$arguments = []; |
|
84
|
|
|
|
|
85
|
3 |
|
if (is_array($options)) { |
|
86
|
3 |
|
$arguments = (array_key_exists('arguments', $options)) ? (array)$options['arguments'] : []; |
|
87
|
3 |
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
$definition->withArguments($arguments); |
|
90
|
3 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param ClassDefinition $definition |
|
94
|
|
|
* @param $options |
|
95
|
|
|
*/ |
|
96
|
2 |
|
protected function addDefinitionMethods(ClassDefinition $definition, $options) |
|
97
|
|
|
{ |
|
98
|
2 |
|
$methods = []; |
|
99
|
|
|
|
|
100
|
2 |
|
if (is_array($options)) { |
|
101
|
2 |
|
$methods = (array_key_exists('methods', $options)) ? (array)$options['methods'] : []; |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
$definition->withMethodCalls($methods); |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Resolves the concrete class |
|
109
|
|
|
* |
|
110
|
|
|
* @param mixed $concrete |
|
111
|
|
|
* |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
9 |
|
protected function resolveConcrete($concrete) |
|
115
|
|
|
{ |
|
116
|
9 |
|
if (is_array($concrete)) { |
|
117
|
6 |
|
if (array_key_exists('definition', $concrete)) { |
|
118
|
1 |
|
$concrete = $concrete['definition']; |
|
119
|
6 |
|
} elseif (array_key_exists('class', $concrete)) { |
|
120
|
4 |
|
$concrete = $concrete['class']; |
|
121
|
4 |
|
} else { |
|
122
|
1 |
|
$concrete = null; |
|
123
|
|
|
} |
|
124
|
6 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
// if the concrete doesn't have a class associated with it then it |
|
127
|
|
|
// must be either a Closure or arbitrary type so we just bind that |
|
128
|
9 |
|
return $concrete; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|