[SoapBundle] Moved configuration of ComplexType aliases from config to PHP objects with annotations

Before:
``` yaml
be_simple_soap:
    services_classmap:
        Cutomer: My\Bundle\Entity\Customer
        Cart:    My\Bundle\Entity\Cart

After:
``` php
<?php

namespace My\Bundle\Entity;

use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;

/**
 * @Soap\Alias("Customer")
 */
class Customer
{
    // ... your PHP code
}

/**
 * @Soap\Alias("Cart")
 */
class Cart
{
    // ... your PHP code
}
This commit is contained in:
Francis Besset 2013-07-23 14:44:32 +02:00
parent 6de878de7d
commit e765ea746e
5 changed files with 71 additions and 42 deletions

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* 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';
}
}

View File

@ -21,7 +21,7 @@ abstract class Configuration implements ConfigurationInterface
{ {
foreach ($values as $k => $v) { foreach ($values as $k => $v) {
if (!method_exists($this, $name = 'set'.$k)) { 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); $this->$name($v);

View File

@ -22,6 +22,7 @@ use BeSimple\SoapBundle\Util\Collection;
*/ */
class AnnotationComplexTypeLoader extends AnnotationClassLoader class AnnotationComplexTypeLoader extends AnnotationClassLoader
{ {
private $aliasClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\Alias';
private $complexTypeClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\ComplexType'; 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)); throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
} }
$class = new \ReflectionClass($class); $annotations = array();
$collection = new Collection('getName', 'BeSimple\SoapBundle\ServiceDefinition\ComplexType');
$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) { foreach ($class->getProperties() as $property) {
$complexType = $this->reader->getPropertyAnnotation($property, $this->complexTypeClass); $complexType = $this->reader->getPropertyAnnotation($property, $this->complexTypeClass);
@ -51,10 +57,10 @@ class AnnotationComplexTypeLoader extends AnnotationClassLoader
$propertyComplexType->setValue($complexType->getValue()); $propertyComplexType->setValue($complexType->getValue());
$propertyComplexType->setNillable($complexType->isNillable()); $propertyComplexType->setNillable($complexType->isNillable());
$propertyComplexType->setName($property->getName()); $propertyComplexType->setName($property->getName());
$collection->add($propertyComplexType); $annotations['properties']->add($propertyComplexType);
} }
} }
return $collection; return $annotations;
} }
} }

View File

@ -133,13 +133,13 @@ class ServiceDefinition
return isset($this->complexTypes[$type]); return isset($this->complexTypes[$type]);
} }
public function addDefinitionComplexType($type, Collection $complexType) public function addDefinitionComplexType($type, $definition)
{ {
if ($this->hasDefinitionComplexType($type)) { if ($this->hasDefinitionComplexType($type)) {
return false; return false;
} }
$this->complexTypes[$type] = $complexType; $this->complexTypes[$type] = $definition;
return true; return true;
} }

View File

@ -38,40 +38,30 @@ class ComplexType extends AbstractComplexTypeStrategy
*/ */
public function addComplexType($classname) public function addComplexType($classname)
{ {
// Really needed?
if (null !== $type = $this->scanRegisteredTypes($classname)) {
return $type;
}
$classmap = $this->definition->getClassmap(); $classmap = $this->definition->getClassmap();
if ($classmap->hasByClassname($classname)) { if ($classmap->hasByClassname($classname)) {
$type = $classmap->getByClassname($classname); return 'tns:'.$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); 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; $xmlType = 'tns:'.$type;
// Register type here to avoid recursion // Register type here to avoid recursion
$classmap->add($type, $classname); $classmap->add($type, $classname);
$this->getContext()->addType($classname, $xmlType); $this->addXmlDefinition($definitionComplexType, $classname, $type);
}
$this->addXmlDefinition($classname, $type);
return $xmlType; 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(); $dom = $this->getContext()->toDomDocument();
$complexType = $dom->createElement('xsd:complexType'); $complexType = $dom->createElement('xsd:complexType');
$complexType->setAttribute('name', $type); $complexType->setAttribute('name', $type);
@ -79,13 +69,12 @@ class ComplexType extends AbstractComplexTypeStrategy
$all = $dom->createElement('xsd:all'); $all = $dom->createElement('xsd:all');
$elements = array(); $elements = array();
$definitionComplexType = $this->loader->load($classname); foreach ($definitionComplexType['properties'] as $property) {
foreach ($definitionComplexType as $annotationComplexType) {
$element = $dom->createElement('xsd:element'); $element = $dom->createElement('xsd:element');
$element->setAttribute('name', $annotationComplexType->getName()); $element->setAttribute('name', $property->getName());
$element->setAttribute('type', $this->getContext()->getType($annotationComplexType->getValue())); $element->setAttribute('type', $this->getContext()->getType($property->getValue()));
if ($annotationComplexType->isNillable()) { if ($property->isNillable()) {
$element->setAttribute('nillable', 'true'); $element->setAttribute('nillable', 'true');
} }
@ -96,7 +85,5 @@ class ComplexType extends AbstractComplexTypeStrategy
$this->getContext()->getSchema()->appendChild($complexType); $this->getContext()->getSchema()->appendChild($complexType);
$this->definition->addDefinitionComplexType($type, $definitionComplexType); $this->definition->addDefinitionComplexType($type, $definitionComplexType);
return true;
} }
} }