Fast404ExceptionHtmlSubscriber.php in Drupal 8
File
core/lib/Drupal/Core/EventSubscriber/Fast404ExceptionHtmlSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Component\Utility\Html;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class Fast404ExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
protected $httpKernel;
protected $configFactory;
public function __construct(ConfigFactoryInterface $config_factory, HttpKernelInterface $http_kernel) {
$this->configFactory = $config_factory;
$this->httpKernel = $http_kernel;
}
protected static function getPriority() {
return 200;
}
protected function getHandledFormats() {
return [
'html',
];
}
public function on404(GetResponseForExceptionEvent $event) {
$request = $event
->getRequest();
$config = $this->configFactory
->get('system.performance');
$exclude_paths = $config
->get('fast_404.exclude_paths');
if ($config
->get('fast_404.enabled') && $exclude_paths && !preg_match($exclude_paths, $request
->getPathInfo())) {
$fast_paths = $config
->get('fast_404.paths');
if ($fast_paths && preg_match($fast_paths, $request
->getPathInfo())) {
$fast_404_html = strtr($config
->get('fast_404.html'), [
'@path' => Html::escape($request
->getUri()),
]);
$response = new Response($fast_404_html, Response::HTTP_NOT_FOUND);
$event
->setResponse($response);
}
}
}
}