1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\FilterManagerBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use ONGR\FilterManagerBundle\DependencyInjection\ONGRFilterManagerExtension; |
15
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
16
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
20
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Compiles custom filters. |
25
|
|
|
*/ |
26
|
|
|
class FilterPass implements CompilerPassInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function process(ContainerBuilder $container) |
32
|
|
|
{ |
33
|
|
|
$filters = []; |
34
|
|
|
foreach ($container->findTaggedServiceIds('ongr_filter_manager.filter') as $filterId => $filterTags) { |
35
|
|
|
$tagOptions = array_shift($filterTags); |
36
|
|
|
|
37
|
|
|
if (!array_key_exists('type', $tagOptions)) { |
38
|
|
|
throw new InvalidConfigurationException( |
39
|
|
|
sprintf('Filter service `%s` must have `type` set.', $filterId) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$filters[$tagOptions['type']] = $filterId; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ($container->getParameter('ongr_filter_manager.filters') as $filterName => $filterOptions) { |
47
|
|
|
if (!array_key_exists($filterOptions['type'], $filters)) { |
48
|
|
|
throw new InvalidConfigurationException( |
49
|
|
|
"There is no `{$filterOptions['type']}` type filter registered." |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$definition = new ChildDefinition($filters[($filterOptions['type'])]); |
54
|
|
|
$definition->addMethodCall('setRequestField', [$filterOptions['request_field']]); |
55
|
|
|
$definition->addMethodCall('setDocumentField', [$filterOptions['document_field']]); |
56
|
|
|
$definition->addMethodCall('setTags', [$filterOptions['tags']]); |
57
|
|
|
$definition->addMethodCall('setOptions', [$filterOptions['options']]); |
58
|
|
|
|
59
|
|
|
$container->setDefinition(ONGRFilterManagerExtension::getFilterId($filterName), $definition); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($container->getParameter('ongr_filter_manager.managers') as $managerName => $managerOptions) { |
63
|
|
|
$filterContainer = new Definition('ONGR\FilterManagerBundle\Search\FilterContainer'); |
64
|
|
|
|
65
|
|
|
if (isset($managerOptions['filters'])) { |
66
|
|
|
foreach ($managerOptions['filters'] as $filter) { |
67
|
|
|
$filterContainer->addMethodCall( |
68
|
|
|
'set', |
69
|
|
|
[$filter, new Reference(ONGRFilterManagerExtension::getFilterId($filter))] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$managerDefinition = new Definition( |
75
|
|
|
'ONGR\FilterManagerBundle\Search\FilterManager', |
76
|
|
|
[ |
77
|
|
|
$filterContainer, |
78
|
|
|
new Reference($managerOptions['repository']), |
79
|
|
|
new Reference('event_dispatcher'), |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$container->setDefinition(ONGRFilterManagerExtension::getFilterManagerId($managerName), $managerDefinition); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|