Chris@0: converters[$id] = $param_converter; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getConverter($converter) { Chris@0: if (isset($this->converters[$converter])) { Chris@0: return $this->converters[$converter]; Chris@0: } Chris@0: else { Chris@0: throw new \InvalidArgumentException(sprintf('No converter has been registered for %s', $converter)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setRouteParameterConverters(RouteCollection $routes) { Chris@0: foreach ($routes->all() as $route) { Chris@0: if (!$parameters = $route->getOption('parameters')) { Chris@0: // Continue with the next route if no parameters have been defined. Chris@0: continue; Chris@0: } Chris@0: Chris@0: // Loop over all defined parameters and look up the right converter. Chris@0: foreach ($parameters as $name => &$definition) { Chris@0: if (isset($definition['converter'])) { Chris@0: // Skip parameters that already have a manually set converter. Chris@0: continue; Chris@0: } Chris@0: Chris@0: foreach (array_keys($this->converters) as $converter) { Chris@0: if ($this->getConverter($converter)->applies($definition, $name, $route)) { Chris@0: $definition['converter'] = $converter; Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Override the parameters array. Chris@0: $route->setOption('parameters', $parameters); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function convert(array $defaults) { Chris@0: /** @var $route \Symfony\Component\Routing\Route */ Chris@0: $route = $defaults[RouteObjectInterface::ROUTE_OBJECT]; Chris@0: Chris@0: // Skip this enhancer if there are no parameter definitions. Chris@0: if (!$parameters = $route->getOption('parameters')) { Chris@0: return $defaults; Chris@0: } Chris@0: Chris@0: // Invoke the registered converter for each parameter. Chris@0: foreach ($parameters as $name => $definition) { Chris@0: if (!isset($defaults[$name])) { Chris@0: // Do not try to convert anything that is already set to NULL. Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (!isset($definition['converter'])) { Chris@0: // Continue if no converter has been specified. Chris@0: continue; Chris@0: } Chris@0: Chris@0: // If a converter returns NULL it means that the parameter could not be Chris@0: // converted. Chris@0: $value = $defaults[$name]; Chris@0: $defaults[$name] = $this->getConverter($definition['converter'])->convert($value, $definition, $name, $defaults); Chris@0: if (!isset($defaults[$name])) { Chris@0: $message = 'The "%s" parameter was not converted for the path "%s" (route name: "%s")'; Chris@0: $route_name = $defaults[RouteObjectInterface::ROUTE_NAME]; Chris@0: throw new ParamNotConvertedException(sprintf($message, $name, $route->getPath(), $route_name), 0, NULL, $route_name, [$name => $value]); Chris@0: } Chris@0: } Chris@0: Chris@0: return $defaults; Chris@0: } Chris@0: Chris@0: }