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 ParametersValidator |
27
|
|
|
*/ |
28
|
|
|
private $parametersValidator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $servicePrefix; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ParametersValidator $parametersValidator |
37
|
|
|
* @param string $servicePrefix |
38
|
|
|
*/ |
39
|
19 |
|
public function __construct( |
40
|
|
|
ParametersValidator $parametersValidator, |
41
|
|
|
$servicePrefix |
42
|
|
|
) { |
43
|
19 |
|
$this->parametersValidator = $parametersValidator; |
44
|
19 |
|
$this->servicePrefix = $servicePrefix; |
45
|
19 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
18 |
|
public function process(ContainerBuilder $container) |
51
|
|
|
{ |
52
|
18 |
|
$contextParam = 'k_gzocha_searcher.contexts'; |
53
|
18 |
|
$contexts = $container->getParameter($contextParam); |
54
|
|
|
|
55
|
18 |
|
foreach ($contexts as $contextId => &$context) { |
56
|
18 |
|
$this->processContext($contextId, $context, $container); |
57
|
|
|
} |
58
|
17 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $contextId |
62
|
|
|
* @param array $context |
63
|
|
|
* @param ContainerBuilder $container |
64
|
|
|
*/ |
65
|
|
|
abstract protected function processContext( |
66
|
|
|
$contextId, |
67
|
|
|
array &$context, |
68
|
|
|
ContainerBuilder $container |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $contextId |
73
|
|
|
* @param string $name |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
17 |
|
protected function buildServiceName($contextId, $name) |
78
|
|
|
{ |
79
|
17 |
|
return sprintf( |
80
|
17 |
|
'%s.%s.%s', |
81
|
17 |
|
$this->servicePrefix, |
82
|
|
|
$contextId, |
83
|
|
|
$name |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $contextId |
89
|
|
|
* @param array $config |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
* |
93
|
|
|
* @throws InvalidDefinitionException |
94
|
|
|
*/ |
95
|
18 |
|
protected function validateParameters( |
96
|
|
|
$contextId, |
97
|
|
|
array &$config |
98
|
|
|
) { |
99
|
|
|
return $this |
100
|
18 |
|
->parametersValidator |
101
|
18 |
|
->validateParameters($contextId, $config); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param ContainerBuilder $container |
106
|
|
|
* @param string $contextId |
107
|
|
|
* @param string $serviceName |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
* |
111
|
|
|
* @throws InvalidDefinitionException |
112
|
|
|
*/ |
113
|
13 |
|
protected function checkIfServiceExists( |
114
|
|
|
ContainerBuilder $container, |
115
|
|
|
$contextId, |
116
|
|
|
$serviceName |
117
|
|
|
) { |
118
|
13 |
|
if ($container->hasDefinition($serviceName)) { |
119
|
12 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
throw new InvalidDefinitionException(sprintf( |
123
|
1 |
|
'Service "%s" configured in searching context "%s" does not exist', |
124
|
|
|
$serviceName, |
125
|
|
|
$contextId |
126
|
|
|
)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|