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

@ -53,6 +53,7 @@ class SoapWebServiceController extends ContainerAware
$this->soapRequest = SoapRequest::createFromHttpRequest($this->container->get('request'));
$this->soapServer = $webServiceContext->getServerFactory()->create($this->soapRequest, $this->soapResponse);
$this->soapServer->setObject($this);
ob_start();

View File

@ -10,6 +10,7 @@
<parameter key="besimple.soap.definition.loader.annot_dir.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationDirectoryLoader</parameter>
<parameter key="besimple.soap.definition.loader.annot_file.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationFileLoader</parameter>
<parameter key="besimple.soap.definition.loader.annot_class.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationClassLoader</parameter>
<parameter key="besimple.soap.definition.loader.annot_complextype.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader</parameter>
</parameters>
<services>
@ -29,6 +30,10 @@
<tag name="besimple.soap.definition.loader" />
<argument type="service" id="annotation_reader" />
</service>
<service id="besimple.soap.definition.loader.annot_complextype" class="%besimple.soap.definition.loader.annot_complextype.class%" public="false">
<argument type="service" id="annotation_reader" />
</service>
</services>
</container>

View File

@ -50,7 +50,9 @@
<service id="besimple.soap.binder.response.documentwrapped" class="%besimple.soap.binder.response.documentwrapped.class%" />
<service id="besimple.soap.definition.dumper.wsdl.rpcliteral" class="%besimple.soap.definition.dumper.wsdl.rpcliteral.class%" />
<service id="besimple.soap.definition.dumper.wsdl.rpcliteral" class="%besimple.soap.definition.dumper.wsdl.rpcliteral.class%">
<argument type="service" id="besimple.soap.definition.loader.annot_complextype" />
</service>
<service id="besimple.soap.converter.repository" class="%besimple.soap.converter.repository.class%" />

View File

@ -20,5 +20,5 @@ interface MessageBinderInterface
*
* @return mixed
*/
function processMessage(Method $messageDefinition, $message);
function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array());
}

View File

@ -11,16 +11,42 @@
namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
class RpcLiteralRequestMessageBinder implements MessageBinderInterface
{
public function processMessage(Method $messageDefinition, $message)
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{
$result = array();
$i = 0;
foreach($messageDefinition->getArguments() as $argument) {
if (isset($message[$i])) {
if (preg_match('/^([^\[]+)\[\]$/', $argument->getType()->getPhpType(), $match)) {
$isArray = true;
$type = $match[1];
} else {
$isArray = false;
$type = $argument->getType()->getPhpType();
}
if (isset($definitionComplexTypes[$type])) {
if ($isArray) {
$array = array();
foreach ($message[$i]->item as $complexType) {
$array[] = $this->getInstanceOfType($type, $complexType, $definitionComplexTypes);
}
$message[$i] = $array;
} else {
$message[$i] = $this->getInstanceOfType($type, $message[$i], $definitionComplexTypes);
}
} elseif ($isArray) {
$message[$i] = $message[$i]->item;
}
$result[$argument->getName()] = $message[$i];
}
@ -29,4 +55,38 @@ class RpcLiteralRequestMessageBinder implements MessageBinderInterface
return $result;
}
private function getInstanceOfType($type, $message, array $definitionComplexTypes)
{
$typeClass = $type;
$instanceType = new $typeClass();
foreach ($definitionComplexTypes[$type] as $type) {
if ($type instanceof PropertyComplexType) {
if (isset($definitionComplexTypes[$type->getValue()])) {
$value = $this->getInstanceOfType($type->getValue(), $message->{$type->getName()}, $definitionComplexTypes);
} else {
$value = $message->{$type->getName()};
}
$instanceType->{$type->getOriginalName()} = $value;
} elseif ($type instanceof MethodComplexType) {
if (!$type->getSetter()) {
throw new \LogicException();
}
if (isset($definitionComplexTypes[$type->getValue()])) {
$value = $this->getInstanceOfType($type->getValue(), $message->{$type->getName()}, $definitionComplexTypes);
} else {
$value = $message->{$type->getName()};
}
$instanceType->{$type->getSetter()}($value);
} else {
throw new \InvalidArgumentException();
}
}
return $instanceType;
}
}

View File

@ -11,11 +11,64 @@
namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
class RpcLiteralResponseMessageBinder implements MessageBinderInterface
{
public function processMessage(Method $messageDefinition, $message)
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{
$return = $messageDefinition->getReturn();
$class = $return->getPhpType();
if (preg_match('/^([^\[]+)\[\]$/', $class, $match)) {
$isArray = true;
$type =
$class = $match[1];
} else {
$isArray = false;
$type = $return->getPhpType();
}
if (isset($definitionComplexTypes[$type])) {
if ($class[0] == '\\') {
$class = substr($class, 1);
}
if ($isArray) {
$array = array();
foreach ($message as $complexType) {
$array[] = $this->getInstanceOfStdClass($type, $class, $complexType, $definitionComplexTypes);
}
$message = $array;
} else {
$message = $this->getInstanceOfStdClass($type, $class, $message, $definitionComplexTypes);
}
}
return $message;
}
private function getInstanceOfStdClass($type, $class, $message, $definitionComplexTypes)
{
if (get_class($message) !== $class) {
throw new \InvalidArgumentException();
}
$stdClass = new \stdClass();
foreach ($definitionComplexTypes[$type] as $type) {
if ($type instanceof PropertyComplexType) {
$stdClass->{$type->getName()} = $message->{$type->getOriginalName()};
} elseif ($type instanceof MethodComplexType) {
$stdClass->{$type->getName()} = $message->{$type->getOriginalName()}();
} else {
throw new \InvalidArgumentException();
}
}
return $stdClass;
}
}

View File

@ -61,7 +61,7 @@ class ServiceBinder
$result = array();
$result['_controller'] = $methodDefinition->getController();
$result = array_merge($result, $this->requestMessageBinder->processMessage($methodDefinition, $arguments));
$result = array_merge($result, $this->requestMessageBinder->processMessage($methodDefinition, $arguments, $this->definition->getDefinitionComplexTypes()));
return $result;
}
@ -70,7 +70,7 @@ class ServiceBinder
{
$methodDefinition = $this->definition->getMethods()->get($name);
return $this->responseMessageBinder->processMessage($methodDefinition, $return);
return $this->responseMessageBinder->processMessage($methodDefinition, $return, $this->definition->getDefinitionComplexTypes());
}
protected function createSoapHeader(Header $headerDefinition, $data)

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
{
}

View File

@ -70,6 +70,11 @@ class WebServiceContext
return $this->serviceDefinition;
}
public function getWsdlFileContent($endpoint = null)
{
return file_get_contents($this->getWsdlFile($endpoint));
}
public function getWsdlFile($endpoint = null)
{
$file = sprintf('%s/%s.%s.wsdl', $this->options['cache_dir'], $this->options['name'], md5($endpoint));
@ -82,11 +87,6 @@ class WebServiceContext
return (string) $cache;
}
public function getWsdlFileContent($endpoint = null)
{
return file_get_contents($this->getWsdlFile($endpoint));
}
public function getServiceBinder()
{
if (null === $this->serviceBinder) {