From bc6b0e47f15d9635d7c158715a618bc7e88555c1 Mon Sep 17 00:00:00 2001 From: Evgeniy Kuzmin Date: Tue, 14 Jan 2014 00:57:31 +0200 Subject: [PATCH 1/4] Initial change to pickup parent class Inject --- Metadata/Driver/AnnotationDriver.php | 33 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index 989a809a..dd92a0b8 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -52,7 +52,7 @@ public function __construct(Reader $reader) } public function loadMetadataForClass(\ReflectionClass $class) - { + { $metadata = new ClassMetadata($className = $class->getName()); if (false !== $filename = $class->getFilename()) { $metadata->fileResources[] = $filename; @@ -122,18 +122,11 @@ public function loadMetadataForClass(\ReflectionClass $class) } $hasInjection = false; - foreach ($class->getProperties() as $property) { - if ($property->getDeclaringClass()->getName() !== $className) { - continue; - } - $name = $property->getName(); - foreach ($this->reader->getPropertyAnnotations($property) as $annot) { - if ($annot instanceof Inject) { - $hasInjection = true; - $metadata->properties[$name] = $this->convertReferenceValue($name, $annot); - } - } + $this->buildProperties($class, $metadata, $hasInjection); + + if($parentClass = $class->getParentClass()) { + $this->buildProperties($parentClass, $metadata, $hasInjection); } foreach ($class->getMethods() as $method) { @@ -226,4 +219,20 @@ private function generateId($name) return strtolower(strtr($name, '\\', '.')); } + + private function buildProperties($class, &$metadata, &$hasInjection) { + foreach ($class->getProperties() as $property) { + if ($property->getDeclaringClass()->getName() !== $class->getName()) { + continue; + } + $name = $property->getName(); + + foreach ($this->reader->getPropertyAnnotations($property) as $annot) { + if ($annot instanceof Inject) { + $hasInjection = true; + $metadata->properties[$name] = $this->convertReferenceValue($name, $annot); + } + } + } + } } From e7402db2fffb1d2a6914af934b2fe44539bf6445 Mon Sep 17 00:00:00 2001 From: Evgeniy Kuzmin Date: Tue, 14 Jan 2014 01:08:11 +0200 Subject: [PATCH 2/4] change classes lookup direction: first - parent, last - child --- Metadata/Driver/AnnotationDriver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index dd92a0b8..c912ffd6 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -52,7 +52,7 @@ public function __construct(Reader $reader) } public function loadMetadataForClass(\ReflectionClass $class) - { + { $metadata = new ClassMetadata($className = $class->getName()); if (false !== $filename = $class->getFilename()) { $metadata->fileResources[] = $filename; @@ -122,12 +122,12 @@ public function loadMetadataForClass(\ReflectionClass $class) } $hasInjection = false; - - $this->buildProperties($class, $metadata, $hasInjection); if($parentClass = $class->getParentClass()) { $this->buildProperties($parentClass, $metadata, $hasInjection); } + + $this->buildProperties($class, $metadata, $hasInjection); foreach ($class->getMethods() as $method) { if ($method->getDeclaringClass()->getName() !== $className) { From dee3e75053673882adf84d0d86a82c1aafcb0079 Mon Sep 17 00:00:00 2001 From: Evgeniy Kuzmin Date: Tue, 14 Jan 2014 01:34:31 +0200 Subject: [PATCH 3/4] separate functions for Methods and Annotations parent lookup --- Metadata/Driver/AnnotationDriver.php | 184 ++++++++++++++------------- 1 file changed, 96 insertions(+), 88 deletions(-) diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index c912ffd6..c2db5ec4 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -66,71 +66,64 @@ public function loadMetadataForClass(\ReflectionClass $class) return null; } - foreach ($this->reader->getClassAnnotations($class) as $annot) { - if ($annot instanceof Service) { - if (null === $annot->id) { - $metadata->id = $this->generateId($className); - } else { - $metadata->id = $annot->id; - } + $hasInjection = false; + + if($parentClass = $class->getParentClass()) { + $this->buildAnnotations($parentClass, $metadata); + $this->buildProperties($parentClass, $metadata, $hasInjection); + $this->buildMethods($parentClass, $metadata, $hasInjection); + } + + $this->buildAnnotations($class, $metadata); + $this->buildProperties($class, $metadata, $hasInjection); + $this->buildMethods($class, $metadata, $hasInjection); - $metadata->parent = $annot->parent; - $metadata->public = $annot->public; - $metadata->scope = $annot->scope; - $metadata->abstract = $annot->abstract; - } else if ($annot instanceof Tag) { - $metadata->tags[$annot->name][] = $annot->attributes; - } else if ($annot instanceof Validator) { - // automatically register as service if not done explicitly - if (null === $metadata->id) { - $metadata->id = $this->generateId($className); - } + if (null == $metadata->id && !$hasInjection) { + return null; + } - $metadata->tags['validator.constraint_validator'][] = array( - 'alias' => $annot->alias, - ); - } else if ($annot instanceof AbstractDoctrineListener) { - if (null === $metadata->id) { - $metadata->id = $this->generateId($className); - } + return $metadata; + } - foreach ($annot->events as $event) { - $metadata->tags[$annot->getTag()][] = array( - 'event' => $event, - 'connection' => $annot->connection ?: 'default', - 'lazy' => $annot->lazy, - 'priority' => $annot->priority, - ); - } - } else if ($annot instanceof FormType) { - if (null === $metadata->id) { - $metadata->id = $this->generateId($className); - } + private function convertReferenceValue($name, AnnotReference $annot) + { + if (null === $annot->value) { + return new Reference($this->generateId($name), false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict); + } - $alias = $annot->alias; + if (false === strpos($annot->value, '%')) { + return new Reference($annot->value, false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict); + } - // try to extract it from the class itself - if (null === $alias) { - $instance = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className)); - $alias = $instance->getName(); - } + return $annot->value; + } - $metadata->tags['form.type'][] = array( - 'alias' => $alias, - ); + private function generateId($name) + { + $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); + + return strtolower(strtr($name, '\\', '.')); + } + + private function buildProperties($class, &$metadata, &$hasInjection) { + foreach ($class->getProperties() as $property) { + if ($property->getDeclaringClass()->getName() !== $class->getName()) { + continue; } - } + $name = $property->getName(); - $hasInjection = false; - - if($parentClass = $class->getParentClass()) { - $this->buildProperties($parentClass, $metadata, $hasInjection); + foreach ($this->reader->getPropertyAnnotations($property) as $annot) { + if ($annot instanceof Inject) { + $hasInjection = true; + $metadata->properties[$name] = $this->convertReferenceValue($name, $annot); + } + } } - - $this->buildProperties($class, $metadata, $hasInjection); - + } + + private function buildMethods($class, &$metadata, &$hasInjection) { foreach ($class->getMethods() as $method) { - if ($method->getDeclaringClass()->getName() !== $className) { + if ($method->getDeclaringClass()->getName() !== $class->getName()) { continue; } $name = $method->getName(); @@ -192,46 +185,61 @@ public function loadMetadataForClass(\ReflectionClass $class) } } } - - if (null == $metadata->id && !$hasInjection) { - return null; - } - - return $metadata; } + + private function buildAnnotations($class, &$metadata) { + foreach ($this->reader->getClassAnnotations($class) as $annot) { + if ($annot instanceof Service) { + if (null === $annot->id) { + $metadata->id = $this->generateId($className); + } else { + $metadata->id = $annot->id; + } - private function convertReferenceValue($name, AnnotReference $annot) - { - if (null === $annot->value) { - return new Reference($this->generateId($name), false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict); - } - - if (false === strpos($annot->value, '%')) { - return new Reference($annot->value, false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict); - } + $metadata->parent = $annot->parent; + $metadata->public = $annot->public; + $metadata->scope = $annot->scope; + $metadata->abstract = $annot->abstract; + } else if ($annot instanceof Tag) { + $metadata->tags[$annot->name][] = $annot->attributes; + } else if ($annot instanceof Validator) { + // automatically register as service if not done explicitly + if (null === $metadata->id) { + $metadata->id = $this->generateId($className); + } - return $annot->value; - } + $metadata->tags['validator.constraint_validator'][] = array( + 'alias' => $annot->alias, + ); + } else if ($annot instanceof AbstractDoctrineListener) { + if (null === $metadata->id) { + $metadata->id = $this->generateId($className); + } - private function generateId($name) - { - $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); + foreach ($annot->events as $event) { + $metadata->tags[$annot->getTag()][] = array( + 'event' => $event, + 'connection' => $annot->connection ?: 'default', + 'lazy' => $annot->lazy, + 'priority' => $annot->priority, + ); + } + } else if ($annot instanceof FormType) { + if (null === $metadata->id) { + $metadata->id = $this->generateId($className); + } - return strtolower(strtr($name, '\\', '.')); - } - - private function buildProperties($class, &$metadata, &$hasInjection) { - foreach ($class->getProperties() as $property) { - if ($property->getDeclaringClass()->getName() !== $class->getName()) { - continue; - } - $name = $property->getName(); + $alias = $annot->alias; - foreach ($this->reader->getPropertyAnnotations($property) as $annot) { - if ($annot instanceof Inject) { - $hasInjection = true; - $metadata->properties[$name] = $this->convertReferenceValue($name, $annot); + // try to extract it from the class itself + if (null === $alias) { + $instance = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className)); + $alias = $instance->getName(); } + + $metadata->tags['form.type'][] = array( + 'alias' => $alias, + ); } } } From 6e8ec89c41ec1493e3dd2ce152d7f45bd02404c4 Mon Sep 17 00:00:00 2001 From: Evgeniy Kuzmin Date: Tue, 14 Jan 2014 10:08:51 +0200 Subject: [PATCH 4/4] fixing test failing --- Metadata/Driver/AnnotationDriver.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index c2db5ec4..38b167db 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -71,7 +71,9 @@ public function loadMetadataForClass(\ReflectionClass $class) if($parentClass = $class->getParentClass()) { $this->buildAnnotations($parentClass, $metadata); $this->buildProperties($parentClass, $metadata, $hasInjection); - $this->buildMethods($parentClass, $metadata, $hasInjection); + // temp disabledavoid test failing, only child lookup for now + // @todo fix test failing Cannot redeclare class EnhancedProxy_...SecuredController in EnhancedProxy___CG__-JMS-DiExtraBundle-Tests-Functional-Bundle-TestBundle-Controller-SecuredController.php on line 11 + //$this->buildMethods($parentClass, $metadata, $hasInjection); } $this->buildAnnotations($class, $metadata); @@ -188,6 +190,7 @@ private function buildMethods($class, &$metadata, &$hasInjection) { } private function buildAnnotations($class, &$metadata) { + $className = $class->getName(); foreach ($this->reader->getClassAnnotations($class) as $annot) { if ($annot instanceof Service) { if (null === $annot->id) {