PhpArrayContainer::resolveServicesAndParameters

protected PhpArrayContainer::resolveServicesAndParameters($arguments)

Resolves arguments that represent services or variables to the real values.

Parameters

array|\stdClass $arguments: The arguments to resolve.

Return value

array The resolved arguments.

Throws

\Symfony\Component\DependencyInjection\Exception\RuntimeException If a parameter/service could not be resolved.

\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException If an unknown type is met while resolving parameters and services.

Overrides Container::resolveServicesAndParameters

File

core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php, line 185

Class

PhpArrayContainer
Provides a container optimized for Drupal's needs.

Namespace

Drupal\Component\DependencyInjection

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
protected function resolveServicesAndParameters($arguments) {
  // This method is different from the parent method only for the following
  // cases:
  // - A service is denoted by '@service' and not by a \stdClass object.
  // - A parameter is denoted by '%parameter%' and not by a \stdClass object.
  // - The depth of the tree representing the arguments is not known in
  //   advance, so it needs to be fully traversed recursively.
  foreach ($arguments as $key => $argument) {
    if ($argument instanceof \stdClass) {
      $type = $argument->type;
 
      // Private services are a special flavor: In case a private service is
      // only used by one other service, the ContainerBuilder uses a
      // Definition object as an argument, which does not have an ID set.
      // Therefore the format uses a \stdClass object to store the definition
      // and to be able to create the service on the fly.
      //
      // Note: When constructing a private service by hand, 'id' must be set.
      //
      // The PhpArrayDumper just uses the hash of the private service
      // definition to generate a unique ID.
      //
      // @see \Drupal\Component\DependecyInjection\Dumper\OptimizedPhpArrayDumper::getPrivateServiceCall
      if ($type == 'private_service') {
        $id = $argument->id;
 
        // Check if the private service already exists - in case it is shared.
        if (!empty($argument->shared) && isset($this->privateServices[$id])) {
          $arguments[$key] = $this->privateServices[$id];
          continue;
        }
 
        // Create a private service from a service definition.
        $arguments[$key] = $this->createService($argument->value, $id);
        if (!empty($argument->shared)) {
          $this->privateServices[$id] = $arguments[$key];
        }
 
        continue;
      }
 
      if ($type !== NULL) {
        throw new InvalidArgumentException("Undefined type '$type' while resolving parameters and services.");
      }
    }
 
    if (is_array($argument)) {
      $arguments[$key] = $this->resolveServicesAndParameters($argument);
      continue;
    }
 
    if (!is_string($argument)) {
      continue;
    }
 
    // Resolve parameters.
    if ($argument[0] === '%') {
      $name = substr($argument, 1, -1);
      if (!isset($this->parameters[$name])) {
        $arguments[$key] = $this->getParameter($name);
        // This can never be reached as getParameter() throws an Exception,
        // because we already checked that the parameter is not set above.
      }
      $argument = $this->parameters[$name];
      $arguments[$key] = $argument;
    }
 
    // Resolve services.
    if ($argument[0] === '@') {
      $id = substr($argument, 1);
      $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
      if ($id[0] === '?') {
        $id = substr($id, 1);
        $invalid_behavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
      }
      if (isset($this->services[$id])) {
        $arguments[$key] = $this->services[$id];
      }
      else {
        $arguments[$key] = $this->get($id, $invalid_behavior);
      }
    }
  }
 
  return $arguments;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.