@@ -40,14 +40,6 @@ class MetadataCollector
4040 */
4141 private $ enableCache = false ;
4242
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-
5143 /**
5244 * @param DocumentFinder $finder For finding documents.
5345 * @param DocumentParser $parser For reading document annotations.
@@ -58,10 +50,6 @@ public function __construct($finder, $parser, $cache = null)
5850 $ this ->finder = $ finder ;
5951 $ this ->parser = $ parser ;
6052 $ this ->cache = $ cache ;
61-
62- if ($ this ->cache ) {
63- $ this ->mappings = $ this ->cache ->fetch ('ongr.metadata.mappings ' );
64- }
6553 }
6654
6755 /**
@@ -83,8 +71,13 @@ public function setEnableCache($enableCache)
8371 public function getMappings (array $ bundles )
8472 {
8573 $ output = [];
86- foreach ($ bundles as $ bundle ) {
87- $ mappings = $ this ->getBundleMapping ($ bundle );
74+ foreach ($ bundles as $ name => $ bundleConfig ) {
75+ // Backward compatibility hack for support.
76+ if (!is_array ($ bundleConfig )) {
77+ $ name = $ bundleConfig ;
78+ $ bundleConfig = [];
79+ }
80+ $ mappings = $ this ->getBundleMapping ($ name , $ bundleConfig );
8881
8982 $ alreadyDefinedTypes = array_intersect_key ($ mappings , $ output );
9083 if (count ($ alreadyDefinedTypes )) {
@@ -105,30 +98,38 @@ public function getMappings(array $bundles)
10598 * Searches for documents in the bundle and tries to read them.
10699 *
107100 * @param string $name
101+ * @param array $config Bundle configuration
108102 *
109103 * @return array Empty array on containing zero documents.
110104 */
111- public function getBundleMapping ($ name )
105+ public function getBundleMapping ($ name, $ config = [] )
112106 {
113107 if (!is_string ($ name )) {
114108 throw new \LogicException ('getBundleMapping() in the Metadata collector expects a string argument only! ' );
115109 }
116110
117- if (isset ($ this ->mappings [$ name ])) {
118- return $ this ->mappings [$ name ];
111+ $ cacheName = 'ongr.metadata.mapping. ' . md5 ($ name .serialize ($ config ));
112+
113+ $ this ->enableCache && $ mappings = $ this ->cache ->contains ($ cacheName );
114+
115+ if (isset ($ mappings )) {
116+ return $ mappings ;
119117 }
120118
119+ $ mappings = [];
120+ $ documentDir = isset ($ config ['document_dir ' ]) ? $ config ['document_dir ' ] : DocumentFinder::DOCUMENT_DIR ;
121+
121122 // Handle the case when single document mapping requested
123+ // Usage od ":" in name is deprecated. This if is only for BC.
122124 if (strpos ($ name , ': ' ) !== false ) {
123125 list ($ bundle , $ documentClass ) = explode (': ' , $ name );
124126 $ documents = $ this ->finder ->getBundleDocumentClasses ($ bundle );
125127 $ documents = in_array ($ documentClass , $ documents ) ? [$ documentClass ] : [];
126128 } else {
127- $ documents = $ this ->finder ->getBundleDocumentClasses ($ name );
129+ $ documents = $ this ->finder ->getBundleDocumentClasses ($ name, $ documentDir );
128130 $ bundle = $ name ;
129131 }
130132
131- $ mappings = [];
132133 $ bundleNamespace = $ this ->finder ->getBundleClass ($ bundle );
133134 $ bundleNamespace = substr ($ bundleNamespace , 0 , strrpos ($ bundleNamespace , '\\' ));
134135
@@ -140,7 +141,7 @@ public function getBundleMapping($name)
140141 foreach ($ documents as $ document ) {
141142 $ documentReflection = new \ReflectionClass (
142143 $ bundleNamespace .
143- '\\' . DocumentFinder:: DOCUMENT_DIR .
144+ '\\' . $ documentDir .
144145 '\\' . $ document
145146 );
146147
@@ -162,7 +163,7 @@ public function getBundleMapping($name)
162163 }
163164 }
164165
165- $ this ->cacheBundle ( $ name , $ mappings );
166+ $ this ->enableCache && $ this -> cache -> save ( $ cacheName , $ mappings );
166167
167168 return $ mappings ;
168169 }
@@ -235,10 +236,10 @@ function ($value) {
235236 */
236237 public function getClientAnalysis (array $ bundles , $ analysisConfig = [])
237238 {
238- $ cacheName = 'ongr.metadata.analysis. ' .md5 (implode ( ' , ' , $ bundles ));
239- $ typesAnalysis = $ this ->cache ->fetch ($ cacheName );
239+ $ cacheName = 'ongr.metadata.analysis. ' .md5 (serialize ( $ bundles ));
240+ $ this -> enableCache && $ typesAnalysis = $ this ->cache ->fetch ($ cacheName );
240241
241- if ($ typesAnalysis ) {
242+ if (isset ( $ typesAnalysis) ) {
242243 return $ typesAnalysis ;
243244 }
244245
@@ -279,7 +280,7 @@ public function getClientAnalysis(array $bundles, $analysisConfig = [])
279280 }
280281 }
281282
282- $ this ->cacheBundle ($ cacheName , $ typesAnalysis );
283+ $ this ->enableCache && $ this -> cache -> save ($ cacheName , $ mappings );
283284
284285 return $ typesAnalysis ;
285286 }
@@ -335,30 +336,20 @@ private function getDocumentReflectionMapping(\ReflectionClass $reflectionClass)
335336 */
336337 public function getMapping ($ namespace )
337338 {
339+ $ cacheName = 'ongr.metadata.document. ' .md5 ($ namespace );
340+
338341 $ namespace = $ this ->getClassName ($ namespace );
342+ $ this ->enableCache && $ mapping = $ this ->cache ->fetch ($ cacheName );
339343
340- if (isset ($ this -> mappings [ $ namespace ] )) {
341- return $ this -> mappings [ $ namespace ] ;
344+ if (isset ($ mapping )) {
345+ return $ mapping ;
342346 }
343347
344348 $ mapping = $ this ->getDocumentReflectionMapping (new \ReflectionClass ($ namespace ));
345- $ this ->cacheBundle ($ namespace , $ mapping );
346349
347- return $ mapping ;
348- }
350+ $ this ->enableCache && $ this ->cache ->save ($ cacheName , $ mapping );
349351
350- /**
351- * Adds metadata information to the cache storage.
352- *
353- * @param string $bundle
354- * @param array $mapping
355- */
356- private function cacheBundle ($ bundle , array $ mapping )
357- {
358- if ($ this ->enableCache ) {
359- $ this ->mappings [$ bundle ] = $ mapping ;
360- $ this ->cache ->save ('ongr.metadata.mappings ' , $ this ->mappings );
361- }
352+ return $ mapping ;
362353 }
363354
364355 /**
0 commit comments