1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KGzocha\Bundle\SearcherBundle\DependencyInjection\CompilerPass; |
4
|
|
|
|
5
|
|
|
use KGzocha\Bundle\SearcherBundle\DependencyInjection\Configuration; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Krzysztof Gzocha <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class CellCompilerPass extends AbstractChainsCompilerPass |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritDoc |
16
|
|
|
*/ |
17
|
2 |
View Code Duplication |
protected function processParam( |
18
|
|
|
$contextId, |
19
|
|
|
array &$paramConfig, |
20
|
|
|
ContainerBuilder $container |
21
|
|
|
) { |
22
|
2 |
|
$cellCollection = $container->getDefinition($this->buildChainServiceName( |
23
|
|
|
$contextId, |
24
|
2 |
|
CellCollectionCompilerPass::CELL_COLLECTION |
25
|
|
|
)); |
26
|
|
|
|
27
|
2 |
|
foreach ($paramConfig['cells'] as &$cellConfig) { |
28
|
2 |
|
$cellDefinition = $this->processCell($contextId, $cellConfig, $container); |
29
|
|
|
|
30
|
2 |
|
$cellCollection->addMethodCall( |
31
|
2 |
|
'addNamedCell', |
32
|
2 |
|
[$cellConfig[self::NAME_PARAMETER], $cellDefinition] |
33
|
|
|
); |
34
|
|
|
} |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $contextId |
39
|
|
|
* @param array $cellConfig |
40
|
|
|
* @param ContainerBuilder $container |
41
|
|
|
* |
42
|
|
|
* @return Definition |
43
|
|
|
*/ |
44
|
2 |
|
private function processCell($contextId, array &$cellConfig, ContainerBuilder $container) |
45
|
|
|
{ |
46
|
2 |
|
$definitionName = $this->buildChainServiceName( |
47
|
|
|
$contextId, |
48
|
2 |
|
sprintf('cell.%s', $cellConfig[self::NAME_PARAMETER]) |
49
|
|
|
); |
50
|
|
|
|
51
|
2 |
|
$definition = $this->buildDefinition( |
52
|
|
|
$container, |
53
|
|
|
$contextId, |
54
|
|
|
$definitionName, |
55
|
|
|
$cellConfig |
56
|
|
|
); |
57
|
|
|
|
58
|
2 |
|
if ($cellConfig[self::SERVICE_PARAMETER]) { |
59
|
1 |
|
return $definition; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
return $this->configureDefinition($contextId, $cellConfig, $container, $definition); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ContainerBuilder $container |
67
|
|
|
* @param string $contextId |
68
|
|
|
* @param array $cellConfig |
69
|
|
|
* |
70
|
|
|
* @return Definition |
71
|
|
|
*/ |
72
|
2 |
|
private function getTransformerDefinition( |
73
|
|
|
ContainerBuilder $container, |
74
|
|
|
$contextId, |
75
|
|
|
array &$cellConfig |
76
|
|
|
) { |
77
|
2 |
|
if (!$cellConfig[self::TRANSFORMER_PARAMETER]) { |
78
|
1 |
|
return new Definition(Configuration::END_TRANSFORMER_CLASS); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
return $container->getDefinition($this->buildChainServiceName( |
82
|
|
|
$contextId, |
83
|
|
|
sprintf( |
84
|
2 |
|
'%s.%s', |
85
|
2 |
|
self::TRANSFORMER_PARAMETER, |
86
|
2 |
|
$cellConfig[self::TRANSFORMER_PARAMETER] |
87
|
|
|
) |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $contextId |
93
|
|
|
* @param array $cellConfig |
94
|
|
|
* @param ContainerBuilder $container |
95
|
|
|
* @param Definition $definition |
96
|
|
|
* |
97
|
|
|
* @return Definition |
98
|
|
|
*/ |
99
|
2 |
|
private function configureDefinition( |
100
|
|
|
$contextId, |
101
|
|
|
array &$cellConfig, |
102
|
|
|
ContainerBuilder $container, |
103
|
|
|
Definition $definition |
104
|
|
|
) { |
105
|
|
|
// Add searcher as first argument |
106
|
2 |
|
$definition->addArgument($container->getDefinition( |
107
|
2 |
|
$this->buildServiceName($cellConfig[self::SEARCHER_PARAMETER], self::SEARCHER_PARAMETER) |
108
|
|
|
)); |
109
|
|
|
|
110
|
|
|
// Add transformer as second argument |
111
|
2 |
|
$definition->addArgument($this->getTransformerDefinition( |
112
|
|
|
$container, $contextId, $cellConfig |
113
|
|
|
)); |
114
|
|
|
|
115
|
2 |
|
return $definition; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|