ParamConverterManager.php in Zircon Profile 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 = array();
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;
}
$defaults[$name] = $this
->getConverter($definition['converter'])
->convert($defaults[$name], $definition, $name, $defaults);
if (!isset($defaults[$name])) {
throw new ParamNotConvertedException(sprintf('The "%s" parameter was not converted for the path "%s" (route name: "%s")', $name, $route
->getPath(), $defaults[RouteObjectInterface::ROUTE_NAME]));
}
}
return $defaults;
}
}