New definition of complex type

The exposure of complex type properties was done only with public property.
Now you can expose properties and public methods.

Before:
  In a complex type:
    /**
     * @var string
     */
   public $var;

After:
  In a complex type:
    use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;

    /**
     * @Soap\PropertyComplexType("string", name="var", nillable="true")
     */
    public $username;

    private $email;

    /**
     * @Soap\MethodComplexType("string", name="email", nillable="true", setter="setEmail")
     */
    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

name, nillable are optionnal.
setter is optional and only available for MethodComplexType.
This commit is contained in:
Francis Besset
2011-07-23 20:06:42 +02:00
parent 9bd9677325
commit 4fa893042f
19 changed files with 592 additions and 23 deletions

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* 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 MethodComplexType extends Configuration
{
private $name;
private $value;
private $setter;
private $isNillable = false;
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
public function getSetter()
{
return $this->setter;
}
public function isNillable()
{
return $this->isNillable;
}
public function setName($name)
{
$this->name = $name;
}
public function setValue($value)
{
$this->value = $value;
}
public function setSetter($setter)
{
$this->setter = $setter;
}
public function setNillable($isNillable)
{
$this->isNillable = (bool) $isNillable;
}
public function getAliasName()
{
return 'methodcomplextype';
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* 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 PropertyComplexType extends Configuration
{
private $name;
private $value;
private $isNillable = false;
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
public function isNillable()
{
return $this->isNillable;
}
public function setName($name)
{
$this->name = $name;
}
public function setValue($value)
{
$this->value = $value;
}
public function setNillable($isNillable)
{
$this->isNillable = (bool) $isNillable;
}
public function getAliasName()
{
return 'propertycomplextype';
}
}

View File

@ -13,6 +13,7 @@ namespace BeSimple\SoapBundle\ServiceDefinition\Dumper;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Type;
use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
use BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader;
use BeSimple\SoapBundle\Util\Assert;
use BeSimple\SoapBundle\Util\QName;
@ -23,9 +24,15 @@ use Zend\Soap\Wsdl;
*/
class WsdlDumper implements DumperInterface
{
private $loader;
private $wsdl;
private $definition;
public function __construct(AnnotationComplexTypeLoader $loader)
{
$this->loader = $loader;
}
public function dumpServiceDefinition(ServiceDefinition $definition, array $options = array())
{
Assert::thatArgumentNotNull('definition', $definition);
@ -33,7 +40,7 @@ class WsdlDumper implements DumperInterface
$options = array_merge(array('endpoint' => '', 'stylesheet' => null), $options);
$this->definition = $definition;
$this->wsdl = new Wsdl($definition->getName(), $definition->getNamespace(), new WsdlTypeStrategy());
$this->wsdl = new Wsdl($definition->getName(), $definition->getNamespace(), new WsdlTypeStrategy($this->loader, $definition));
$port = $this->wsdl->addPortType($this->getPortTypeName());
$binding = $this->wsdl->addBinding($this->getBindingName(), $this->qualify($this->getPortTypeName()));

View File

@ -10,13 +10,15 @@
namespace BeSimple\SoapBundle\ServiceDefinition\Dumper;
use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
use BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\ComplexType;
use BeSimple\SoapBundle\Util\String;
use Zend\Soap\Exception;
use Zend\Soap\Wsdl;
use Zend\Soap\Wsdl\Strategy;
use Zend\Soap\Wsdl\Strategy\ArrayOfTypeSequence;
use Zend\Soap\Wsdl\Strategy\DefaultComplexType;
class WsdlTypeStrategy implements Strategy
{
@ -27,13 +29,16 @@ class WsdlTypeStrategy implements Strategy
*/
private $context;
private $loader;
private $definition;
private $typeStrategy;
private $arrayStrategy;
public function __construct()
public function __construct(AnnotationComplexTypeLoader $loader, ServiceDefinition $definition)
{
$this->typeStrategy = new DefaultComplexType();
$this->arrayStrategy = new ArrayOfTypeSequence();
$this->loader = $loader;
$this->definition = $definition;
}
/**
@ -51,9 +56,11 @@ class WsdlTypeStrategy implements Strategy
/**
* Create a complex type based on a strategy
*
* @throws \Zend\Soap\WsdlException
* @param string $type
*
* @return string XSD type
*
* @throws \Zend\Soap\WsdlException
*/
public function addComplexType($type)
{
@ -61,9 +68,28 @@ class WsdlTypeStrategy implements Strategy
throw new \LogicException(sprintf('Cannot add complex type "%s", no context is set for this composite strategy.', $type));
}
$strategy = String::endsWith($type, '[]') ? $this->arrayStrategy : $this->typeStrategy;
$strategy->setContext($this->context);
$strategy = String::endsWith($type, '[]') ? $this->getArrayStrategy() : $this->getTypeStrategy();
return $strategy->addComplexType($type);
}
private function getArrayStrategy()
{
if (!$this->arrayStrategy) {
$this->arrayStrategy = new ArrayOfTypeSequence();
$this->arrayStrategy->setContext($this->context);
}
return $this->arrayStrategy;
}
private function getTypeStrategy()
{
if (!$this->typeStrategy) {
$this->typeStrategy = new ComplexType($this->loader, $this->definition);
$this->typeStrategy->setContext($this->context);
}
return $this->typeStrategy;
}
}

View File

@ -32,10 +32,6 @@ use Symfony\Component\Config\Loader\LoaderResolver;
*/
class AnnotationClassLoader implements LoaderInterface
{
private $methodAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Method';
private $paramAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Param';
private $resultAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Result';
protected $reader;
/**

View File

@ -0,0 +1,94 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Loader;
use BeSimple\SoapBundle\ServiceDefinition\Annotation\ComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
use BeSimple\SoapBundle\Util\Collection;
/**
* AnnotationComplexTypeLoader loads ServiceDefinition from a PHP class and its methods.
*
* Based on \Symfony\Component\Routing\Loader\AnnotationClassLoader
*
* @author Francis Besset <francis.besset@gmail.com>
*/
class AnnotationComplexTypeLoader extends AnnotationClassLoader
{
private $propertyComplexTypeClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\PropertyComplexType';
private $methodComplexTypeClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\MethodComplexType';
/**
* Loads a ServiceDefinition from annotations from a class.
*
* @param string $class A class name
* @param string $type The resource type
*
* @return ServiceDefinition A ServiceDefinition instance
*
* @throws \InvalidArgumentException When route can't be parsed
*/
public function load($class, $type = null)
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
$class = new \ReflectionClass($class);
$collection = new Collection('getName');
foreach ($class->getProperties() as $property) {
if ($property->isPublic()) {
$complexType = $this->reader->getPropertyAnnotation($property, $this->propertyComplexTypeClass);
if ($complexType) {
$propertyComplexType = new PropertyComplexType();
$propertyComplexType->setValue($complexType->getValue());
$propertyComplexType->setNillable($complexType->isNillable());
if (!$complexType->getName()) {
$propertyComplexType->setName($property->getName());
} else {
$propertyComplexType->setName($complexType->getName());
$propertyComplexType->setOriginalName($property->getName());
}
$collection->add($propertyComplexType);
}
}
}
foreach ($class->getMethods() as $method) {
if ($method->isPublic()) {
$complexType = $this->reader->getMethodAnnotation($method, $this->methodComplexTypeClass);
if ($complexType) {
$methodComplexType = new MethodComplexType();
$methodComplexType->setValue($complexType->getValue());
$methodComplexType->setSetter($complexType->getSetter());
$methodComplexType->setNillable($complexType->isNillable());
if (!$complexType->getName()) {
$methodComplexType->setName($property->getName());
} else {
$methodComplexType->setName($complexType->getName());
$methodComplexType->setOriginalName($method->getName());
}
$collection->add($methodComplexType);
}
}
}
return $collection;
}
}

View File

@ -34,6 +34,8 @@ class ServiceDefinition
*/
private $headers;
private $complexTypes = array();
public function __construct($name = null, $namespace = null, array $methods = array(), array $headers = array())
{
$this->setName($name);
@ -127,4 +129,14 @@ class ServiceDefinition
return $types;
}
public function addDefinitionComplexType($type, Collection $complexType)
{
$this->complexTypes[$type] = $complexType;
}
public function getDefinitionComplexTypes()
{
return $this->complexTypes;
}
}

View File

@ -0,0 +1,62 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
abstract class BaseComplexType
{
private $name;
private $originalName;
private $value;
private $isNillable = false;
public function getName()
{
return $this->name;
}
public function getOriginalName()
{
return $this->originalName ?: $this->name;
}
public function getValue()
{
return $this->value;
}
public function isNillable()
{
return $this->isNillable;
}
public function setName($name)
{
$this->name = $name;
}
public function setOriginalName($originalName)
{
$this->originalName = $originalName;
}
public function setValue($value)
{
$this->value = $value;
}
public function setNillable($isNillable)
{
$this->isNillable = (bool) $isNillable;
}
}

View File

@ -0,0 +1,81 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
use BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader;
use Zend\Soap\Wsdl;
use Zend\Soap\Wsdl\Strategy\AbstractStrategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class ComplexType extends AbstractStrategy
{
private $loader;
private $definition;
public function __construct(AnnotationComplexTypeLoader $loader, $definition)
{
$this->loader = $loader;
$this->definition = $definition;
}
/**
* Add a complex type by recursivly using all the class properties fetched via Reflection.
*
* @param string $type Name of the class to be specified
* @return string XSD Type for the given PHP type
*/
public function addComplexType($type)
{
if (null !== $soapType = $this->scanRegisteredTypes($type)) {
return $soapType;
}
if (!$this->loader->supports($type)) {
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.', $type));
}
$dom = $this->getContext()->toDomDocument();
$soapTypeName = Wsdl::translateType($type);
$soapType = 'tns:'.$soapTypeName;
// Register type here to avoid recursion
$this->getContext()->addType($type, $soapType);
$complexType = $dom->createElement('xsd:complexType');
$complexType->setAttribute('name', $soapTypeName);
$all = $dom->createElement('xsd:all');
$definitionComplexType = $this->loader->load($type);
$this->definition->addDefinitionComplexType($type, $definitionComplexType);
foreach ($definitionComplexType as $annotationComplexType) {
$element = $dom->createElement('xsd:element');
$element->setAttribute('name', $propertyName = $annotationComplexType->getName());
$element->setAttribute('type', $this->getContext()->getType(trim($annotationComplexType->getValue())));
if ($annotationComplexType->isNillable()) {
$element->setAttribute('nillable', 'true');
}
$all->appendChild($element);
}
$complexType->appendChild($all);
$this->getContext()->getSchema()->appendChild($complexType);
return $soapType;
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class MethodComplexType extends BaseComplexType
{
private $setter;
public function getSetter()
{
return $this->setter;
}
public function setSetter($setter)
{
$this->setter = $setter;
}
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class PropertyComplexType extends BaseComplexType
{
}