You are here

public function Spec::instantiate in Little helpers 7.2

Create a new class instance based on the spec.

Parameters

array $kwargs: Associative array of keyword arguments that may be passed as arguments.

File

src/Services/Spec.php, line 105

Class

Spec
A class instantiation spec.

Namespace

Drupal\little_helpers\Services

Code

public function instantiate(array $kwargs = []) {
  $class = $this->class;
  $arguments = $this
    ->resolveArguments($this->arguments, $kwargs);
  if ($method = $this->constructor) {
    $instance = $class::$method(...$arguments);
  }
  else {
    $instance = new $class(...$arguments);
  }
  foreach ($this->calls as $call) {
    list($method, $arguments) = $call;
    $arguments = $this
      ->resolveArguments($arguments, $kwargs);
    $instance
      ->{$method}(...$arguments);
  }
  return $instance;
}