<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-07-15
* Time: 20:57
*/
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class DisabledRouteSubscriber implements EventSubscriberInterface
{
/**
* Feature key in route defaults
*/
const DISABLED_ROUTE_KEY = '_route_disabled';
/**
* Filter by routing metadata
*
* @throws NotFoundHttpException
*/
public function onKernelController(ControllerEvent $event): void
{
if (!$event->getRequest()->attributes->has(self::DISABLED_ROUTE_KEY)) {
return;
}
if (true === $event->getRequest()->attributes->get(self::DISABLED_ROUTE_KEY)) {
throw new NotFoundHttpException('Route in this app is disabled.');
}
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}