1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TomPHP\ContainerConfigurator\Pimple; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Pimple\Container; |
7
|
|
|
use TomPHP\ContainerConfigurator\ApplicationConfig; |
8
|
|
|
use TomPHP\ContainerConfigurator\ContainerAdapter; |
9
|
|
|
use TomPHP\ContainerConfigurator\Exception\UnsupportedFeatureException; |
10
|
|
|
use TomPHP\ContainerConfigurator\InflectorConfig; |
11
|
|
|
use TomPHP\ContainerConfigurator\ServiceConfig; |
12
|
|
|
use TomPHP\ContainerConfigurator\ServiceDefinition; |
13
|
|
|
|
14
|
|
|
final class PimpleContainerAdapter implements ContainerAdapter |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Container |
18
|
|
|
*/ |
19
|
|
|
private $container; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Container $container |
23
|
|
|
*/ |
24
|
|
|
public function setContainer($container) |
25
|
|
|
{ |
26
|
|
|
$this->container = $container; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function addApplicationConfig(ApplicationConfig $config, $prefix = 'config') |
30
|
|
|
{ |
31
|
|
|
Assertion::string($prefix); |
32
|
|
|
|
33
|
|
|
if (!empty($prefix)) { |
34
|
|
|
$prefix .= $config->getSeparator(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
foreach ($config as $key => $value) { |
38
|
|
|
$this->container[$prefix . $key] = $value; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function addServiceConfig(ServiceConfig $config) |
43
|
|
|
{ |
44
|
|
|
foreach ($config as $definition) { |
45
|
|
|
$this->addServiceToContainer($definition); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws UnsupportedFeatureException |
51
|
|
|
*/ |
52
|
|
|
public function addInflectorConfig(InflectorConfig $config) |
53
|
|
|
{ |
54
|
|
|
throw UnsupportedFeatureException::forInflectors('Pimple'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function addServiceToContainer(ServiceDefinition $definition) |
58
|
|
|
{ |
59
|
|
|
$factory = function () use ($definition) { |
60
|
|
|
$className = $definition->getClass(); |
61
|
|
|
$instance = new $className(...$this->resolveArguments($definition->getArguments())); |
|
|
|
|
62
|
|
|
|
63
|
|
|
foreach ($definition->getMethods() as $name => $args) { |
64
|
|
|
$instance->$name(...$this->resolveArguments($args)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $instance; |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
if (!$definition->isSingleton()) { |
71
|
|
|
$factory = $this->container->factory($factory); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->container[$definition->getName()] = $factory; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function resolveArguments(array $arguments) |
78
|
|
|
{ |
79
|
|
|
return array_map( |
80
|
|
|
function ($argument) { |
81
|
|
|
if (isset($this->container[$argument])) { |
82
|
|
|
return $this->container[$argument]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $argument; |
86
|
|
|
}, |
87
|
|
|
$arguments |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.