2011-04-08 00:41:16 +02:00
|
|
|
<?php
|
|
|
|
/*
|
2011-04-09 00:40:31 +02:00
|
|
|
* This file is part of the WebServiceBundle.
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* (c) Christian Kerl <christian-kerl@web.de>
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
|
2011-04-08 00:41:16 +02:00
|
|
|
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
|
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
|
|
|
|
use Bundle\WebServiceBundle\ServiceDefinition\Method;
|
|
|
|
use Bundle\WebServiceBundle\ServiceDefinition\Argument;
|
|
|
|
use Bundle\WebServiceBundle\ServiceDefinition\Type;
|
|
|
|
|
|
|
|
use Bundle\WebServiceBundle\ServiceDefinition\Annotation\Method as MethodAnnotation;
|
|
|
|
|
2011-04-08 00:41:16 +02:00
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
|
|
|
use Symfony\Component\Config\Loader\LoaderResolver;
|
|
|
|
|
|
|
|
/**
|
2011-04-09 00:40:31 +02:00
|
|
|
* AnnotationClassLoader loads ServiceDefinition from a PHP class and its methods.
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* Based on \Symfony\Component\Routing\Loader\AnnotationClassLoader
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* @author Christian Kerl <christian-kerl@web.de>
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
|
|
|
class AnnotationClassLoader implements LoaderInterface
|
|
|
|
{
|
2011-04-09 00:40:31 +02:00
|
|
|
private $wsMethodAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Method';
|
|
|
|
private $wsParamAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Param';
|
|
|
|
private $wsResultAnnotationClass = 'Bundle\\WebServiceBundle\\ServiceDefinition\\Annotation\\Result';
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-08 00:41:16 +02:00
|
|
|
protected $reader;
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-08 00:41:16 +02:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param AnnotationReader $reader
|
|
|
|
*/
|
|
|
|
public function __construct(AnnotationReader $reader)
|
|
|
|
{
|
|
|
|
$this->reader = $reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-09 00:40:31 +02:00
|
|
|
* Loads a ServiceDefinition from annotations from a class.
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
|
|
|
* @param string $class A class name
|
|
|
|
* @param string $type The resource type
|
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* @return ServiceDefinition A ServiceDefinition instance
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When route can't be parsed
|
|
|
|
*/
|
|
|
|
public function load($class, $type = null)
|
|
|
|
{
|
2011-07-14 17:45:03 +02:00
|
|
|
if (!class_exists($class)) {
|
2011-04-08 00:41:16 +02:00
|
|
|
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = new \ReflectionClass($class);
|
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
$definition = new ServiceDefinition();
|
2011-07-14 17:45:03 +02:00
|
|
|
|
|
|
|
foreach ($class->getMethods() as $method) {
|
2011-04-09 00:40:31 +02:00
|
|
|
$wsMethodAnnot = $this->reader->getMethodAnnotation($method, $this->wsMethodAnnotationClass);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
|
|
|
if($wsMethodAnnot !== null) {
|
2011-04-09 00:40:31 +02:00
|
|
|
$wsParamAnnots = $this->reader->getMethodAnnotations($method, $this->wsParamAnnotationClass);
|
|
|
|
$wsResultAnnot = $this->reader->getMethodAnnotation($method, $this->wsResultAnnotationClass);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
$serviceMethod = new Method();
|
|
|
|
$serviceMethod->setName($wsMethodAnnot->getName($method->getName()));
|
|
|
|
$serviceMethod->setController($this->getController($method, $wsMethodAnnot));
|
2011-07-14 17:45:03 +02:00
|
|
|
|
|
|
|
foreach($wsParamAnnots as $wsParamAnnot) {
|
2011-04-09 00:40:31 +02:00
|
|
|
$serviceArgument = new Argument();
|
|
|
|
$serviceArgument->setName($wsParamAnnot->getName());
|
|
|
|
$serviceArgument->setType(new Type($wsParamAnnot->getPhpType(), $wsParamAnnot->getXmlType()));
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
$serviceMethod->getArguments()->add($serviceArgument);
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
|
|
|
if($wsResultAnnot !== null) {
|
2011-04-09 00:40:31 +02:00
|
|
|
$serviceMethod->setReturn(new Type($wsResultAnnot->getPhpType(), $wsResultAnnot->getXmlType()));
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
$definition->getMethods()->add($serviceMethod);
|
|
|
|
}
|
2011-04-08 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
return $definition;
|
2011-04-08 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
2011-04-09 00:40:31 +02:00
|
|
|
private function getController(\ReflectionMethod $method, MethodAnnotation $annotation)
|
|
|
|
{
|
2011-07-14 17:45:03 +02:00
|
|
|
if($annotation->getService() !== null) {
|
2011-04-09 00:40:31 +02:00
|
|
|
return $annotation->getService() . ':' . $method->name;
|
2011-07-14 17:45:03 +02:00
|
|
|
} else {
|
2011-04-09 00:40:31 +02:00
|
|
|
return $method->class . '::' . $method->name;
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-08 00:41:16 +02:00
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
}
|