You are here

protected function ArgumentsResolver::getArgument in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Utility/ArgumentsResolver.php \Drupal\Component\Utility\ArgumentsResolver::getArgument()

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.

1 call to ArgumentsResolver::getArgument()
ArgumentsResolver::getArguments in core/lib/Drupal/Component/Utility/ArgumentsResolver.php
Gets arguments suitable for passing to the given callable.

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 = Reflection::getParameterClassName($parameter);
  $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) {
    $parameter_type_hint = new \ReflectionClass($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);
}