1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KGzocha\Bundle\SearcherBundle\DependencyInjection\CompilerPass; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException; |
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Krzysztof Gzocha <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractCompilerPass implements CompilerPassInterface |
13
|
|
|
{ |
14
|
|
|
const CLASS_PARAMETER = 'class'; |
15
|
|
|
const SERVICE_PARAMETER = 'service'; |
16
|
|
|
const NAME_PARAMETER = 'name'; |
17
|
|
|
|
18
|
|
|
const CRITERIA_COLLECTION_PARAMETER = 'criteria_collection'; |
19
|
|
|
const BUILDER_COLLECTION_PARAMETER = 'builder_collection'; |
20
|
|
|
const CRITERIA_PARAMETER = 'criteria'; |
21
|
|
|
const CONTEXT_PARAMETER = 'context'; |
22
|
|
|
const SEARCHER_PARAMETER = 'searcher'; |
23
|
|
|
const WRAPPER_CLASS_PARAMETER = 'wrapper_class'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var DefinitionBuilder |
27
|
|
|
*/ |
28
|
|
|
private $definitionBuilder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $servicePrefix; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param DefinitionBuilder $definitionBuilder |
37
|
|
|
* @param string $servicePrefix |
38
|
|
|
*/ |
39
|
20 |
|
public function __construct(DefinitionBuilder $definitionBuilder, $servicePrefix) |
40
|
|
|
{ |
41
|
20 |
|
$this->definitionBuilder = $definitionBuilder; |
42
|
20 |
|
$this->servicePrefix = $servicePrefix; |
43
|
20 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
19 |
|
public function process(ContainerBuilder $container) |
49
|
|
|
{ |
50
|
19 |
|
$contextParam = 'k_gzocha_searcher.contexts'; |
51
|
19 |
|
$contexts = $container->getParameter($contextParam); |
52
|
|
|
|
53
|
19 |
|
foreach ($contexts as $contextId => &$context) { |
54
|
19 |
|
$this->processContext($contextId, $context, $container); |
55
|
|
|
} |
56
|
18 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $contextId |
60
|
|
|
* @param array $context |
61
|
|
|
* @param ContainerBuilder $container |
62
|
|
|
*/ |
63
|
|
|
abstract protected function processContext( |
64
|
|
|
$contextId, |
65
|
|
|
array &$context, |
66
|
|
|
ContainerBuilder $container |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $contextId |
71
|
|
|
* @param string $name |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
19 |
|
protected function buildServiceName($contextId, $name) |
76
|
|
|
{ |
77
|
19 |
|
return sprintf( |
78
|
19 |
|
'%s.%s.%s', |
79
|
19 |
|
$this->servicePrefix, |
80
|
|
|
$contextId, |
81
|
|
|
$name |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ContainerBuilder $container |
87
|
|
|
* @param string $contextId |
88
|
|
|
* @param string $definitionName |
89
|
|
|
* @param array $config |
90
|
|
|
* |
91
|
|
|
* @return \Symfony\Component\DependencyInjection\Definition |
92
|
|
|
*/ |
93
|
19 |
|
protected function buildDefinition( |
94
|
|
|
ContainerBuilder $container, |
95
|
|
|
$contextId, |
96
|
|
|
$definitionName, |
97
|
|
|
array &$config |
98
|
|
|
) { |
99
|
|
|
return $this |
100
|
19 |
|
->definitionBuilder |
101
|
19 |
|
->buildDefinition( |
102
|
|
|
$container, |
103
|
|
|
$contextId, |
104
|
|
|
$definitionName, |
105
|
|
|
$config |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param ContainerBuilder $container |
111
|
|
|
* @param string $contextId |
112
|
|
|
* @param string $serviceName |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
* |
116
|
|
|
* @throws InvalidDefinitionException |
117
|
|
|
*/ |
118
|
2 |
|
protected function checkIfServiceExists( |
119
|
|
|
ContainerBuilder $container, |
120
|
|
|
$contextId, |
121
|
|
|
$serviceName |
122
|
|
|
) { |
123
|
2 |
|
if (!$container->hasDefinition($serviceName)) { |
124
|
1 |
|
throw new InvalidDefinitionException(sprintf( |
125
|
1 |
|
'Service "%s" configured in searching context "%s" does not exist', |
126
|
|
|
$serviceName, |
127
|
|
|
$contextId |
128
|
|
|
)); |
129
|
|
|
} |
130
|
1 |
|
} |
131
|
|
|
} |
132
|
|
|
|