From 69401745864c2e70104d5246fe6b3b7919c120ec Mon Sep 17 00:00:00 2001 From: lsmith77 Date: Fri, 13 Apr 2012 16:33:24 +0200 Subject: [PATCH] support controllers as services --- Extractor/ApiDocExtractor.php | 39 ++++++++++++++++++++++++++++------- Resources/config/services.xml | 1 + 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 3df058c..f802451 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -13,11 +13,18 @@ namespace Nelmio\ApiDocBundle\Extractor; use Doctrine\Common\Annotations\Reader; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; class ApiDocExtractor { const ANNOTATION_CLASS = 'Nelmio\\ApiDocBundle\\Annotation\\ApiDoc'; + /** + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + private $container; + /** * @var \ymfony\Component\Routing\RouterInterface */ @@ -28,8 +35,9 @@ class ApiDocExtractor */ private $reader; - public function __construct(RouterInterface $router, Reader $reader) + public function __construct(ContainerInterface $container, RouterInterface $router, Reader $reader) { + $this->container = $container; $this->router = $router; $this->reader = $reader; } @@ -46,16 +54,31 @@ class ApiDocExtractor { $array = array(); $resources = array(); + foreach ($this->router->getRouteCollection()->all() as $route) { - preg_match('#(.+)::([\w]+)#', $route->getDefault('_controller'), $matches); - $method = new \ReflectionMethod($matches[1], $matches[2]); - - if ($annot = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) { - if ($annot->isResource()) { - $resources[] = $route->getPattern(); + $method = false; + if (preg_match('#(.+)::([\w]+)#', $route->getDefault('_controller'), $matches)) { + $method = new \ReflectionMethod($matches[1], $matches[2]); + } elseif (preg_match('#(.+):([\w]+)#', $route->getDefault('_controller'), $matches)) { + $controller = $matches[1]; + if ($this->container->has($controller)) { + $this->container->enterScope('request'); + $this->container->set('request', new Request); + $class = get_class($this->container->get($controller)); + $this->container->leaveScope('request'); + $method = new \ReflectionMethod($class, $matches[2]); } + } - $array[] = array('annotation' => $annot, 'route' => $route); + if ($method) { + $annot = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS); + if ($annot) { + if ($annot->isResource()) { + $resources[] = $route->getPattern(); + } + + $array[] = array('annotation' => $annot, 'route' => $route); + } } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index fd7c565..2d3891f 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -10,6 +10,7 @@ +