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-08-14 18:00:28 +02:00
|
|
|
use BeSimple\SoapBundle\ServiceDefinition as Definition;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Annotation;
|
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;
|
2012-09-18 22:33:29 +02:00
|
|
|
use Symfony\Component\Config\Loader\LoaderResolverInterface;
|
2011-04-08 00:41:16 +02:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
{
|
|
|
|
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-08-14 18:00:28 +02:00
|
|
|
* @return \BeSimple\SoapBundle\ServiceDefinition\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-08-14 18:00:28 +02:00
|
|
|
$definition = new Definition\ServiceDefinition();
|
2011-07-17 15:03:05 +02:00
|
|
|
|
2011-09-20 22:34:16 +02:00
|
|
|
$serviceMethodHeaders = array();
|
|
|
|
foreach ($this->reader->getClassAnnotations($class) as $annotation) {
|
|
|
|
if ($annotation instanceof Annotation\Header) {
|
|
|
|
$serviceMethodHeaders[$annotation->getValue()] = $annotation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-14 17:45:03 +02:00
|
|
|
foreach ($class->getMethods() as $method) {
|
2011-08-14 18:00:28 +02:00
|
|
|
$serviceArguments =
|
|
|
|
$serviceHeaders = array();
|
2011-07-17 10:46:54 +02:00
|
|
|
$serviceMethod =
|
|
|
|
$serviceReturn = null;
|
|
|
|
|
2011-09-20 22:34:16 +02:00
|
|
|
foreach ($serviceMethodHeaders as $annotation) {
|
|
|
|
$serviceHeaders[$annotation->getValue()] = new Definition\Header(
|
|
|
|
$annotation->getValue(),
|
|
|
|
$this->getArgumentType($method, $annotation)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
|
2011-08-14 18:00:28 +02:00
|
|
|
if ($annotation instanceof Annotation\Header) {
|
2011-09-20 22:34:16 +02:00
|
|
|
$serviceHeaders[$annotation->getValue()] = new Definition\Header(
|
2011-07-17 10:46:54 +02:00
|
|
|
$annotation->getValue(),
|
2011-07-17 15:03:05 +02:00
|
|
|
$this->getArgumentType($method, $annotation)
|
2011-07-17 10:46:54 +02:00
|
|
|
);
|
2011-08-14 18:00:28 +02:00
|
|
|
} elseif ($annotation instanceof Annotation\Param) {
|
|
|
|
$serviceArguments[] = new Definition\Argument(
|
|
|
|
$annotation->getValue(),
|
|
|
|
$this->getArgumentType($method, $annotation)
|
|
|
|
);
|
|
|
|
} elseif ($annotation instanceof Annotation\Method) {
|
2011-07-17 10:46:54 +02:00
|
|
|
if ($serviceMethod) {
|
2011-08-14 18:00:28 +02:00
|
|
|
throw new \LogicException(sprintf('@Soap\Method defined twice for "%s".', $method->getName()));
|
2011-07-17 10:46:54 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 18:00:28 +02:00
|
|
|
$serviceMethod = new Definition\Method(
|
2011-07-17 10:46:54 +02:00
|
|
|
$annotation->getValue(),
|
2012-01-05 17:37:12 +01:00
|
|
|
$this->getController($class, $method, $annotation)
|
2011-07-17 10:46:54 +02:00
|
|
|
);
|
2011-08-14 18:00:28 +02:00
|
|
|
} elseif ($annotation instanceof Annotation\Result) {
|
2011-07-17 10:46:54 +02:00
|
|
|
if ($serviceReturn) {
|
2011-08-14 18:00:28 +02:00
|
|
|
throw new \LogicException(sprintf('@Soap\Result defined twice for "%s".', $method->getName()));
|
2011-07-17 10:46:54 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 18:00:28 +02:00
|
|
|
$serviceReturn = new Definition\Type($annotation->getPhpType(), $annotation->getXmlType());
|
2011-07-17 10:46:54 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
if (!$serviceMethod && (!empty($serviceArguments) || $serviceReturn)) {
|
2011-08-14 18:00:28 +02:00
|
|
|
throw new \LogicException(sprintf('@Soap\Method non-existent for "%s".', $method->getName()));
|
2011-07-17 10:46:54 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
if ($serviceMethod) {
|
|
|
|
$serviceMethod->setArguments($serviceArguments);
|
2011-08-14 18:00:28 +02:00
|
|
|
$serviceMethod->setHeaders($serviceHeaders);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-08-14 18:00:28 +02:00
|
|
|
if (!$serviceReturn) {
|
|
|
|
throw new \LogicException(sprintf('@Soap\Result non-existent for "%s".', $method->getName()));
|
2011-04-09 00:40:31 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-08-14 18:00:28 +02:00
|
|
|
$serviceMethod->setReturn($serviceReturn);
|
|
|
|
|
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-08-14 18:00:28 +02:00
|
|
|
/**
|
|
|
|
* @param \ReflectionMethod $method
|
|
|
|
* @param \BeSimple\SoapBundle\ServiceDefinition\Annotation\Method $annotation
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-01-05 17:37:12 +01:00
|
|
|
private function getController(\ReflectionClass $class, \ReflectionMethod $method, Annotation\Method $annotation)
|
2011-04-09 00:40:31 +02:00
|
|
|
{
|
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 {
|
2012-01-05 17:37:12 +01:00
|
|
|
return $class->name . '::' . $method->name;
|
2011-04-09 00:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 15:03:05 +02:00
|
|
|
/**
|
|
|
|
* @param \ReflectionMethod $method
|
2011-08-14 18:00:28 +02:00
|
|
|
* @param \BeSimple\SoapBundle\ServiceDefinition\Annotation\Param $annotation
|
2011-07-17 15:03:05 +02:00
|
|
|
*
|
2011-07-18 22:43:12 +02:00
|
|
|
* @return \BeSimple\SoapBundle\ServiceDefinition\Type
|
2011-07-17 15:03:05 +02:00
|
|
|
*/
|
2011-08-14 18:00:28 +02:00
|
|
|
private function getArgumentType(\ReflectionMethod $method, Annotation\Param $annotation)
|
2011-07-17 15:03:05 +02:00
|
|
|
{
|
|
|
|
$phpType = $annotation->getPhpType();
|
|
|
|
$xmlType = $annotation->getXmlType();
|
|
|
|
|
|
|
|
if (null === $phpType) {
|
|
|
|
foreach ($method->getParameters() as $param) {
|
|
|
|
if ($param->name === $annotation->getName()) {
|
|
|
|
$phpType = $param->getClass()->name;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-14 18:00:28 +02:00
|
|
|
return new Definition\Type($phpType, $xmlType);
|
2011-07-17 15:03:05 +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.
|
|
|
|
*
|
2012-09-18 22:33:29 +02:00
|
|
|
* @param LoaderResolverInterface $resolver A LoaderResolverInterface instance
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
2012-09-18 22:33:29 +02:00
|
|
|
public function setResolver(LoaderResolverInterface $resolver){
|
|
|
|
|
2011-04-08 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the loader resolver.
|
|
|
|
*
|
2012-09-18 22:33:29 +02:00
|
|
|
* @return LoaderResolverInterface A LoaderResolverInterface instance
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
2012-09-18 22:33:29 +02:00
|
|
|
public function getResolver(){
|
|
|
|
|
2011-04-08 00:41:16 +02:00
|
|
|
}
|
2011-07-17 15:03:05 +02:00
|
|
|
}
|