[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) {
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);

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}
}