NelmioApiDocBundle/EventListener/RequestListener.php

72 lines
1.8 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;
/**
* @var string
*/
protected $parameter;
public function __construct(ApiDocExtractor $extractor, FormatterInterface $formatter, $parameter)
2012-04-11 20:00:21 +02:00
{
$this->extractor = $extractor;
2012-04-11 20:00:21 +02:00
$this->formatter = $formatter;
$this->parameter = $parameter;
2012-04-11 20:00:21 +02:00
}
/**
* {@inheritdoc}
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if (!$request->query->has($this->parameter)) {
2012-04-11 20:00:21 +02:00
return;
}
2012-04-13 15:12:21 +02:00
$controller = $request->attributes->get('_controller');
$route = $request->attributes->get('_route');
2012-04-11 20:00:21 +02:00
if (null !== $annotation = $this->extractor->get($controller, $route)) {
$result = $this->formatter->formatOne($annotation);
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
}
}
}