The bundle is back!

The definition of service has changed, read the README.
This commit is contained in:
Francis Besset
2011-07-17 10:46:54 +02:00
parent 81118f8d47
commit 1c608ccf20
48 changed files with 641 additions and 535 deletions

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the WebServiceBundle.
*
* (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 Bundle\WebServiceBundle\ServiceDefinition\Annotation;
/**
* Based on \Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation
*
* @author Francis Besset <francis.besset@gmail.com>
*/
abstract class Configuration implements ConfigurationInterface
{
public function __construct(array $values)
{
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)));
}
$this->$name($v);
}
}
}

View File

@ -0,0 +1,26 @@
<?php
/*
* This file is part of the WebServiceBundle.
*
* (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 Bundle\WebServiceBundle\ServiceDefinition\Annotation;
/**
* Based on \Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface
*
* @author Francis Besset <francis.besset@gmail.com>
*/
interface ConfigurationInterface
{
/**
* Returns the alias name for an annotated configuration.
*
* @return string
*/
function getAliasName();
}

View File

@ -10,24 +10,36 @@
namespace Bundle\WebServiceBundle\ServiceDefinition\Annotation;
class Method
/**
* @Annotation
*/
class Method extends Configuration
{
private $name;
private $value;
private $service;
public function __construct($values)
public function getValue()
{
$this->name = isset($values['value']) ? $values['value'] : null;
$this->service = isset($values['service']) ? $values['service'] : null;
}
public function getName($default = null)
{
return $this->name !== null ? $this->name : $default;
return $this->value;
}
public function getService()
{
return $this->service;
}
public function setValue($value)
{
$this->value = $value;
}
public function setService($service)
{
$this->service = $service;
}
public function getAliasName()
{
return 'method';
}
}

View File

@ -10,19 +10,47 @@
namespace Bundle\WebServiceBundle\ServiceDefinition\Annotation;
class Param extends TypedElement
/**
* @Annotation
*/
class Param extends Configuration implements TypedElementInterface
{
private $name;
private $value;
private $phpType;
private $xmlType;
public function __construct($values)
public function getValue()
{
parent::__construct($values);
$this->name = $values['value'];
return $this->value;
}
public function getName()
public function getPhpType()
{
return $this->name;
return $this->phpType;
}
public function getXmlType()
{
return $this->xmlType;
}
public function setValue($value)
{
$this->value = $value;
}
public function setPhpType($phpType)
{
$this->phpType = $phpType;
}
public function setXmlType($xmlType)
{
$this->xmlType = $xmlType;
}
public function getAliasName()
{
return 'param';
}
}

View File

@ -10,10 +10,36 @@
namespace Bundle\WebServiceBundle\ServiceDefinition\Annotation;
class Result extends TypedElement
/**
* @Annotation
*/
class Result extends Configuration implements TypedElementInterface
{
public function __construct($values)
private $phpType;
private $xmlType;
public function getPhpType()
{
parent::__construct($values);
return $this->phpType;
}
public function getXmlType()
{
return $this->xmlType;
}
public function setPhpType($phpType)
{
$this->phpType = $phpType;
}
public function setXmlType($xmlType)
{
$this->xmlType = $xmlType;
}
public function getAliasName()
{
return 'result';
}
}

View File

