From f0e84a96d7a14f2fb1eb3340855ab200c74f5724 Mon Sep 17 00:00:00 2001 From: lsmith77 Date: Fri, 13 Apr 2012 17:56:09 +0200 Subject: [PATCH] also support services as controller in ApiDocExtractor::get() --- Extractor/ApiDocExtractor.php | 55 ++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index f802451..069601c 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -56,19 +56,7 @@ class ApiDocExtractor $resources = array(); foreach ($this->router->getRouteCollection()->all() as $route) { - $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]); - } - } + $method = $this->getReflectionMethod($route->getDefault('_controller')); if ($method) { $annot = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS); @@ -125,6 +113,38 @@ class ApiDocExtractor return $array; } + /** + * Returns the ReflectionMethod for the given controller string + * + * @param string $controller + * @return ReflectionMethod|null + */ + public function getReflectionMethod($controller) + { + if (preg_match('#(.+)::([\w]+)#', $controller, $matches)) { + $class = $matches[1]; + $method = $matches[2]; + } elseif (preg_match('#(.+):([\w]+)#', $controller, $matches)) { + $controller = $matches[1]; + $method = $matches[2]; + 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'); + } + } + + if (isset($class) && isset($method)) { + try { + return new \ReflectionMethod($class, $method); + } catch (\ReflectionException $e) { + } + } + + return null; + } + /** * Returns an array containing two values with the following keys: * - annotation @@ -136,13 +156,8 @@ class ApiDocExtractor */ public function get($controller, $route) { - if (!preg_match('#(.+)::([\w]+)#', $controller, $matches)) { - return null; - } - - try { - $method = new \ReflectionMethod($matches[1], $matches[2]); - } catch (\ReflectionException $e) { + $method = $this->getReflectionMethod($controller); + if (!$method) { return null; }