diff --git a/src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Alias.php b/src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Alias.php new file mode 100644 index 0000000..f88f5d4 --- /dev/null +++ b/src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Alias.php @@ -0,0 +1,36 @@ + + * (c) Francis Besset + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapBundle\ServiceDefinition\Annotation; + +/** + * @Annotation + */ +class Alias extends Configuration +{ + private $value; + + public function getValue() + { + return $this->value; + } + + public function setValue($value) + { + $this->value = $value; + } + + public function getAliasName() + { + return 'alias'; + } +} diff --git a/src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Configuration.php b/src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Configuration.php index f7c4d0a..2334809 100644 --- a/src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Configuration.php +++ b/src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Configuration.php @@ -21,10 +21,10 @@ abstract class Configuration implements ConfigurationInterface { foreach ($values as $k => $v) { if (!method_exists($this, $name = 'set'.$k)) { - throw new \RuntimeException(sprintf('Unknown key "%s" for annotation "@%s".', $k, get_class($this))); + throw new \RuntimeException(sprintf('Unknown key "%s" for annotation "@%s".', $k, __CLASS__)); } $this->$name($v); } } -} \ No newline at end of file +} diff --git a/src/BeSimple/SoapBundle/ServiceDefinition/Loader/AnnotationComplexTypeLoader.php b/src/BeSimple/SoapBundle/ServiceDefinition/Loader/AnnotationComplexTypeLoader.php index 0cc7579..053aa0e 100644 --- a/src/BeSimple/SoapBundle/ServiceDefinition/Loader/AnnotationComplexTypeLoader.php +++ b/src/BeSimple/SoapBundle/ServiceDefinition/Loader/AnnotationComplexTypeLoader.php @@ -22,6 +22,7 @@ use BeSimple\SoapBundle\Util\Collection; */ class AnnotationComplexTypeLoader extends AnnotationClassLoader { + private $aliasClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\Alias'; private $complexTypeClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\ComplexType'; /** @@ -40,9 +41,14 @@ class AnnotationComplexTypeLoader extends AnnotationClassLoader throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); } - $class = new \ReflectionClass($class); - $collection = new Collection('getName', 'BeSimple\SoapBundle\ServiceDefinition\ComplexType'); + $annotations = array(); + $class = new \ReflectionClass($class); + if ($alias = $this->reader->getClassAnnotation($class, $this->aliasClass)) { + $annotations['alias'] = $alias->getValue(); + } + + $annotations['properties'] = new Collection('getName', 'BeSimple\SoapBundle\ServiceDefinition\ComplexType'); foreach ($class->getProperties() as $property) { $complexType = $this->reader->getPropertyAnnotation($property, $this->complexTypeClass); @@ -51,10 +57,10 @@ class AnnotationComplexTypeLoader extends AnnotationClassLoader $propertyComplexType->setValue($complexType->getValue()); $propertyComplexType->setNillable($complexType->isNillable()); $propertyComplexType->setName($property->getName()); - $collection->add($propertyComplexType); + $annotations['properties']->add($propertyComplexType); } } - return $collection; + return $annotations; } -} \ No newline at end of file +} diff --git a/src/BeSimple/SoapBundle/ServiceDefinition/ServiceDefinition.php b/src/BeSimple/SoapBundle/ServiceDefinition/ServiceDefinition.php index b748c4b..cb9834e 100644 --- a/src/BeSimple/SoapBundle/ServiceDefinition/ServiceDefinition.php +++ b/src/BeSimple/SoapBundle/ServiceDefinition/ServiceDefinition.php @@ -133,13 +133,13 @@ class ServiceDefinition return isset($this->complexTypes[$type]); } - public function addDefinitionComplexType($type, Collection $complexType) + public function addDefinitionComplexType($type, $definition) { if ($this->hasDefinitionComplexType($type)) { return false; } - $this->complexTypes[$type] = $complexType; + $this->complexTypes[$type] = $definition; return true; } diff --git a/src/BeSimple/SoapBundle/ServiceDefinition/Strategy/ComplexType.php b/src/BeSimple/SoapBundle/ServiceDefinition/Strategy/ComplexType.php index 0e0b579..ab824ad 100644 --- a/src/BeSimple/SoapBundle/ServiceDefinition/Strategy/ComplexType.php +++ b/src/BeSimple/SoapBundle/ServiceDefinition/Strategy/ComplexType.php @@ -38,54 +38,43 @@ class ComplexType extends AbstractComplexTypeStrategy */ public function addComplexType($classname) { - // Really needed? - if (null !== $type = $this->scanRegisteredTypes($classname)) { - return $type; - } - $classmap = $this->definition->getClassmap(); if ($classmap->hasByClassname($classname)) { - $type = $classmap->getByClassname($classname); - - $xmlType = 'tns:'.$type; - } else { - if (!$this->loader->supports($classname)) { - throw new \InvalidArgumentException(sprintf('Cannot add a complex type "%s" that is not an object or where class could not be found in "ComplexType" strategy.', $classname)); - } - - $type = $this->getContext()->translateType($classname); - $xmlType = 'tns:'.$type; - - // Register type here to avoid recursion - $classmap->add($type, $classname); - $this->getContext()->addType($classname, $xmlType); + return 'tns:'.$classmap->getByClassname($classname); } - $this->addXmlDefinition($classname, $type); + if (!$this->loader->supports($classname)) { + throw new \InvalidArgumentException(sprintf('Cannot add ComplexType "%s" because it is not an object or the class could not be found.', $classname)); + } + + $definitionComplexType = $this->loader->load($classname); + $classnameAlias = isset($definitionComplexType['alias']) ? $definitionComplexType['alias'] : $classname; + + $type = $this->getContext()->translateType($classnameAlias); + $xmlType = 'tns:'.$type; + + // Register type here to avoid recursion + $classmap->add($type, $classname); + $this->addXmlDefinition($definitionComplexType, $classname, $type); return $xmlType; } - private function addXmlDefinition($classname, $type) + private function addXmlDefinition(array $definitionComplexType, $classname, $type) { - if ($this->definition->hasDefinitionComplexType($classname)) { - return false; - } - $dom = $this->getContext()->toDomDocument(); $complexType = $dom->createElement('xsd:complexType'); $complexType->setAttribute('name', $type); $all = $dom->createElement('xsd:all'); - $elements = array(); - $definitionComplexType = $this->loader->load($classname); - foreach ($definitionComplexType as $annotationComplexType) { + $elements = array(); + foreach ($definitionComplexType['properties'] as $property) { $element = $dom->createElement('xsd:element'); - $element->setAttribute('name', $annotationComplexType->getName()); - $element->setAttribute('type', $this->getContext()->getType($annotationComplexType->getValue())); + $element->setAttribute('name', $property->getName()); + $element->setAttribute('type', $this->getContext()->getType($property->getValue())); - if ($annotationComplexType->isNillable()) { + if ($property->isNillable()) { $element->setAttribute('nillable', 'true'); } @@ -96,7 +85,5 @@ class ComplexType extends AbstractComplexTypeStrategy $this->getContext()->getSchema()->appendChild($complexType); $this->definition->addDefinitionComplexType($type, $definitionComplexType); - - return true; } }