2011-04-08 00:41:16 +02:00
|
|
|
<?php
|
|
|
|
/*
|
2011-07-18 22:43:12 +02:00
|
|
|
* This file is part of the BeSimpleSoapBundle.
|
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-07-18 22:43:12 +02:00
|
|
|
namespace BeSimple\SoapBundle\ServiceDefinition\Loader;
|
2011-04-08 00:41:16 +02:00
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Argument;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Method;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Type;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Annotation\Method as MethodAnnotation;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Annotation\Param as ParamAnnotation;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Annotation\Result as ResultAnnotation;
|
2011-07-17 10:46:54 +02:00
|
|
|
|
|
|
|
use Doctrine\Common\Annotations\Reader;
|
2011-04-09 00:40:31 +02:00
|
|
|
|
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-07-18 22:43:12 +02:00
|
|
|
private $methodAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Method';
|
|
|
|
private $paramAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Param';
|
|
|
|
private $resultAnnotationClass = 'BeSimple\\SoapBundle\\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.
|
|
|
|
*
|
2011-07-17 10:46:54 +02:00
|
|
|
* @param \Doctrine\Common\Annotations\Reader $reader
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
2011-07-17 10:46:54 +02:00
|
|
|
public function __construct(Reader $reader)
|
2011-04-08 00:41:16 +02:00
|
|
|
{
|
|
|
|
$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));
|
|
|
|
}
|
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
$class = new \ReflectionClass($class);
|
2011-04-09 00:40:31 +02:00
|
|
|
$definition = new ServiceDefinition();
|
2011-07-17 15:03:05 +02:00
|
|
|
|
2011-07-14 17:45:03 +02:00
|
|
|
foreach ($class->getMethods() as $method) {
|
2011-07-17 10:46:54 +02:00
|
|
|
$serviceArguments = array();
|
|
|
|
$serviceMethod =
|
|
|
|
$serviceReturn = null;
|
|
|
|
|
|
|
|
foreach ($this->reader->getMethodAnnotations($method) as $i => $annotation) {
|
|
|
|
if ($annotation instanceof ParamAnnotation) {
|
|
|
|
$serviceArguments[] = new Argument(
|
|
|
|
$annotation->getValue(),
|
2011-07-17 15:03:05 +02:00
|
|
|
$this->getArgumentType($method, $annotation)
|
2011-07-17 10:46:54 +02:00
|
|
|
);
|
|
|
|
} elseif ($annotation instanceof MethodAnnotation) {
|
|
|
|
if ($serviceMethod) {
|
|
|
|
throw new \LogicException(sprintf('@Method defined twice for "%s".', $method->getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$serviceReturn = new Type($annotation->getPhpType(), $annotation->getXmlType());
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
if (!$serviceMethod && (!empty($serviceArguments) || $serviceReturn)) {
|
|
|
|
throw new \LogicException(sprintf('@Method non-existent for "%s".', $method->getName()));
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
if ($serviceMethod) {
|
|
|
|
$serviceMethod->setArguments($serviceArguments);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
if ($serviceReturn) {
|
|
|
|
$serviceMethod->setReturn($serviceReturn);
|
2011-04-09 00:40:31 +02:00
|
|
|
}
|
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-17 10:46:54 +02:00
|
|
|
if(null !== $annotation->getService()) {
|
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-07-17 15:03:05 +02:00
|
|
|
/**
|
|
|
|
* @param \ReflectionMethod $method
|
|
|
|
* @param ParamAnnotation $annotation
|
|
|
|
*
|
2011-07-18 22:43:12 +02:00
|
|
|
* @return \BeSimple\SoapBundle\ServiceDefinition\Type
|
2011-07-17 15:03:05 +02:00
|
|
|
*/
|
|
|
|
private function getArgumentType(\ReflectionMethod $method, ParamAnnotation $annotation)
|
|
|
|
{
|
|
|
|
$phpType = $annotation->getPhpType();
|
|
|
|
$xmlType = $annotation->getXmlType();
|
|
|
|
|
|
|
|
if (null === $phpType) {
|
|
|
|
foreach ($method->getParameters() as $param) {
|
|
|
|
if ($param->name === $annotation->getName()) {
|
|
|
|
$phpType = $param->getClass()->name;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Type($phpType, $xmlType);
|
|
|
|
}
|
|
|
|
|
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-17 15:03:05 +02:00
|
|
|
}
|