1 | <?php |
||
26 | class ONGRFilterManagerExtension extends Extension |
||
27 | { |
||
28 | /** |
||
29 | * @var AbstractFilterFactory[] |
||
30 | */ |
||
31 | protected $factories = []; |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | public function load(array $configs, ContainerBuilder $container) |
||
37 | { |
||
38 | $configuration = new Configuration(); |
||
39 | $config = $this->processConfiguration($configuration, $configs); |
||
40 | |||
41 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
||
42 | $loader->load('services.yml'); |
||
43 | |||
44 | !isset($config['filters']) ? : $this->addFilters($config['filters'], $container); |
||
45 | !isset($config['managers']) ? : $this->addFilterManagers($config, $container); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Adds filter factory. |
||
50 | * |
||
51 | * @param AbstractFilterFactory $factory |
||
52 | */ |
||
53 | public function addFilterFactory(AbstractFilterFactory $factory) |
||
57 | |||
58 | /** |
||
59 | * Formats filter service id from given name. |
||
60 | * |
||
61 | * @param string $name Filter name. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public static function getFilterId($name) |
||
69 | |||
70 | /** |
||
71 | * Formats filter manager service id from given name. |
||
72 | * |
||
73 | * @param string $name Filter manager name. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public static function getFilterManagerId($name) |
||
81 | |||
82 | /** |
||
83 | * Adds filters based on configuration. |
||
84 | * |
||
85 | * @param array $config Configuration. |
||
86 | * @param ContainerBuilder $container Service container. |
||
87 | */ |
||
88 | private function addFilters(array $config, ContainerBuilder $container) |
||
89 | { |
||
90 | $this->validateFilterNames($config); |
||
91 | |||
92 | foreach ($config as $type => $filters) { |
||
93 | foreach ($filters as $name => $config) { |
||
94 | $filterDefinition = $this |
||
95 | ->getFilterFactory($type) |
||
96 | ->setConfiguration($config) |
||
97 | ->getDefinition(); |
||
98 | |||
99 | $this->addRelation($filterDefinition, $config, 'search', 'include'); |
||
100 | $this->addRelation($filterDefinition, $config, 'search', 'exclude'); |
||
101 | $this->addRelation($filterDefinition, $config, 'reset', 'include'); |
||
102 | $this->addRelation($filterDefinition, $config, 'reset', 'exclude'); |
||
103 | |||
104 | $container->setDefinition(self::getFilterId($name), $filterDefinition); |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Checks if filter names are valid. |
||
111 | * |
||
112 | * @param array $filters Filters to validate. |
||
113 | * |
||
114 | * @throws InvalidConfigurationException |
||
115 | */ |
||
116 | private function validateFilterNames(array $filters) |
||
132 | |||
133 | /** |
||
134 | * Adds filters managers based on configuration. |
||
135 | * |
||
136 | * @param array $config Configuration array. |
||
137 | * @param ContainerBuilder $container Service container. |
||
138 | */ |
||
139 | private function addFilterManagers(array $config, ContainerBuilder $container) |
||
140 | { |
||
141 | foreach ($config['managers'] as $name => $manager) { |
||
142 | $filterContainer = new Definition('ONGR\FilterManagerBundle\Search\FilterContainer'); |
||
143 | |||
144 | foreach ($manager['filters'] as $filter) { |
||
145 | $filterContainer->addMethodCall( |
||
146 | 'set', |
||
147 | [$filter, new Reference(self::getFilterId($filter))] |
||
148 | ); |
||
149 | } |
||
150 | |||
151 | $managerDefinition = new Definition( |
||
152 | 'ONGR\FilterManagerBundle\Search\FilterManager', |
||
153 | [ |
||
154 | $filterContainer, |
||
155 | new Reference($manager['repository']), |
||
156 | ] |
||
157 | ); |
||
158 | $managerDefinition->addTag('es.filter_manager'); |
||
159 | |||
160 | $container->setDefinition(self::getFilterManagerId($name), $managerDefinition); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Adds relation to filter. |
||
166 | * |
||
167 | * @param Definition $definition |
||
168 | * @param array $filter |
||
169 | * @param string $urlType |
||
170 | * @param string $relationType |
||
171 | */ |
||
172 | private function addRelation(Definition $definition, $filter, $urlType, $relationType) |
||
173 | { |
||
174 | if (!empty($filter['relations'][$urlType][$relationType])) { |
||
175 | $definition->addMethodCall( |
||
176 | 'set' . ucfirst($urlType) . 'Relation', |
||
177 | [$this->getRelation($relationType, $filter['relations'][$urlType][$relationType])] |
||
178 | ); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Creates relation definition by given parameters. |
||
184 | * |
||
185 | * @param string $type |
||
186 | * @param array $relations |
||
187 | * |
||
188 | * @return Definition |
||
189 | */ |
||
190 | private function getRelation($type, $relations) |
||
191 | { |
||
192 | return new Definition( |
||
193 | sprintf('ONGR\FilterManagerBundle\Relation\%sRelation', ucfirst($type)), |
||
194 | [$relations] |
||
195 | ); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Returns filter factory. |
||
200 | * |
||
201 | * @param string $name Factory name. |
||
202 | * |
||
203 | * @return AbstractFilterFactory |
||
204 | * |
||
205 | * @throws InvalidConfigurationException Invaid filter name request. |
||
206 | */ |
||
207 | private function getFilterFactory($name) |
||
221 | } |
||
222 |