ParamConverterManager.php in Drupal 8
File
core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php
View source
<?php
namespace Drupal\Core\ParamConverter;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Routing\RouteCollection;
class ParamConverterManager implements ParamConverterManagerInterface {
protected $converters = [];
public function addConverter(ParamConverterInterface $param_converter, $id) {
$this->converters[$id] = $param_converter;
return $this;
}
public function getConverter($converter) {
if (isset($this->converters[$converter])) {
return $this->converters[$converter];
}
else {
throw new \InvalidArgumentException(sprintf('No converter has been registered for %s', $converter));
}
}
public function setRouteParameterConverters(RouteCollection $routes) {
foreach ($routes
->all() as $route) {
if (!($parameters = $route
->getOption('parameters'))) {
continue;
}
foreach ($parameters as $name => &$definition) {
if (isset($definition['converter'])) {
continue;
}
foreach (array_keys($this->converters) as $converter) {
if ($this
->getConverter($converter)
->applies($definition, $name, $route)) {
$definition['converter'] = $converter;
break;
}
}
}
$route
->setOption('parameters', $parameters);
}
}
public function convert(array $defaults) {
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
if (!($parameters = $route
->getOption('parameters'))) {
return $defaults;
}
foreach ($parameters as $name => $definition) {
if (!isset($defaults[$name])) {
continue;
}
if (!isset($definition['converter'])) {
continue;
}
$value = $defaults[$name];
$defaults[$name] = $this
->getConverter($definition['converter'])
->convert($value, $definition, $name, $defaults);
if (!isset($defaults[$name])) {
$message = 'The "%s" parameter was not converted for the path "%s" (route name: "%s")';
$route_name = $defaults[RouteObjectInterface::ROUTE_NAME];
throw new ParamNotConvertedException(sprintf($message, $name, $route
->getPath(), $route_name), 0, NULL, $route_name, [
$name => $value,
]);
}
}
return $defaults;
}
}