1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KGzocha\Bundle\SearcherBundle\DependencyInjection\CompilerPass; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Krzysztof Gzocha <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class DefinitionBuilder |
13
|
|
|
{ |
14
|
|
|
const CLASS_PARAMETER = 'class'; |
15
|
|
|
const SERVICE_PARAMETER = 'service'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ParametersValidator |
19
|
|
|
*/ |
20
|
|
|
private $parameterValidator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ParametersValidator $parameterValidator |
24
|
|
|
*/ |
25
|
24 |
|
public function __construct(ParametersValidator $parameterValidator) |
26
|
|
|
{ |
27
|
24 |
|
$this->parameterValidator = $parameterValidator; |
28
|
24 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ContainerBuilder $container |
32
|
|
|
* @param string $contextId |
33
|
|
|
* @param string $definitionName |
34
|
|
|
* @param array $config |
35
|
|
|
* |
36
|
|
|
* @return Definition |
37
|
|
|
* |
38
|
|
|
* @throws InvalidDefinitionException |
39
|
|
|
*/ |
40
|
23 |
|
public function buildDefinition( |
41
|
|
|
ContainerBuilder $container, |
42
|
|
|
$contextId, |
43
|
|
|
$definitionName, |
44
|
|
|
array &$config |
45
|
|
|
) { |
46
|
23 |
|
$this->parameterValidator->validateParameters($contextId, $config); |
47
|
|
|
|
48
|
23 |
|
if (isset($config[self::SERVICE_PARAMETER])) { |
49
|
16 |
|
return $this->buildFromService( |
50
|
|
|
$container, |
51
|
|
|
$definitionName, |
52
|
16 |
|
$config[self::SERVICE_PARAMETER] |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
17 |
|
return $this->buildFromClass( |
57
|
|
|
$container, |
58
|
|
|
$definitionName, |
59
|
17 |
|
$config[self::CLASS_PARAMETER] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ContainerBuilder $container |
65
|
|
|
* @param string $definitionName |
66
|
|
|
* @param string $oldServiceName |
67
|
|
|
* |
68
|
|
|
* @return Definition |
69
|
|
|
*/ |
70
|
16 |
|
private function buildFromService( |
71
|
|
|
ContainerBuilder $container, |
72
|
|
|
$definitionName, |
73
|
|
|
$oldServiceName |
74
|
|
|
) { |
75
|
16 |
|
if (!$container->hasDefinition($oldServiceName)) { |
76
|
2 |
|
throw new InvalidDefinitionException(sprintf( |
77
|
2 |
|
'Could not create "%s" service, because configured service "%s" does not exist.', |
78
|
|
|
$definitionName, |
79
|
|
|
$oldServiceName |
80
|
|
|
)); |
81
|
|
|
} |
82
|
|
|
|
83
|
14 |
|
return $container->setDefinition( |
84
|
|
|
$definitionName, |
85
|
14 |
|
$container->getDefinition($oldServiceName) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param ContainerBuilder $container |
91
|
|
|
* @param string $definitionName |
92
|
|
|
* @param string $className |
93
|
|
|
* |
94
|
|
|
* @return Definition |
95
|
|
|
*/ |
96
|
17 |
|
private function buildFromClass( |
97
|
|
|
ContainerBuilder $container, |
98
|
|
|
$definitionName, |
99
|
|
|
$className |
100
|
|
|
) { |
101
|
17 |
|
if (!class_exists($className)) { |
102
|
1 |
|
throw new InvalidDefinitionException(sprintf( |
103
|
1 |
|
'Could not create service "%s", because class "%s" does not exist.', |
104
|
|
|
$definitionName, |
105
|
|
|
$className |
106
|
|
|
)); |
107
|
|
|
} |
108
|
|
|
|
109
|
16 |
|
return $container->setDefinition( |
110
|
|
|
$definitionName, |
111
|
16 |
|
new Definition($className) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|