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

@ -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']);
}
/**