From 4533b8ae06b100d052e014354a410484a29b6c61 Mon Sep 17 00:00:00 2001 From: wodka Date: Mon, 15 Feb 2016 09:59:05 +0100 Subject: [PATCH] improve logic --- Exception/InvalidParentException.php | 9 ++++++ Metadata/ClassMetadata.php | 18 ++++++++---- Metadata/Driver/AnnotationDriver.php | 28 ++++++++++++------ Metadata/MetadataConverter.php | 29 ++++++++++++------- .../Metadata/Driver/AnnotationDriverTest.php | 2 +- 5 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 Exception/InvalidParentException.php diff --git a/Exception/InvalidParentException.php b/Exception/InvalidParentException.php new file mode 100644 index 0000000..ce0b89d --- /dev/null +++ b/Exception/InvalidParentException.php @@ -0,0 +1,9 @@ +id)) { $this->id = $service['id']; - $this->parent = $service['parent']; - $this->public = $service['public']; - $this->scope = $service['scope']; - $this->abstract = $service['abstract']; + $this->parent = @$service['parent']; + $this->public = @$service['public']; + $this->scope = @$service['scope']; + $this->abstract = @$service['abstract']; // TODO update call for other tags (there are several pull requests) } $this->services[$service['id']] = $service; } + /** + * @return bool + */ + public function hasServices() + { + return !empty($this->services); + } + /** * get list of defined services, use fallback of original fields * diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index b75a33d..44ce7ed 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -83,21 +83,27 @@ public function loadMetadataForClass(\ReflectionClass $class) $service['public'] = $annot->public; $service['scope'] = $annot->scope; $service['abstract'] = $annot->abstract; - $metadata->addService($service); + + // remove all empty values from service definition + $metadata->addService(array_filter($service)); } 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->namingStrategy->classToServiceName($className); + if (!$metadata->hasServices()) { + $metadata->addService(array( + 'id' => $this->namingStrategy->classToServiceName($className) + )); } $metadata->tags['validator.constraint_validator'][] = array( 'alias' => $annot->alias, ); } else if ($annot instanceof AbstractDoctrineListener) { - if (null === $metadata->id) { - $metadata->id = $this->namingStrategy->classToServiceName($className); + if (!$metadata->hasServices()) { + $metadata->addService(array( + 'id' => $this->namingStrategy->classToServiceName($className) + )); } foreach ($annot->events as $event) { @@ -109,8 +115,10 @@ public function loadMetadataForClass(\ReflectionClass $class) ); } } else if ($annot instanceof FormType) { - if (null === $metadata->id) { - $metadata->id = $this->namingStrategy->classToServiceName($className); + if (!$metadata->hasServices()) { + $metadata->addService(array( + 'id' => $this->namingStrategy->classToServiceName($className) + )); } $alias = $annot->alias; @@ -125,8 +133,10 @@ public function loadMetadataForClass(\ReflectionClass $class) 'alias' => $alias, ); } else if ($annot instanceof MetadataProcessorInterface) { - if (null === $metadata->id) { - $metadata->id = $this->namingStrategy->classToServiceName($className); + if (!$metadata->hasServices()) { + $metadata->addService(array( + 'id' => $this->namingStrategy->classToServiceName($className) + )); } $annot->processMetadata($metadata); diff --git a/Metadata/MetadataConverter.php b/Metadata/MetadataConverter.php index c1a20db..88c1a29 100644 --- a/Metadata/MetadataConverter.php +++ b/Metadata/MetadataConverter.php @@ -18,6 +18,7 @@ namespace JMS\DiExtraBundle\Metadata; +use JMS\DiExtraBundle\Exception\InvalidParentException; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\DefinitionDecorator; use Symfony\Component\DependencyInjection\Definition; @@ -36,27 +37,35 @@ public function convert(ClassHierarchyMetadata $metadata) static $count = 0; $definitions = array(); + /** @var ClassMetadata $previous */ $previous = null; /** @var ClassMetadata $classMetadata */ foreach ($metadata->classMetadata as $classMetadata) { foreach ($classMetadata->getServices() as $service) { - if (null === $previous && null === @$service['parent']) { + if (null === $previous && empty($service['parent'])) { $definition = new Definition(); } else { + if (empty($service['parent']) && sizeof($previous->getServices()) > 1) { + throw new InvalidParentException('there are multiple services on '.$classMetadata->name); + } + $definition = new DefinitionDecorator( @$service['parent'] ?: $previous->id ); } $definition->setClass($classMetadata->name); - if (null !== @$service['scope']) { - $definition->setScope(@$service['scope']); + if (!empty($service['scope'])) { + if (!method_exists($definition, 'setScope')) { + throw new \RuntimeException('service scopes are not available on your Symfony version.'); + } + $definition->setScope($service['scope']); } - if (null !== @$service['public']) { - $definition->setPublic(@$service['public']); + if (!empty($service['public'])) { + $definition->setPublic($service['public']); } - if (null !== @$service['abstract']) { - $definition->setAbstract(@$service['abstract']); + if (!empty($service['abstract'])) { + $definition->setAbstract($service['abstract']); } if (null !== $classMetadata->arguments) { $definition->setArguments($classMetadata->arguments); @@ -66,15 +75,15 @@ public function convert(ClassHierarchyMetadata $metadata) $definition->setTags($classMetadata->tags); $definition->setProperties($classMetadata->properties); - if (null === @$service['id']) { - $classMetadata->id = '_jms_di_extra.unnamed.service_' . $count++; + if (empty($service['id'])) { + $service['id'] = '_jms_di_extra.unnamed.service_' . $count++; } if ($classMetadata->initMethod) { throw new \RuntimeException(sprintf('You can\'t use @AfterSetup on a service.')); } - $definitions[$classMetadata->id] = $definition; + $definitions[$service['id']] = $definition; } $previous = $classMetadata; diff --git a/Tests/Metadata/Driver/AnnotationDriverTest.php b/Tests/Metadata/Driver/AnnotationDriverTest.php index 545fad4..fca0772 100644 --- a/Tests/Metadata/Driver/AnnotationDriverTest.php +++ b/Tests/Metadata/Driver/AnnotationDriverTest.php @@ -52,7 +52,7 @@ public function testMultiService() 'id' => 'first.service', ), 'second.service' => array( - 'id' => 'first.service', + 'id' => 'second.service', ) ), $metadata->getServices()); }