You are here

class ParamConverterManager in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager

Manages converter services for converting request parameters to full objects.

A typical use case for this would be upcasting (converting) a node id to a node entity.

Hierarchy

Expanded class hierarchy of ParamConverterManager

1 file declares its use of ParamConverterManager
ParamConverterManagerTest.php in core/tests/Drupal/Tests/Core/ParamConverter/ParamConverterManagerTest.php
Contains \Drupal\Tests\Core\ParamConverter\ParamConverterManagerTest.
1 string reference to 'ParamConverterManager'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ParamConverterManager
paramconverter_manager in core/core.services.yml
Drupal\Core\ParamConverter\ParamConverterManager

File

core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php, line 19
Contains \Drupal\Core\ParamConverter\ParamConverterManager.

Namespace

Drupal\Core\ParamConverter
View source
class ParamConverterManager implements ParamConverterManagerInterface {

  /**
   * Array of loaded converter services keyed by their ids.
   *
   * @var array
   */
  protected $converters = array();

  /**
   * {@inheritdoc}
   */
  public function addConverter(ParamConverterInterface $param_converter, $id) {
    $this->converters[$id] = $param_converter;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  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));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setRouteParameterConverters(RouteCollection $routes) {
    foreach ($routes
      ->all() as $route) {
      if (!($parameters = $route
        ->getOption('parameters'))) {

        // Continue with the next route if no parameters have been defined.
        continue;
      }

      // Loop over all defined parameters and look up the right converter.
      foreach ($parameters as $name => &$definition) {
        if (isset($definition['converter'])) {

          // Skip parameters that already have a manually set converter.
          continue;
        }
        foreach (array_keys($this->converters) as $converter) {
          if ($this
            ->getConverter($converter)
            ->applies($definition, $name, $route)) {
            $definition['converter'] = $converter;
            break;
          }
        }
      }

      // Override the parameters array.
      $route
        ->setOption('parameters', $parameters);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function convert(array $defaults) {

    /** @var $route \Symfony\Component\Routing\Route */
    $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];

    // Skip this enhancer if there are no parameter definitions.
    if (!($parameters = $route
      ->getOption('parameters'))) {
      return $defaults;
    }

    // Invoke the registered converter for each parameter.
    foreach ($parameters as $name => $definition) {
      if (!isset($defaults[$name])) {

        // Do not try to convert anything that is already set to NULL.
        continue;
      }
      if (!isset($definition['converter'])) {

        // Continue if no converter has been specified.
        continue;
      }

      // If a converter returns NULL it means that the parameter could not be
      // converted.
      $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParamConverterManager::$converters protected property Array of loaded converter services keyed by their ids.
ParamConverterManager::addConverter public function Registers a parameter converter with the manager. Overrides ParamConverterManagerInterface::addConverter
ParamConverterManager::convert public function Invokes the registered converter for each defined parameter on a route. Overrides ParamConverterManagerInterface::convert
ParamConverterManager::getConverter public function Lazy-loads converter services. Overrides ParamConverterManagerInterface::getConverter
ParamConverterManager::setRouteParameterConverters public function Saves a list of applicable converters to each route. Overrides ParamConverterManagerInterface::setRouteParameterConverters