class ParamConversionEnhancer in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php \Drupal\Core\Routing\Enhancer\ParamConversionEnhancer
Provides a route enhancer that handles parameter conversion.
Hierarchy
- class \Drupal\Core\Routing\Enhancer\ParamConversionEnhancer implements RouteEnhancerInterface, EventSubscriberInterface
Expanded class hierarchy of ParamConversionEnhancer
1 file declares its use of ParamConversionEnhancer
- ParamConversionEnhancerTest.php in core/
tests/ Drupal/ Tests/ Core/ Enhancer/ ParamConversionEnhancerTest.php - Contains \Drupal\Tests\Core\Enhancer\ParamConversionEnhancerTest.
1 string reference to 'ParamConversionEnhancer'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses ParamConversionEnhancer
File
- core/
lib/ Drupal/ Core/ Routing/ Enhancer/ ParamConversionEnhancer.php, line 24 - Contains \Drupal\Core\Routing\Enhancer\ParamConversionEnhancer.
Namespace
Drupal\Core\Routing\EnhancerView source
class ParamConversionEnhancer implements RouteEnhancerInterface, EventSubscriberInterface {
/**
* The parameter conversion manager.
*
* @var \Drupal\Core\ParamConverter\ParamConverterManagerInterface
*/
protected $paramConverterManager;
/**
* Constructs a new ParamConversionEnhancer.
*
* @param \Drupal\Core\ParamConverter\ParamConverterManagerInterface $param_converter_manager
* The parameter conversion manager.
*/
public function __construct(ParamConverterManagerInterface $param_converter_manager) {
$this->paramConverterManager = $param_converter_manager;
}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request) {
// Just run the parameter conversion once per request.
if (!isset($defaults['_raw_variables'])) {
$defaults['_raw_variables'] = $this
->copyRawVariables($defaults);
$defaults = $this->paramConverterManager
->convert($defaults);
}
return $defaults;
}
/**
* Store a backup of the raw values that corresponding to the route pattern.
*
* @param array $defaults
* The route defaults array.
*
* @return \Symfony\Component\HttpFoundation\ParameterBag
*/
protected function copyRawVariables(array $defaults) {
/** @var $route \Symfony\Component\Routing\Route */
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
$variables = array_flip($route
->compile()
->getVariables());
// Foreach will copy the values from the array it iterates. Even if they
// are references, use it to break them. This avoids any scenarios where raw
// variables also get replaced with converted values.
$raw_variables = array();
foreach (array_intersect_key($defaults, $variables) as $key => $value) {
$raw_variables[$key] = $value;
}
return new ParameterBag($raw_variables);
}
/**
* Catches failed parameter conversions and throw a 404 instead.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
*/
public function onException(GetResponseForExceptionEvent $event) {
$exception = $event
->getException();
if ($exception instanceof ParamNotConvertedException) {
$event
->setException(new NotFoundHttpException($exception
->getMessage(), $exception));
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = array(
'onException',
75,
);
return $events;
}
/**
* {@inheritdoc}
*/
public function applies(Route $route) {
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ParamConversionEnhancer:: |
protected | property | The parameter conversion manager. | |
ParamConversionEnhancer:: |
public | function |
Declares if the route enhancer applies to the given route. Overrides RouteEnhancerInterface:: |
|
ParamConversionEnhancer:: |
protected | function | Store a backup of the raw values that corresponding to the route pattern. | |
ParamConversionEnhancer:: |
public | function |
Update the defaults based on its own data and the request. Overrides RouteEnhancerInterface:: |
|
ParamConversionEnhancer:: |
public static | function |
Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface:: |
|
ParamConversionEnhancer:: |
public | function | Catches failed parameter conversions and throw a 404 instead. | |
ParamConversionEnhancer:: |
public | function | Constructs a new ParamConversionEnhancer. |