@ -1,40 +0,0 @@
<?php
/*
* This file is part of the WebServiceBundle.
*
* (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 Bundle\WebServiceBundle\ServiceDefinition\Annotation;
abstract class TypedElement
{
private $phpType;
private $xmlType;
public function __construct($values)
{
foreach(array('type', 'phpType') as $key)
{
if(isset($values[$key]))
{
$this->phpType = $values[$key];
}
}
$this->xmlType = isset($values['xmlType']) ? $values['xmlType'] : null;
}
public function getPhpType()
{
return $this->phpType;
}
public function getXmlType()
{
return $this->xmlType;
}
}

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the WebServiceBundle.
*
* (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 Bundle\WebServiceBundle\ServiceDefinition\Annotation;
interface TypedElementInterface
{
function getPhpType();
function getXmlType();
function setPhpType($phpType);
function setXmlType($xmlType);
}

View File

@ -12,14 +12,11 @@ namespace Bundle\WebServiceBundle\ServiceDefinition\Dumper;
use Bundle\WebServiceBundle\ServiceDefinition\Method;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Bundle\WebServiceBundle\Util\Assert;
use Zend\Soap\Wsdl;
/**
*
* @author Christian Kerl <christian-kerl@web.de>
*/
class WsdlDumper implements DumperInterface
@ -28,22 +25,20 @@ class WsdlDumper implements DumperInterface
public function dumpServiceDefinition(ServiceDefinition $definition, array $options = array())
{
Assert::thatArgumentNotNull('definition', $definition);
$options = array_merge(array('endpoint' => ''), $options);
Assert::thatArgumentNotNull('definition', $definition);
$this->definition = $definition;
$wsdl = new Wsdl($definition->getName(), $definition->getNamespace());
$port = $wsdl->addPortType($this->getPortTypeName());
$binding = $wsdl->addBinding($this->getBindingName(), 'tns:' . $this->getPortTypeName());
$wsdl = new Wsdl($definition->getName(), $definition->getNamespace());
$port = $wsdl->addPortType($this->getPortTypeName());
$binding = $wsdl->addBinding($this->getBindingName(), 'tns:' . $this->getPortTypeName());
$wsdl->addSoapBinding($binding, 'rpc');
$wsdl->addService($this->getServiceName(), $this->getPortName(), 'tns:' . $this->getBindingName(), $options['endpoint']);
foreach($definition->getMethods() as $method) {
$requestParts = array();
$requestParts = array();
$responseParts = array();
foreach($method->getArguments() as $argument) {
@ -61,15 +56,15 @@ class WsdlDumper implements DumperInterface
$portOperation->setAttribute('parameterOrder', implode(' ', array_keys($requestParts)));
$bindingInput = array(
'parts' => implode(' ', array_keys($requestParts)),
'use' => 'literal',
'namespace' => $definition->getNamespace(),
'parts' => implode(' ', array_keys($requestParts)),
'use' => 'literal',
'namespace' => $definition->getNamespace(),
'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/',
);
$bindingOutput = array(
'parts' => implode(' ', array_keys($responseParts)),
'use' => 'literal',
'namespace' => $definition->getNamespace(),
'parts' => implode(' ', array_keys($responseParts)),
'use' => 'literal',
'namespace' => $definition->getNamespace(),
'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/',
);
@ -86,36 +81,36 @@ class WsdlDumper implements DumperInterface
protected function getPortName()
{
return $this->definition->getName() . 'Port';
return $this->definition->getName().'Port';
}
protected function getPortTypeName()
{
return $this->definition->getName() . 'PortType';
return $this->definition->getName().'PortType';
}
protected function getBindingName()
{
return $this->definition->getName() . 'Binding';
return $this->definition->getName().'Binding';
}
protected function getServiceName()
{
return $this->definition->getName() . 'Service';
return $this->definition->getName().'Service';
}
protected function getRequestMessageName(Method $method)
{
return $method->getName() . 'Request';
return $method->getName().'Request';
}
protected function getResponseMessageName(Method $method)
{
return $method->getName() . 'Response';
return $method->getName().'Response';
}
protected function getSoapOperationName(Method $method)
{
return $this->definition->getNamespace() . $method->getName();
return $this->definition->getNamespace().$method->getName();
}
}

View File

@ -8,17 +8,17 @@
* with this source code in the file LICENSE.
*/
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Bundle\WebServiceBundle\ServiceDefinition\Method;
use Bundle\WebServiceBundle\ServiceDefinition\Argument;
use Bundle\WebServiceBundle\ServiceDefinition\Method;
use Bundle\WebServiceBundle\ServiceDefinition\Type;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Bundle\WebServiceBundle\ServiceDefinition\Annotation\Method as MethodAnnotation;
use Bundle\WebServiceBundle\ServiceDefinition\Annotation\Param as ParamAnnotation;
use Bundle\WebServiceBundle\ServiceDefinition\Annotation\Result as ResultAnnotation;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
@ -32,18 +32,18 @@ use Symfony\Component\Config\Loader\LoaderResolver;
*/
class AnnotationClassLoader implements LoaderInterface
{
private $wsMethodAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Method';
private $wsParamAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Param';
private $wsResultAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Result';
private $methodAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Method';
private $paramAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Param';
private $resultAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Result';
protected $reader;
/**
* Constructor.
*
* @param AnnotationReader $reader
* @param \Doctrine\Common\Annotations\Reader $reader
*/
public function __construct(AnnotationReader $reader)
public function __construct(Reader $reader)
{
$this->reader = $reader;
}
@ -64,31 +64,46 @@ class AnnotationClassLoader implements LoaderInterface
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
$class = new \ReflectionClass($class);
$class = new \ReflectionClass($class);
$definition = new ServiceDefinition();
foreach ($class->getMethods() as $method) {
$wsMethodAnnot = $this->reader->getMethodAnnotation($method, $this->wsMethodAnnotationClass);
$serviceArguments = array();
$serviceMethod =
$serviceReturn = null;
if($wsMethodAnnot !== null) {
$wsParamAnnots = $this->reader->getMethodAnnotations($method, $this->wsParamAnnotationClass);
$wsResultAnnot = $this->reader->getMethodAnnotation($method, $this->wsResultAnnotationClass);
foreach ($this->reader->getMethodAnnotations($method) as $i => $annotation) {
if ($annotation instanceof ParamAnnotation) {
$serviceArguments[] = new Argument(
$annotation->getValue(),
new Type($annotation->getPhpType(), $annotation->getXmlType())
);
} elseif ($annotation instanceof MethodAnnotation) {
if ($serviceMethod) {
throw new \LogicException(sprintf('@Method defined twice for "%s".', $method->getName()));
}
$serviceMethod = new Method();
$serviceMethod->setName($wsMethodAnnot->getName($method->getName()));
$serviceMethod->setController($this->getController($method, $wsMethodAnnot));
$serviceMethod = new Method(
$annotation->getValue(),
$this->getController($method, $annotation)
);
} elseif ($annotation instanceof ResultAnnotation) {
if ($serviceReturn) {
throw new \LogicException(sprintf('@Result defined twice for "%s".', $method->getName()));
}
foreach($wsParamAnnots as $wsParamAnnot) {
$serviceArgument = new Argument();
$serviceArgument->setName($wsParamAnnot->getName());
$serviceArgument->setType(new Type($wsParamAnnot->getPhpType(), $wsParamAnnot->getXmlType()));
$serviceMethod->getArguments()->add($serviceArgument);
$serviceReturn = new Type($annotation->getPhpType(), $annotation->getXmlType());
}
}
if($wsResultAnnot !== null) {
$serviceMethod->setReturn(new Type($wsResultAnnot->getPhpType(), $wsResultAnnot->getXmlType()));
if (!$serviceMethod && (!empty($serviceArguments) || $serviceReturn)) {
throw new \LogicException(sprintf('@Method non-existent for "%s".', $method->getName()));
}
if ($serviceMethod) {
$serviceMethod->setArguments($serviceArguments);
if ($serviceReturn) {
$serviceMethod->setReturn($serviceReturn);
}
$definition->getMethods()->add($serviceMethod);
@ -100,7 +115,7 @@ class AnnotationClassLoader implements LoaderInterface
private function getController(\ReflectionMethod $method, MethodAnnotation $annotation)
{
if($annotation->getService() !== null) {
if(null !== $annotation->getService()) {
return $annotation->getService() . ':' . $method->name;
} else {
return $method->class . '::' . $method->name;

View File

@ -8,14 +8,13 @@
* with this source code in the file LICENSE.
*/
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
/**
* AnnotationFileLoader loads ServiceDefinition from annotations set
@ -60,13 +59,11 @@ class AnnotationFileLoader extends FileLoader
{
$path = $this->locator->locate($file);
$definition = new ServiceDefinition();
if ($class = $this->findClass($path)) {
$definition = $this->loader->load($class, $type);
return $definition = $this->loader->load($class, $type);
}
return $definition;
return null;
}
/**
@ -91,9 +88,9 @@ class AnnotationFileLoader extends FileLoader
*/
protected function findClass($file)
{
$class = false;
$class = false;
$namespace = false;
$tokens = token_get_all(file_get_contents($file));
$tokens = token_get_all(file_get_contents($file));
while ($token = array_shift($tokens)) {
if (!is_array($token)) {
continue;

View File

@ -1,52 +0,0 @@
<?php
/*
* This file is part of the WebServiceBundle.
*
* (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 Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Doctrine\Common\Annotations\Lexer;
use Doctrine\Common\Annotations\Parser;
/**
* AnnotationParser allows multiple annotations of the same class to be present.
*
* @author Christian Kerl <christian-kerl@web.de>
*/
class AnnotationParser extends Parser
{
/**
* Annotations ::= Annotation {[ "*" ]* [Annotation]}*
*
* @return array
*/
public function Annotations()
{
$this->isNestedAnnotation = false;
$annotations = array();
$annot = $this->Annotation();
if ($annot !== false) {
$annotations[get_class($annot)][] = $annot;
$this->getLexer()->skipUntil(Lexer::T_AT);
}
while ($this->getLexer()->lookahead !== null && $this->getLexer()->isNextToken(Lexer::T_AT)) {
$this->isNestedAnnotation = false;
$annot = $this->Annotation();
if ($annot !== false) {
$annotations[get_class($annot)][] = $annot;
$this->getLexer()->skipUntil(Lexer::T_AT);
}
}
return $annotations;
}
}

View File

@ -1,39 +0,0 @@
<?php
/*
* This file is part of the WebServiceBundle.
*
* (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 Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Doctrine\Common\Annotations\AnnotationReader as BaseAnnotationReader;
/**
* AnnotationReader.
*
* @author Christian Kerl <christian-kerl@web.de>
*/
class AnnotationReader extends BaseAnnotationReader
{
public function getMethodAnnotation(\ReflectionMethod $method, $type)
{
$annotation = parent::getMethodAnnotation($method, $type);
if($annotation !== null && count($annotation) > 1) {
throw new \LogicException(sprintf("There is more than one annotation of type '%s'!", $type));
}
return $annotation !== null ? $annotation[0] : null;
}
public function getMethodAnnotations(\ReflectionMethod $method, $type = null)
{
$annotations = parent::getMethodAnnotations($method);
return $type !== null && isset($annotations[$type]) ? $annotations[$type] : $annotations;
}
}

View File

@ -10,13 +10,13 @@
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Symfony\Component\Config\Loader\FileLoader;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Bundle\WebServiceBundle\ServiceDefinition\Argument;
use Bundle\WebServiceBundle\ServiceDefinition\Header;
use Bundle\WebServiceBundle\ServiceDefinition\Method;
use Bundle\WebServiceBundle\ServiceDefinition\Argument;
use Bundle\WebServiceBundle\ServiceDefinition\Type;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Symfony\Component\Config\Loader\FileLoader;
class XmlFileLoader extends FileLoader
{
@ -28,8 +28,7 @@ class XmlFileLoader extends FileLoader
public function load($file, $type = null)
{
$path = $this->locator->locate($file);
$xml = $this->parseFile($path);
$xml = $this->parseFile($path);
$definition = new ServiceDefinition();
$definition->setName((string) $xml['name']);
@ -53,9 +52,7 @@ class XmlFileLoader extends FileLoader
*/
protected function parseHeader(\SimpleXMLElement $node)
{
$header = new Header((string)$node['name'], $this->parseType($node->type));
return $header;
return new Header((string)$node['name'], $this->parseType($node->type));
}
/**
@ -96,12 +93,10 @@ class XmlFileLoader extends FileLoader
protected function parseType(\SimpleXMLElement $node)
{
$namespaces = $node->getDocNamespaces(true);
$qname = explode(':', $node['xml-type'], 2);
$xmlType = sprintf('{%s}%s', $namespaces[$qname[0]], $qname[1]);
$qname = explode(':', $node['xml-type'], 2);
$xmlType = sprintf('{%s}%s', $namespaces[$qname[0]], $qname[1]);
$type = new Type((string)$node['php-type'], $xmlType, (string)$node['converter']);
return $type;
return new Type((string)$node['php-type'], $xmlType, (string)$node['converter']);
}
/**

View File

@ -19,12 +19,15 @@ class Method
private $arguments;
private $return;
public function __construct($name = null, $controller = null, array $arguments = array(), $return = null)
public function __construct($name = null, $controller = null, array $arguments = array(), Type $return = null)
{
$this->setName($name);
$this->setController($controller);
$this->setArguments($arguments);
$this->setReturn($return);
if ($return) {
$this->setReturn($return);
}
}
public function getName()
@ -52,9 +55,9 @@ class Method
return $this->arguments;
}
public function setArguments($arguments)
public function setArguments(array $arguments)
{
$this->arguments = new Collection('getName');
$this->arguments = new Collection('getName', 'Bundle\WebServiceBundle\ServiceDefinition\Argument');
$this->arguments->addAll($arguments);
}
@ -63,7 +66,7 @@ class Method
return $this->return;
}
public function setReturn($return)
public function setReturn(Type $return)
{
$this->return = $return;
}

View File

@ -38,6 +38,10 @@ class ServiceDefinition
{
$this->setName($name);
$this->setNamespace($namespace);
$this->methods = new Collection('getName', 'Bundle\WebServiceBundle\ServiceDefinition\Method');
$this->headers = new Collection('getName', 'Bundle\WebServiceBundle\ServiceDefinition\Header');
$this->setMethods($methods);
$this->setHeaders($headers);
}
@ -85,9 +89,8 @@ class ServiceDefinition
/**
* @param array $methods
*/
public function setMethods($methods)
public function setMethods(array $methods)
{
$this->methods = new Collection('getName');
$this->methods->addAll($methods);
}
@ -102,9 +105,8 @@ class ServiceDefinition
/**
* @param array $headers
*/
public function setHeaders($headers)
public function setHeaders(array $headers)
{
$this->headers = new Collection('getName');
$this->headers->addAll($headers);
}
}