diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php index 6d2a2d1..eba7121 100644 --- a/Command/DumpCommand.php +++ b/Command/DumpCommand.php @@ -9,11 +9,6 @@ use Symfony\Component\Console\Output\OutputInterface; class DumpCommand extends ContainerAwareCommand { - /** - * @var string - */ - protected $annotationClass = 'Nelmio\\ApiBundle\\Annotation\\ApiDoc'; - /** * @var array */ @@ -43,22 +38,13 @@ class DumpCommand extends ContainerAwareCommand $formatter = $this->getContainer()->get(sprintf('nelmio.api.formatter.%s_formatter', $format)); } - $results = array(); - foreach ($routeCollection->all() as $route) { - preg_match('#(.+)::([\w]+)#', $route->getDefault('_controller'), $matches); - $method = new \ReflectionMethod($matches[1], $matches[2]); - - if ($annot = $this->getContainer()->get('annotation_reader')->getMethodAnnotation($method, $this->annotationClass)) { - $results[] = $formatter->format($annot, $route); - } - } + $extractedDoc = $this->getContainer()->get('nelmio.api.extractor.api_doc_extractor')->all(); + $result = $formatter->format($extractedDoc); if ('json' === $format) { - $output->writeln(json_encode($results)); + $output->writeln(json_encode($result)); } else { - foreach ($results as $result) { - $output->writeln($result); - } + $output->writeln($result); } } } diff --git a/Controller/ApiDocController.php b/Controller/ApiDocController.php new file mode 100644 index 0000000..70533ab --- /dev/null +++ b/Controller/ApiDocController.php @@ -0,0 +1,17 @@ +get('nelmio.api.extractor.api_doc_extractor')->all(); + $htmlContent = $this->get('nelmio.api.formatter.html_formatter')->format($extractedDoc); + + return new Response($htmlContent); + } +} diff --git a/DependencyInjection/NelmioApiExtension.php b/DependencyInjection/NelmioApiExtension.php index 15b249a..7fb67c1 100644 --- a/DependencyInjection/NelmioApiExtension.php +++ b/DependencyInjection/NelmioApiExtension.php @@ -15,6 +15,7 @@ class NelmioApiExtension extends Extension public function load(array $configs, ContainerBuilder $container) { $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('formatters.xml'); $loader->load('request_listener.xml'); $loader->load('services.xml'); } diff --git a/EventListener/RequestListener.php b/EventListener/RequestListener.php index 1812236..3c2cd6b 100644 --- a/EventListener/RequestListener.php +++ b/EventListener/RequestListener.php @@ -2,28 +2,22 @@ namespace Nelmio\ApiBundle\EventListener; -use Doctrine\Common\Annotations\Reader; +use Nelmio\ApiBundle\Extractor\ApiDocExtractor; use Nelmio\ApiBundle\Formatter\FormatterInterface; -use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\Routing\RouterInterface; class RequestListener { - protected $annotationClass = 'Nelmio\\ApiBundle\\Annotation\\ApiDoc'; - - protected $reader; - - protected $router; + protected $extractor; protected $formatter; - public function __construct(Reader $reader, RouterInterface $router, FormatterInterface $formatter) + public function __construct(ApiDocExtractor $extractor, FormatterInterface $formatter) { - $this->reader = $reader; - $this->router = $router; + $this->extractor = $extractor; $this->formatter = $formatter; } @@ -42,16 +36,15 @@ class RequestListener return; } - preg_match('#(.+)::([\w]+)#', $request->get('_controller'), $matches); - $method = new \ReflectionMethod($matches[1], $matches[2]); - $route = $request->get('_route'); + $controller = $request->get('_controller'); + $route = $request->get('_route'); - if ($annot = $this->reader->getMethodAnnotation($method, $this->annotationClass)) { - if ($route = $this->router->getRouteCollection()->get($route)) { - $result = $this->formatter->format($annot, $route); + if (null !== $array = $this->extractor->get($controller, $route)) { + $result = $this->formatter->formatOne($array['annotation'], $array['route']); - $event->setResponse(new JsonResponse($result)); - } + $event->setResponse(new Response($result, 200, array( + 'Content-Type' => 'text/html' + ))); } } } diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php new file mode 100644 index 0000000..60b02b9 --- /dev/null +++ b/Extractor/ApiDocExtractor.php @@ -0,0 +1,56 @@ +router = $router; + $this->reader = $reader; + } + + public function all() + { + $array = 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)) { + $array[] = array('annotation' => $annot, 'route' => $route); + } + } + + return $array; + } + + public function get($controller, $route) + { + preg_match('#(.+)::([\w]+)#', $controller, $matches); + $method = new \ReflectionMethod($matches[1], $matches[2]); + + if ($annot = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) { + if ($route = $this->router->getRouteCollection()->get($route)) { + return array('annotation' => $annot, 'route' => $route); + } + } + + return null; + } +} diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index 824b97f..49e3175 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -41,8 +41,7 @@ class FormTypeParser } } - $parameters[] = array( - 'name' => $name, + $parameters[$name] = array( 'type' => $bestType, 'is_required' => $b->getRequired() ); diff --git a/Resources/config/request_listener.xml b/Resources/config/request_listener.xml index 1dbf15e..2a9a6bb 100644 --- a/Resources/config/request_listener.xml +++ b/Resources/config/request_listener.xml @@ -9,9 +9,8 @@ - - - + + diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml new file mode 100644 index 0000000..a5ab609 --- /dev/null +++ b/Resources/config/routing.yml @@ -0,0 +1,5 @@ +nelmio_api_api_doc_index: + pattern: /api/doc + defaults: { _controller: NelmioApiBundle:ApiDoc:index } + requirements: + _method: GET diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 51d2351..78c1147 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,23 +4,14 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - Nelmio\ApiBundle\Parser\FormTypeParser - Nelmio\ApiBundle\Formatter\AbstractFormatter - Nelmio\ApiBundle\Formatter\MarkdownFormatter - Nelmio\ApiBundle\Formatter\SimpleFormatter + Nelmio\ApiBundle\Extractor\ApiDocExtractor - - + + + - - - - -