@@ -187,10 +187,11 @@ private function getHashMapAnnotationData(\ReflectionProperty $property)
187187 * Returns meta field annotation data from reader.
188188 *
189189 * @param \ReflectionProperty $property
190+ * @param string $directory The name of the Document directory in the bundle
190191 *
191192 * @return array
192193 */
193- private function getMetaFieldAnnotationData ($ property )
194+ private function getMetaFieldAnnotationData ($ property, $ directory )
194195 {
195196 /** @var MetaField $annotation */
196197 $ annotation = $ this ->reader ->getPropertyAnnotation ($ property , self ::ID_ANNOTATION );
@@ -208,7 +209,7 @@ private function getMetaFieldAnnotationData($property)
208209 ];
209210
210211 if ($ annotation instanceof ParentDocument) {
211- $ data ['settings ' ]['type ' ] = $ this ->getDocumentType ($ annotation ->class );
212+ $ data ['settings ' ]['type ' ] = $ this ->getDocumentType ($ annotation ->class , $ directory );
212213 }
213214
214215 return $ data ;
@@ -235,6 +236,7 @@ private function getObjects()
235236 private function getAliases (\ReflectionClass $ reflectionClass , array &$ metaFields = null )
236237 {
237238 $ reflectionName = $ reflectionClass ->getName ();
239+ $ directory = $ this ->guessDirName ($ reflectionClass );
238240
239241 // We skip cache in case $metaFields is given. This should not affect performance
240242 // because for each document this method is called only once. For objects it might
@@ -254,7 +256,7 @@ private function getAliases(\ReflectionClass $reflectionClass, array &$metaField
254256 $ type = $ type !== null ? $ type : $ this ->getHashMapAnnotationData ($ property );
255257
256258 if ($ type === null && $ metaFields !== null
257- && ($ metaData = $ this ->getMetaFieldAnnotationData ($ property )) !== null ) {
259+ && ($ metaData = $ this ->getMetaFieldAnnotationData ($ property, $ directory )) !== null ) {
258260 $ metaFields [$ metaData ['name ' ]] = $ metaData ['settings ' ];
259261 $ type = new \stdClass ();
260262 $ type ->name = $ metaData ['name ' ];
@@ -299,11 +301,11 @@ private function getAliases(\ReflectionClass $reflectionClass, array &$metaField
299301 $ alias [$ type ->name ]['propertyType ' ] = $ propertyType ;
300302
301303 if ($ type instanceof Embedded) {
302- $ child = new \ReflectionClass ($ this ->finder ->getNamespace ($ type ->class ));
304+ $ child = new \ReflectionClass ($ this ->finder ->getNamespace ($ type ->class , $ directory ));
303305 $ alias [$ type ->name ] = array_merge (
304306 $ alias [$ type ->name ],
305307 [
306- 'type ' => $ this ->getObjectMapping ($ type ->class )['type ' ],
308+ 'type ' => $ this ->getObjectMapping ($ type ->class , $ directory )['type ' ],
307309 'multiple ' => $ type ->multiple ,
308310 'aliases ' => $ this ->getAliases ($ child , $ metaFields ),
309311 'namespace ' => $ child ->getName (),
@@ -397,13 +399,14 @@ private function registerAnnotations()
397399 /**
398400 * Returns document type.
399401 *
400- * @param string $document Format must be like AcmeBundle:Document.
402+ * @param string $document Format must be like AcmeBundle:Document.
403+ * @param string $directory The Document directory name of the bundle.
401404 *
402405 * @return string
403406 */
404- private function getDocumentType ($ document )
407+ private function getDocumentType ($ document, $ directory )
405408 {
406- $ namespace = $ this ->finder ->getNamespace ($ document );
409+ $ namespace = $ this ->finder ->getNamespace ($ document, $ directory );
407410 $ reflectionClass = new \ReflectionClass ($ namespace );
408411 $ document = $ this ->getDocumentAnnotationData ($ reflectionClass );
409412
@@ -453,14 +456,16 @@ private function getDocumentPropertiesReflection(\ReflectionClass $reflectionCla
453456 private function getAnalyzers (\ReflectionClass $ reflectionClass )
454457 {
455458 $ analyzers = [];
459+ $ directory = $ this ->guessDirName ($ reflectionClass );
460+
456461 foreach ($ this ->getDocumentPropertiesReflection ($ reflectionClass ) as $ name => $ property ) {
457462 $ type = $ this ->getPropertyAnnotationData ($ property );
458463 $ type = $ type !== null ? $ type : $ this ->getEmbeddedAnnotationData ($ property );
459464
460465 if ($ type instanceof Embedded) {
461466 $ analyzers = array_merge (
462467 $ analyzers ,
463- $ this ->getAnalyzers (new \ReflectionClass ($ this ->finder ->getNamespace ($ type ->class )))
468+ $ this ->getAnalyzers (new \ReflectionClass ($ this ->finder ->getNamespace ($ type ->class , $ directory )))
464469 );
465470 }
466471
@@ -499,6 +504,8 @@ private function getAnalyzers(\ReflectionClass $reflectionClass)
499504 private function getProperties (\ReflectionClass $ reflectionClass , $ properties = [], $ flag = false )
500505 {
501506 $ mapping = [];
507+ $ directory = $ this ->guessDirName ($ reflectionClass );
508+
502509 /** @var \ReflectionProperty $property */
503510 foreach ($ this ->getDocumentPropertiesReflection ($ reflectionClass ) as $ name => $ property ) {
504511 $ type = $ this ->getPropertyAnnotationData ($ property );
@@ -516,7 +523,7 @@ private function getProperties(\ReflectionClass $reflectionClass, $properties =
516523
517524 // Inner object
518525 if ($ type instanceof Embedded) {
519- $ map = array_replace_recursive ($ map , $ this ->getObjectMapping ($ type ->class ));
526+ $ map = array_replace_recursive ($ map , $ this ->getObjectMapping ($ type ->class , $ directory ));
520527 }
521528
522529 // HashMap object
@@ -546,12 +553,13 @@ private function getProperties(\ReflectionClass $reflectionClass, $properties =
546553 * Loads from cache if it's already loaded.
547554 *
548555 * @param string $className
556+ * @param string $directory Name of the directory where the Document is
549557 *
550558 * @return array
551559 */
552- private function getObjectMapping ($ className )
560+ private function getObjectMapping ($ className, $ directory )
553561 {
554- $ namespace = $ this ->finder ->getNamespace ($ className );
562+ $ namespace = $ this ->finder ->getNamespace ($ className, $ directory );
555563
556564 if (array_key_exists ($ namespace , $ this ->objects )) {
557565 return $ this ->objects [$ namespace ];
@@ -582,4 +590,18 @@ private function getObjectMapping($className)
582590
583591 return $ this ->objects [$ namespace ];
584592 }
593+
594+ /**
595+ * @param \ReflectionClass $reflection
596+ *
597+ * @return string
598+ */
599+ private function guessDirName (\ReflectionClass $ reflection )
600+ {
601+ return substr (
602+ $ directory = $ reflection ->getName (),
603+ $ start = strpos ($ directory , '\\' ) + 1 ,
604+ strrpos ($ directory , '\\' ) - $ start
605+ );
606+ }
585607}
0 commit comments