replaced old LoaderInterface with Symfony Config Component's LoaderInterface; added annotation based loader implementations;

This commit is contained in:
Christian Kerl 2011-04-08 00:41:16 +02:00
parent 70da526fd4
commit 8895a69d04
7 changed files with 321 additions and 61 deletions

View File

@ -0,0 +1,30 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="webservice.annotations.reader.class">Doctrine\Common\Annotations\AnnotationReader</parameter>
<parameter key="webservice.annotations.parser.class">Doctrine\Common\Annotations\Parser</parameter>
</parameters>
<services>
<service id="webservice.annotations.parser" class="%webservice.annotations.parser.class%">
<call method="setAutoloadAnnotations"><argument>true</argument></call>
<call method="setAnnotationNamespaceAlias">
<argument>Bundle\WebServiceBundle\ServiceDefinition\Annotation\</argument>
<argument>ws</argument>
</call>
</service>
<service id="webservice.annotations.reader" class="%webservice.annotations.reader.class%">
<argument type="service" id="webservice.annotations.cache" strict="false" />
<argument type="service" id="webservice.annotations.parser" />
</service>
<service id="webservice.annotations.cache.array" class="Doctrine\Common\Cache\ArrayCache" scope="prototype" />
<service id="webservice.annotations.cache" alias="webservice.annotations.cache.array" />
</services>
</container>

View File

@ -4,6 +4,8 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="webservice.file_locator.class">Symfony\Component\HttpKernel\Config\FileLocator</parameter>
<parameter key="webservice.cache_dir">%kernel.cache_dir%/webservice</parameter>
</parameters>
@ -14,11 +16,13 @@
</service>
<service id="webservice.context" class="Bundle\WebServiceBundle\WebServiceContext" abstract="true">
<argument type="service" id="webservice.definition.loader"/>
<argument type="service" id="webservice.definition.dumper.wsdl"/>
<argument type="service" id="webservice.converter.repository"/>
<argument type="service" id="webservice.binder.request"/>
<argument type="service" id="webservice.binder.response"/>
<argument type="collection">
<!--
<argument key="name">%webservice.name%</argument>
@ -29,6 +33,7 @@
-->
<argument key="cache_dir">%webservice.cache_dir%</argument>
</argument>
</service>
<service id="webservice.binder.request.rpcliteral" class="Bundle\WebServiceBundle\ServiceBinding\RpcLiteralRequestMessageBinder" />
@ -41,12 +46,27 @@
<argument type="service" id="service_container" />
</call>
</service>
<!--
<service id="webservice.definition.loader" class="">
<service id="webservice.file_locator" class="%webservice.file_locator.class%" public="false">
<argument type="service" id="kernel" />
</service>
-->
<service id="webservice.definition.dumper.wsdl.rpcliteral" class="Bundle\WebServiceBundle\ServiceDefinition\Dumper\WsdlDumpler">
<!-- TODO: replace with delegating loader -->
<service id="webservice.definition.loader" alias="webservice.definition.loader.annot_file" />
<service id="webservice.definition.loader.annot_file" class="Bundle\WebServiceBundle\ServiceDefinition\Loader\AnnotationFileLoader">
<tag name="webservice.definition.loader" />
<argument type="service" id="webservice.file_locator" />
<argument type="service" id="webservice.definition.loader.annot_class" />
</service>
<service id="webservice.definition.loader.annot_class" class="Bundle\WebServiceBundle\ServiceDefinition\Loader\AnnotationClassLoader">
<tag name="webservice.definition.loader" />
<argument type="service" id="webservice.annotations.reader" />
<argument type="service" id="annotations.configuration_reader" />
</service>
<service id="webservice.definition.dumper.wsdl.rpcliteral" class="Bundle\WebServiceBundle\ServiceDefinition\Dumper\WsdlDumper">
</service>
<!--

View File

