1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Bundle\VMProApiBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
|
9
|
|
|
class CachePoolPass implements CompilerPassInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
public function process(ContainerBuilder $container) |
15
|
|
|
{ |
16
|
|
|
$clientDefinition = $container->getDefinition('vmpro_api.client'); |
17
|
|
|
|
18
|
|
|
$clientDefinition->setArgument(2, $this->getServiceReference($container, 'vm_pro_api_logger')); |
19
|
|
|
$clientDefinition->setArgument(3, $this->getServiceReference($container, 'vm_pro_api_cache_pool')); |
20
|
|
|
$clientDefinition->setArgument(4, $container->getParameter('vm_pro_api_cache_ttl')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns a reference to a service, if that service exists in the container. |
25
|
|
|
* The service ID is obtained by fetching the value of the provided $parameterName. |
26
|
|
|
* This allows us to inject a service into the bundle by supplying the service ID in the bundle configuration. |
27
|
|
|
* |
28
|
|
|
* If the specified service does not exist in the container, an exception is thrown. |
29
|
|
|
* |
30
|
|
|
* @param ContainerBuilder $container |
31
|
|
|
* @param string $parameterName |
32
|
|
|
* |
33
|
|
|
* @return Reference|null |
34
|
|
|
* |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
|
|
private function getServiceReference(ContainerBuilder $container, $parameterName) |
38
|
|
|
{ |
39
|
|
|
$serviceId = $container->getParameter($parameterName); |
40
|
|
|
if (empty($serviceId)) { |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($container->hasDefinition($serviceId) || $container->hasAlias($serviceId)) { |
45
|
|
|
return new Reference($serviceId); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
throw new \Exception(sprintf( |
49
|
|
|
'Service "%s" specified in parameter "%s" does not exist', |
50
|
|
|
$serviceId, |
51
|
|
|
$parameterName |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|