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