protected function ArgumentsResolver::getArgument

protected ArgumentsResolver::getArgument(\ReflectionParameter $parameter)

Gets the argument value for a parameter.

Parameters

\ReflectionParameter $parameter: The parameter of a callable to get the value for.

Return value

mixed The value of the requested parameter value.

Throws

\RuntimeException Thrown when there is a missing parameter.

File

core/lib/Drupal/Component/Utility/ArgumentsResolver.php, line 71

Class

ArgumentsResolver
Resolves the arguments to pass to a callable.

Namespace

Drupal\Component\Utility

Code

protected function getArgument(\ReflectionParameter $parameter) {
  $parameter_type_hint = $parameter->getClass();
  $parameter_name = $parameter->getName();

  // If the argument exists and is NULL, return it, regardless of
  // parameter type hint.
  if (!isset($this->objects[$parameter_name]) && array_key_exists($parameter_name, $this->objects)) {
    return NULL;
  }

  if ($parameter_type_hint) {
    // If the argument exists and complies with the type hint, return it.
    if (isset($this->objects[$parameter_name]) && is_object($this->objects[$parameter_name]) && $parameter_type_hint->isInstance($this->objects[$parameter_name])) {
      return $this->objects[$parameter_name];
    }
    // Otherwise, resolve wildcard arguments by type matching.
    foreach ($this->wildcards as $wildcard) {
      if ($parameter_type_hint->isInstance($wildcard)) {
        return $wildcard;
      }
    }
  }
  elseif (isset($this->scalars[$parameter_name])) {
    return $this->scalars[$parameter_name];
  }

  // If the callable provides a default value, use it.
  if ($parameter->isDefaultValueAvailable()) {
    return $parameter->getDefaultValue();
  }

  // Can't resolve it: call a method that throws an exception or can be
  // overridden to do something else.
  return $this->handleUnresolvedArgument($parameter);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!ArgumentsResolver.php/function/ArgumentsResolver::getArgument/8.1.x