1 | <?php |
||
21 | class MetadataCollector |
||
22 | { |
||
23 | /** |
||
24 | * @var DocumentFinder |
||
25 | */ |
||
26 | private $finder; |
||
27 | |||
28 | /** |
||
29 | * @var DocumentParser |
||
30 | */ |
||
31 | private $parser; |
||
32 | |||
33 | /** |
||
34 | * @var CacheProvider |
||
35 | */ |
||
36 | private $cache = null; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | private $enableCache = false; |
||
42 | |||
43 | /** |
||
44 | * Bundles mappings local cache container. Could be stored as the whole bundle or as single document. |
||
45 | * e.g. AcmeDemoBundle, AcmeDemoBundle:Product. |
||
46 | * |
||
47 | * @var mixed |
||
48 | */ |
||
49 | private $mappings = []; |
||
50 | |||
51 | /** |
||
52 | * @param DocumentFinder $finder For finding documents. |
||
53 | * @param DocumentParser $parser For reading document annotations. |
||
54 | * @param CacheProvider $cache Cache provider to store the meta data for later use. |
||
55 | */ |
||
56 | public function __construct($finder, $parser, $cache = null) |
||
66 | |||
67 | /** |
||
68 | * Enables metadata caching. |
||
69 | * |
||
70 | * @param bool $enableCache |
||
71 | */ |
||
72 | public function setEnableCache($enableCache) |
||
76 | |||
77 | /** |
||
78 | * Fetches bundles mapping from documents. |
||
79 | * |
||
80 | * @param string[] $bundles Elasticsearch manager config. You can get bundles list from 'mappings' node. |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getMappings(array $bundles) |
||
103 | |||
104 | /** |
||
105 | * Searches for documents in the bundle and tries to read them. |
||
106 | * |
||
107 | * @param string $name |
||
108 | * |
||
109 | * @return array Empty array on containing zero documents. |
||
110 | */ |
||
111 | public function getBundleMapping($name) |
||
112 | { |
||
113 | if (!is_string($name)) { |
||
114 | throw new \LogicException('getBundleMapping() in the Metadata collector expects a string argument only!'); |
||
115 | } |
||
116 | |||
117 | if (isset($this->mappings[$name])) { |
||
118 | return $this->mappings[$name]; |
||
119 | } |
||
120 | |||
121 | // Handle the case when single document mapping requested |
||
122 | if (strpos($name, ':') !== false) { |
||
123 | list($bundle, $documentClass) = explode(':', $name); |
||
124 | $documents = $this->finder->getBundleDocumentClasses($bundle); |
||
125 | $documents = in_array($documentClass, $documents) ? [$documentClass] : []; |
||
126 | } else { |
||
127 | $documents = $this->finder->getBundleDocumentClasses($name); |
||
128 | $bundle = $name; |
||
129 | } |
||
130 | |||
131 | $mappings = []; |
||
132 | $bundleNamespace = $this->finder->getBundleClass($bundle); |
||
133 | $bundleNamespace = substr($bundleNamespace, 0, strrpos($bundleNamespace, '\\')); |
||
134 | |||
135 | if (!count($documents)) { |
||
136 | return []; |
||
137 | } |
||
138 | |||
139 | // Loop through documents found in bundle. |
||
140 | foreach ($documents as $document) { |
||
141 | $documentReflection = new \ReflectionClass( |
||
142 | $bundleNamespace . |
||
143 | '\\' . DocumentFinder::DOCUMENT_DIR . |
||
144 | '\\' . $document |
||
145 | ); |
||
146 | |||
147 | try { |
||
148 | $documentMapping = $this->getDocumentReflectionMapping($documentReflection); |
||
149 | } catch (MissingDocumentAnnotationException $exception) { |
||
150 | // Not a document, just ignore |
||
151 | continue; |
||
152 | } |
||
153 | |||
154 | if (!array_key_exists($documentMapping['type'], $mappings)) { |
||
155 | $documentMapping['bundle'] = $bundle; |
||
156 | $mappings = array_merge($mappings, [$documentMapping['type'] => $documentMapping]); |
||
157 | } else { |
||
158 | throw new \LogicException( |
||
159 | $bundle . ' has 2 same type names defined in the documents. ' . |
||
160 | 'Type names must be unique!' |
||
161 | ); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | $this->cacheBundle($name, $mappings); |
||
166 | |||
167 | return $mappings; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param array $manager |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getManagerTypes($manager) |
||
181 | |||
182 | /** |
||
183 | * Resolves Elasticsearch type by document class. |
||
184 | * |
||
185 | * @param string $className FQCN or string in AppBundle:Document format |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getDocumentType($className) |
||
190 | { |
||
191 | $mapping = $this->getMapping($className); |
||
192 | |||
193 | return $mapping['type']; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Retrieves prepared mapping to sent to the elasticsearch client. |
||
198 | * |
||
199 | * @param array $bundles Manager config. |
||
200 | * |
||
201 | * @return array|null |
||
202 | */ |
||
203 | public function getClientMapping(array $bundles) |
||
227 | |||
228 | /** |
||
229 | * Prepares analysis node for Elasticsearch client. |
||
230 | * |
||
231 | * @param array $bundles |
||
232 | * @param array $analysisConfig |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | public function getClientAnalysis(array $bundles, $analysisConfig = []) |
||
277 | |||
278 | /** |
||
279 | * Prepares analysis node content for Elasticsearch client. |
||
280 | * |
||
281 | * @param string $type Node type: filter, tokenizer or char_filter |
||
282 | * @param array $analyzer Analyzer from which used helpers will be extracted. |
||
283 | * @param array $analysisConfig Pre configured analyzers container |
||
284 | * @param array $container Current analysis container where prepared helpers will be appended. |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | private function getAnalysisNodeConfiguration($type, $analyzer, $analysisConfig, $container = []) |
||
305 | |||
306 | /** |
||
307 | * Gathers annotation data from class. |
||
308 | * |
||
309 | * @param \ReflectionClass $reflectionClass Document reflection class to read mapping from. |
||
310 | * |
||
311 | * @return array |
||
312 | * @throws DocumentParserException |
||
313 | */ |
||
314 | private function getDocumentReflectionMapping(\ReflectionClass $reflectionClass) |
||
318 | |||
319 | /** |
||
320 | * Returns single document mapping metadata. |
||
321 | * |
||
322 | * @param string $namespace Document namespace |
||
323 | * |
||
324 | * @return array |
||
325 | * @throws DocumentParserException |
||
326 | */ |
||
327 | public function getMapping($namespace) |
||
340 | |||
341 | /** |
||
342 | * Adds metadata information to the cache storage. |
||
343 | * |
||
344 | * @param string $bundle |
||
345 | * @param array $mapping |
||
346 | */ |
||
347 | private function cacheBundle($bundle, array $mapping) |
||
354 | |||
355 | /** |
||
356 | * Returns fully qualified class name. |
||
357 | * |
||
358 | * @param string $className |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | public function getClassName($className) |
||
366 | } |
||
367 |