1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Interop\Container\ServiceProviderInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
use TheCodingMachine\ServiceProvider\Registry; |
14
|
|
|
|
15
|
|
|
class ServiceProviderCompilationPass implements CompilerPassInterface |
16
|
|
|
{ |
17
|
|
|
private $registryId; |
18
|
|
|
|
19
|
|
|
private $registryProvider; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param int $registryId |
23
|
|
|
* @param RegistryProviderInterface $registryProvider |
24
|
|
|
*/ |
25
|
|
|
public function __construct($registryId, RegistryProviderInterface $registryProvider) |
26
|
|
|
{ |
27
|
|
|
$this->registryId = $registryId; |
28
|
|
|
$this->registryProvider = $registryProvider; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* You can modify the container here before it is dumped to PHP code. |
33
|
|
|
* |
34
|
|
|
* @param ContainerBuilder $container |
35
|
|
|
*/ |
36
|
|
|
public function process(ContainerBuilder $container) |
37
|
|
|
{ |
38
|
|
|
// Now, let's store the registry in the container (an empty version of it... it will be dynamically added at runtime): |
39
|
|
|
$this->registerRegistry($container); |
40
|
|
|
|
41
|
|
|
$registry = $this->registryProvider->getRegistry($container); |
42
|
|
|
|
43
|
|
|
// Note: in the 'boot' method of a bundle, the container is available. |
44
|
|
|
// We use that to push the lazy array in the container. |
45
|
|
|
// The lazy array can be used by the registry that is also part of the container. |
46
|
|
|
// The registry can itself be used by a factory that creates services! |
47
|
|
|
|
48
|
|
|
$this->registerAcclimatedContainer($container); |
49
|
|
|
|
50
|
|
|
foreach ($registry as $serviceProviderKey => $serviceProvider) { |
51
|
|
|
$this->registerFactories($serviceProviderKey, $serviceProvider, $container); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach ($registry as $serviceProviderKey => $serviceProvider) { |
55
|
|
|
$this->registerExtensions($serviceProviderKey, $serviceProvider, $container); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
private function registerRegistry(ContainerBuilder $container) |
61
|
|
|
{ |
62
|
|
|
$definition = new Definition(Registry::class); |
63
|
|
|
$definition->setSynthetic(true); |
64
|
|
|
|
65
|
|
|
$container->setDefinition('service_provider_registry_'.$this->registryId, $definition); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function registerAcclimatedContainer(ContainerBuilder $container) { |
69
|
|
|
$definition = new Definition('TheCodingMachine\\Interop\\ServiceProviderBridgeBundle\\SymfonyContainerAdapter'); |
70
|
|
|
$definition->addArgument(new Reference("service_container")); |
71
|
|
|
|
72
|
|
|
$container->setDefinition('interop_service_provider_acclimated_container', $definition); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
private function registerFactories($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container) { |
|
|
|
|
76
|
|
|
$serviceFactories = $serviceProvider->getFactories(); |
77
|
|
|
|
78
|
|
|
foreach ($serviceFactories as $serviceName => $callable) { |
79
|
|
|
$this->registerService($serviceName, $serviceProviderKey, $callable, $container); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
private function registerExtensions($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container) { |
|
|
|
|
84
|
|
|
$serviceFactories = $serviceProvider->getExtensions(); |
85
|
|
|
|
86
|
|
|
foreach ($serviceFactories as $serviceName => $callable) { |
87
|
|
|
$this->extendService($serviceName, $serviceProviderKey, $callable, $container); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function registerService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container) { |
92
|
|
|
$factoryDefinition = $this->getServiceDefinitionFromCallable($serviceName, $serviceProviderKey, $callable); |
93
|
|
|
|
94
|
|
|
$container->setDefinition($serviceName, $factoryDefinition); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function extendService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container) { |
98
|
|
|
$factoryDefinition = $this->getServiceDefinitionFromCallable($serviceName, $serviceProviderKey, $callable, true); |
99
|
|
|
|
100
|
|
|
if (!$container->has($serviceName)) { |
101
|
|
|
$container->setDefinition($serviceName, $factoryDefinition); |
102
|
|
|
} else { |
103
|
|
|
// The new service will be created under the name 'xxx_decorated_y' |
104
|
|
|
// The old service will be moved to the name 'xxx_decorated_y.inner' |
105
|
|
|
// This old service will be accessible through a callback represented by 'xxx_decorated_y.callbackwrapper' |
106
|
|
|
// The $servicename becomes an alias pointing to 'xxx_decorated_y' |
107
|
|
|
|
108
|
|
|
$oldServiceName = $serviceName; |
109
|
|
|
$serviceName = $this->getDecoratedServiceName($serviceName, $container); |
110
|
|
|
|
111
|
|
|
$innerName = $serviceName.'.inner'; |
112
|
|
|
$innerDefinition = $container->findDefinition($oldServiceName); |
113
|
|
|
$container->setDefinition($innerName, $innerDefinition); |
114
|
|
|
|
115
|
|
|
$factoryDefinition->addArgument(new Reference($innerName)); |
116
|
|
|
|
117
|
|
|
$container->setDefinition($serviceName, $factoryDefinition); |
118
|
|
|
$container->setDefinition($innerName, $innerDefinition); |
119
|
|
|
|
120
|
|
|
$container->setAlias($oldServiceName, (new Alias($serviceName))->setPublic(true)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function getDecoratedServiceName($serviceName, ContainerBuilder $container) { |
125
|
|
|
$counter = 1; |
126
|
|
|
while ($container->has($serviceName.'_decorated_'.$counter)) { |
127
|
|
|
$counter++; |
128
|
|
|
} |
129
|
|
|
return $serviceName.'_decorated_'.$counter; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function getServiceDefinitionFromCallable($serviceName, $serviceProviderKey, callable $callable, $isExtension = false) |
133
|
|
|
{ |
134
|
|
|
/*if ($callable instanceof DefinitionInterface) { |
135
|
|
|
// TODO: plug the definition-interop converter here! |
136
|
|
|
}*/ |
137
|
|
|
$factoryDefinition = new Definition('Class'); // TODO: in PHP7, we can get the return type of the function! |
138
|
|
|
$factoryDefinition->setPublic(true); |
139
|
|
|
$containerDefinition = new Reference('interop_service_provider_acclimated_container'); |
140
|
|
|
|
141
|
|
|
if ((is_array($callable) && is_string($callable[0])) || is_string($callable)) { |
142
|
|
|
$factoryDefinition->setFactory($callable); |
143
|
|
|
$factoryDefinition->addArgument($containerDefinition); |
144
|
|
|
} else { |
145
|
|
|
$method = $isExtension ? 'extendService' : 'createService'; |
146
|
|
|
$factoryDefinition->setFactory([ new Reference('service_provider_registry_'.$this->registryId), $method ]); |
147
|
|
|
$factoryDefinition->addArgument($serviceProviderKey); |
148
|
|
|
$factoryDefinition->addArgument($serviceName); |
149
|
|
|
$factoryDefinition->addArgument($containerDefinition); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $factoryDefinition; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.