namespace Symfony\Component\HttpKernel\EventListener;
use Psr\Log\LoggerInterface;
+use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
+use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelEvents;
+use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
+use Symfony\Component\Routing\Exception\NoConfigurationException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
* Initializes the context from the request and sets request attributes based on a matching route.
*
* @author Fabien Potencier <fabien@symfony.com>
+ * @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class RouterListener implements EventSubscriberInterface
{
private $context;
private $logger;
private $requestStack;
+ private $projectDir;
+ private $debug;
/**
- * Constructor.
- *
* @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
* @param RequestStack $requestStack A RequestStack instance
* @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
* @param LoggerInterface|null $logger The logger
+ * @param string $projectDir
+ * @param bool $debug
*
* @throws \InvalidArgumentException
*/
- public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null)
+ public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null, $projectDir = null, $debug = true)
{
if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
$this->context = $context ?: $matcher->getContext();
$this->requestStack = $requestStack;
$this->logger = $logger;
+ $this->projectDir = $projectDir;
+ $this->debug = $debug;
}
private function setCurrentRequest(Request $request = null)
{
if (null !== $request) {
- $this->context->fromRequest($request);
+ try {
+ $this->context->fromRequest($request);
+ } catch (\UnexpectedValueException $e) {
+ throw new BadRequestHttpException($e->getMessage(), $e, $e->getCode());
+ }
}
}
unset($parameters['_route'], $parameters['_controller']);
$request->attributes->set('_route_params', $parameters);
} catch (ResourceNotFoundException $e) {
+ if ($this->debug && $e instanceof NoConfigurationException) {
+ $event->setResponse($this->createWelcomeResponse());
+
+ return;
+ }
+
$message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
if ($referer = $request->headers->get('referer')) {
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
);
}
+
+ private function createWelcomeResponse()
+ {
+ $version = Kernel::VERSION;
+ $baseDir = realpath($this->projectDir).DIRECTORY_SEPARATOR;
+ $docVersion = substr(Kernel::VERSION, 0, 3);
+
+ ob_start();
+ include __DIR__.'/../Resources/welcome.html.php';
+
+ return new Response(ob_get_clean(), Response::HTTP_NOT_FOUND);
+ }
}