private function expand($param, array $share = [], $createFromString = false)
{
if (is_array($param) && isset($param['instance'])) {
// Call or return the value sored under the key 'instance'
// For ['instance' => ['className', 'methodName'] construct the instance before calling it
$args = isset($param['params']) ? $this->expand($param['params']) : [];
if (is_array($param['instance'])) {
$param['instance'][0] = $this->expand($param['instance'][0], $share, true);
}
if (is_callable($param['instance'])) {
return call_user_func($param['instance'], ...$args);
} else {
return $this->create($param['instance'], array_merge($args, $share));
}
} else {
if (is_array($param)) {
foreach ($param as $name => $value) {
$param[$name] = $this->expand($value, $share);
}
}
}
// 'instance' wasn't found, return the value unchanged
return is_string($param) && $createFromString ? $this->create($param) : $param;
}