Skip to content

Commit

Permalink
Move expandParams to the blueprint, since it is part of the object co…
Browse files Browse the repository at this point in the history
…nstruction.
  • Loading branch information
frederikbosch committed May 16, 2024
1 parent d3175ef commit f00bb95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 60 deletions.
55 changes: 24 additions & 31 deletions src/Resolver/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function ($val) use ($resolver) {

return $val;
},
array_values($this->params)
array_values($this->expandParams())
)
);

Expand Down Expand Up @@ -177,36 +177,6 @@ public function withParamSettings(array $paramSettings): self
return $clone;
}

/**
* @return array
*/
public function getParamSettings(): array
{
return $this->paramSettings;
}

/**
* @param array $params
* @return Blueprint
*/
public function replaceParams(array $params): self
{
$clone = clone $this;
$clone->params = $params;
return $clone;
}

/**
* @param array $params
* @return Blueprint
*/
public function withParams(array $params): self
{
$clone = clone $this;
$clone->params = \array_merge($this->params, $params);
return $clone;
}

/**
*
* Merges the setters with overrides; also invokes Lazy values.
Expand Down Expand Up @@ -274,4 +244,27 @@ private function mergeParams(Blueprint $mergeBlueprint): array

return $params;
}

/**
* Expands variadic parameters onto the end of a contructor parameters array.
*/
private function expandParams(): array
{
$params = $this->getParams();

$variadicParams = [];
foreach ($this->paramSettings as $paramName => $isVariadic) {
if ($isVariadic && is_array($params[$paramName])) {
$variadicParams = array_merge($variadicParams, $params[$paramName]);
unset($params[$paramName]);
break; // There can only be one
}

if ($params[$paramName] instanceof DefaultValueParam) {
$params[$paramName] = $params[$paramName]->getValue();
}
}

return array_merge($params, array_values($variadicParams));
}
}
31 changes: 2 additions & 29 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function resolve(Blueprint $blueprint, array $contextualBlueprints = []):
{
if ($contextualBlueprints === []) {
return call_user_func(
$this->expandParams($this->getUnified($blueprint->getClassName())->merge($blueprint)),
$this->getUnified($blueprint->getClassName())->merge($blueprint),
$this,
);
}
Expand Down Expand Up @@ -252,7 +252,7 @@ public function resolve(Blueprint $blueprint, array $contextualBlueprints = []):
}

$resolved = call_user_func(
$this->expandParams($this->getUnified($blueprint->getClassName())->merge($blueprint)),
$this->getUnified($blueprint->getClassName())->merge($blueprint),
$this,
);

Expand Down Expand Up @@ -511,31 +511,4 @@ private function getParamSettings(string $class): array

return $unified;
}

/**
* Expands variadic parameters onto the end of a contructor parameters array.
*
* @param Blueprint $blueprint The blueprint to expand parameters for.
*
* @return Blueprint The blueprint with expanded constructor parameters.
*/
protected function expandParams(Blueprint $blueprint): Blueprint
{
$params = $blueprint->getParams();

$variadicParams = [];
foreach ($blueprint->getParamSettings() as $paramName => $isVariadic) {
if ($isVariadic && is_array($params[$paramName])) {
$variadicParams = array_merge($variadicParams, $params[$paramName]);
unset($params[$paramName]);
break; // There can only be one
}

if ($params[$paramName] instanceof DefaultValueParam) {
$params[$paramName] = $params[$paramName]->getValue();
}
}

return $blueprint->replaceParams(array_merge($params, array_values($variadicParams)));
}
}

0 comments on commit f00bb95

Please sign in to comment.