ParamConversionEnhancer.php in Drupal 9
File
core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php
View source
<?php
namespace Drupal\Core\Routing\Enhancer;
use Drupal\Core\ParamConverter\ParamConverterManagerInterface;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Routing\EnhancerInterface;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class ParamConversionEnhancer implements EnhancerInterface, EventSubscriberInterface {
protected $paramConverterManager;
public function __construct(ParamConverterManagerInterface $param_converter_manager) {
$this->paramConverterManager = $param_converter_manager;
}
public function enhance(array $defaults, Request $request) {
if (!isset($defaults['_raw_variables'])) {
$defaults['_raw_variables'] = $this
->copyRawVariables($defaults);
$defaults = $this->paramConverterManager
->convert($defaults);
}
return $defaults;
}
protected function copyRawVariables(array $defaults) {
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
$variables = array_flip($route
->compile()
->getVariables());
$raw_variables = [];
foreach (array_intersect_key($defaults, $variables) as $key => $value) {
$raw_variables[$key] = $value;
}
return new ParameterBag($raw_variables);
}
public function onException(ExceptionEvent $event) {
$exception = $event
->getThrowable();
if ($exception instanceof ParamNotConvertedException) {
$event
->setThrowable(new NotFoundHttpException($exception
->getMessage(), $exception));
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = [
'onException',
75,
];
return $events;
}
}