NelmioApiDocBundle/EventListener/RequestListener.php

66 lines
1.7 KiB
PHP
Raw Normal View History

2012-04-11 20:00:21 +02:00
<?php
2012-04-13 11:03:05 +02:00
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2012-04-12 18:37:42 +02:00
namespace Nelmio\ApiDocBundle\EventListener;
2012-04-11 20:00:21 +02:00
2012-04-12 18:37:42 +02:00
use Nelmio\ApiDocBundle\Extractor\ApiDocExtractor;
use Nelmio\ApiDocBundle\Formatter\FormatterInterface;
use Symfony\Component\HttpFoundation\Response;
2012-04-11 20:00:21 +02:00
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class RequestListener
{
2012-04-12 17:48:21 +02:00
/**
2012-04-12 18:37:42 +02:00
* @var \Nelmio\ApiDocBundle\Extractor\ApiDocExtractor
2012-04-12 17:48:21 +02:00
*/
protected $extractor;
2012-04-11 20:00:21 +02:00
2012-04-12 17:48:21 +02:00
/**
2012-04-12 18:37:42 +02:00
* @var \Nelmio\ApiDocBundle\Formatter\FormatterInterface
2012-04-12 17:48:21 +02:00
*/
2012-04-11 20:00:21 +02:00
protected $formatter;
public function __construct(ApiDocExtractor $extractor, FormatterInterface $formatter)
2012-04-11 20:00:21 +02:00
{
$this->extractor = $extractor;
2012-04-11 20:00:21 +02:00
$this->formatter = $formatter;
}
/**
* {@inheritdoc}
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if (!$request->get('_doc')) {
return;
}
$controller = $request->get('_controller');
$route = $request->get('_route');
2012-04-11 20:00:21 +02:00
if (null !== $array = $this->extractor->get($controller, $route)) {
$result = $this->formatter->formatOne($array['annotation'], $array['route']);
2012-04-11 20:00:21 +02:00
$event->setResponse(new Response($result, 200, array(
'Content-Type' => 'text/html'
)));
2012-04-11 20:00:21 +02:00
}
}
}