@ -0,0 +1,127 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
/**
* AnnotationClassLoader loads routing information from a PHP class and its methods.
*
* You need to define an implementation for the getRouteDefaults() method. Most of the
* time, this method should define some PHP callable to be called for the route
* (a controller in MVC speak).
*
* The @Route annotation can be set on the class (for global parameters),
* and on each method.
*
* The @Route annotation main value is the route pattern. The annotation also
* recognizes three parameters: requirements, options, and name. The name parameter
* is mandatory. Here is an example of how you should be able to use it:
*
* /**
* * @Route("/Blog")
* * /
* class Blog
* {
* /**
* * @Route("/", name="blog_index")
* * /
* public function index()
* {
* }
*
* /**
* * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"})
* * /
* public function show()
* {
* }
* }
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class AnnotationClassLoader implements LoaderInterface
{
protected $reader;
/**
* Constructor.
*
* @param AnnotationReader $reader
*/
public function __construct(AnnotationReader $reader)
{
$this->reader = $reader;
}
/**
* Loads from annotations from a class.
*
* @param string $class A class name
* @param string $type The resource type
*
* @return RouteCollection A RouteCollection 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 RouteCollection();
$collection->addResource(new FileResource($class->getFileName()));
foreach ($class->getMethods() as $method) {
}
return $collection;
}
/**
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
* @param string $type The resource type
*
* @return Boolean True if this class supports the given resource, false otherwise
*/
public function supports($resource, $type = null)
{
return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
}
/**
* Sets the loader resolver.
*
* @param LoaderResolver $resolver A LoaderResolver instance
*/
public function setResolver(LoaderResolver $resolver)
{
}
/**
* Gets the loader resolver.
*
* @return LoaderResolver A LoaderResolver instance
*/
public function getResolver()
{
}
}

View File

@ -0,0 +1,122 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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;
/**
* AnnotationFileLoader loads ServiceDefinition from annotations set
* on a PHP class and its methods.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class AnnotationFileLoader extends FileLoader
{
protected $loader;
/**
* Constructor.
*
* @param AnnotationClassLoader $loader An AnnotationClassLoader instance
* @param string|array $paths A path or an array of paths where to look for resources
*/
public function __construct(FileLocator $locator, AnnotationClassLoader $loader, $paths = array())
{
if (!function_exists('token_get_all')) {
throw new \RuntimeException('The Tokenizer extension is required for the routing annotation loaders.');
}
parent::__construct($locator, $paths);
$this->loader = $loader;
}
/**
* Loads from annotations from a file.
*
* @param string $file A PHP file path
* @param string $type The resource type
*
* @return RouteCollection A RouteCollection instance
*
* @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed
*/
public function load($file, $type = null)
{
$path = $this->locator->locate($file);
$definiton = new ServiceDefinition();
if ($class = $this->findClass($path)) {
}
return $definiton;
}
/**
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
* @param string $type The resource type
*
* @return Boolean True if this class supports the given resource, false otherwise
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
}
/**
* Returns the full class name for the first class in the file.
*
* @param string $file A PHP file path
*
* @return string|false Full class name if found, false otherwise
*/
protected function findClass($file)
{
$class = false;
$namespace = false;
$tokens = token_get_all(file_get_contents($file));
while ($token = array_shift($tokens)) {
if (!is_array($token)) {
continue;
}
if (true === $class && T_STRING === $token[0]) {
return $namespace.'\\'.$token[1];
}
if (true === $namespace && T_STRING === $token[0]) {
$namespace = '';
do {
$namespace .= $token[1];
$token = array_shift($tokens);
} while ($tokens && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
}
if (T_CLASS === $token[0]) {
$class = true;
}
if (T_NAMESPACE === $token[0]) {
$namespace = true;
}
}
return false;
}
}

View File

@ -1,26 +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 Bundle\WebServiceBundle\Util\Assert;
abstract class FileLoader implements LoaderInterface
{
protected $file;
public function __construct($file)
{
Assert::thatArgument('file', file_exists($file), 'The service definition file %s does not exist');
Assert::thatArgument('file', is_readable($file), 'The service definition file %s is not readable');
$this->file = $file;
}
}

View File

@ -1,23 +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 Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
interface LoaderInterface
{
/**
* Loads the contents of the given ServiceDefinition.
*
* @param ServiceDefinition $definition
*/
function loadServiceDefinition(ServiceDefinition $definition);
}

View File

@ -10,6 +10,8 @@
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Symfony\Component\Config\Loader\FileLoader;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Bundle\WebServiceBundle\ServiceDefinition\Header;
use Bundle\WebServiceBundle\ServiceDefinition\Method;
@ -18,15 +20,21 @@ use Bundle\WebServiceBundle\ServiceDefinition\Type;
class XmlFileLoader extends FileLoader
{
public function loadServiceDefinition(ServiceDefinition $definition)
public function supports($resource, $type = null)
{
$xml = $this->parseFile($this->file);
if($definition->getName() != $xml['name'])
{
throw new \InvalidArgumentException();
return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION);
}
public function load($file, $type = null)
{
$path = $this->locator->locate($file);
$xml = $this->parseFile($path);
$definition = new ServiceDefinition();
$definition->setName((string) $xml['name']);
$definition->setNamespace((string) $xml['namespace']);
foreach($xml->header as $header)
{
$definition->getHeaders()->add($this->parseHeader($header));
@ -36,6 +44,8 @@ class XmlFileLoader extends FileLoader
{
$definition->getMethods()->add($this->parseMethod($method));
}
return $definition;
}
/**