|
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
|
23 |
|
public function __construct(ParametersValidator $parameterValidator) |
|
26
|
|
|
{ |
|
27
|
23 |
|
$this->parameterValidator = $parameterValidator; |
|
28
|
23 |
|
} |
|
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
|
22 |
|
public function buildDefinition( |
|
41
|
|
|
ContainerBuilder $container, |
|
42
|
|
|
$contextId, |
|
43
|
|
|
$definitionName, |
|
44
|
|
|
array &$config |
|
45
|
|
|
) { |
|
46
|
22 |
|
$this->parameterValidator->validateParameters($contextId, $config); |
|
47
|
|
|
|
|
48
|
22 |
|
if (isset($config[self::SERVICE_PARAMETER])) { |
|
49
|
15 |
|
return $this->buildFromService( |
|
50
|
|
|
$container, |
|
51
|
|
|
$definitionName, |
|
52
|
15 |
|
$config[self::SERVICE_PARAMETER] |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
16 |
|
return $this->buildFromClass( |
|
57
|
|
|
$container, |
|
58
|
|
|
$definitionName, |
|
59
|
16 |
|
$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
|
15 |
|
private function buildFromService( |
|
71
|
|
|
ContainerBuilder $container, |
|
72
|
|
|
$definitionName, |
|
73
|
|
|
$oldServiceName |
|
74
|
|
|
) { |
|
75
|
15 |
|
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
|
13 |
|
return $container->setDefinition( |
|
84
|
|
|
$definitionName, |
|
85
|
13 |
|
$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
|
16 |
|
private function buildFromClass( |
|
97
|
|
|
ContainerBuilder $container, |
|
98
|
|
|
$definitionName, |
|
99
|
|
|
$className |
|
100
|
|
|
) { |
|
101
|
16 |
|
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
|
15 |
|
return $container->setDefinition( |
|
110
|
|
|
$definitionName, |
|
111
|
15 |
|
new Definition($className) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|