Skip to content

Commit

Permalink
Remove ArrayDefinitionBuilder (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Nov 18, 2021
1 parent b61ce93 commit 2284a40
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 153 deletions.
98 changes: 96 additions & 2 deletions src/ArrayDefinition.php
Expand Up @@ -6,8 +6,10 @@

use Psr\Container\ContainerInterface;
use Yiisoft\Definitions\Contract\DefinitionInterface;
use Yiisoft\Definitions\Infrastructure\ArrayDefinitionBuilder;
use Yiisoft\Definitions\Exception\InvalidConfigException;

use Yiisoft\Definitions\Infrastructure\DefinitionExtractor;
use Yiisoft\Definitions\Infrastructure\DefinitionResolver;
use function count;

/**
Expand Down Expand Up @@ -129,7 +131,99 @@ public function getMethodsAndProperties(): array

public function resolve(ContainerInterface $container): object
{
return ArrayDefinitionBuilder::getInstance()->build($container, $this->referenceContainer, $this);
$class = $this->getClass();
$dependencies = DefinitionExtractor::getInstance()->fromClassName($class);
$constructorArguments = $this->getConstructorArguments();

$this->injectArguments($dependencies, $constructorArguments);

$resolved = DefinitionResolver::resolveArray($container, $this->referenceContainer, $dependencies);

/** @psalm-suppress MixedMethodCall */
$object = new $class(...array_values($resolved));

foreach ($this->getMethodsAndProperties() as $item) {
/** @var mixed $value */
[$type, $name, $value] = $item;
/** @var mixed */
$value = DefinitionResolver::resolve($container, $this->referenceContainer, $value);
if ($type === self::TYPE_METHOD) {
/** @var mixed */
$setter = call_user_func_array([$object, $name], $value);
if ($setter instanceof $object) {
/** @var object */
$object = $setter;
}
} elseif ($type === self::TYPE_PROPERTY) {
$object->$name = $value;
}
}

return $object;
}

/**
* @psalm-param array<string, DefinitionInterface> $dependencies
*
* @throws InvalidConfigException
*/
private function injectArguments(array &$dependencies, array $arguments): void
{
$isIntegerIndexed = $this->isIntegerIndexed($arguments);
$dependencyIndex = 0;
$usedArguments = [];
$isVariadic = false;
foreach ($dependencies as $key => &$value) {
if ($value instanceof ParameterDefinition && $value->isVariadic()) {
$isVariadic = true;
}
$index = $isIntegerIndexed ? $dependencyIndex : $key;
if (array_key_exists($index, $arguments)) {
$value = DefinitionResolver::ensureResolvable($arguments[$index]);
$usedArguments[$index] = 1;
}
$dependencyIndex++;
}
unset($value);
if ($isVariadic) {
/** @var mixed $value */
foreach ($arguments as $index => $value) {
if (!isset($usedArguments[$index])) {
$dependencies[$index] = DefinitionResolver::ensureResolvable($value);
}
}
}
/** @psalm-var array<string, DefinitionInterface> $dependencies */
}

/**
* @throws InvalidConfigException
*/
private function isIntegerIndexed(array $arguments): bool
{
$hasStringIndex = false;
$hasIntegerIndex = false;

foreach ($arguments as $index => $_argument) {
if (is_string($index)) {
$hasStringIndex = true;
if ($hasIntegerIndex) {
break;
}
} else {
$hasIntegerIndex = true;
if ($hasStringIndex) {
break;
}
}
}
if ($hasIntegerIndex && $hasStringIndex) {
throw new InvalidConfigException(
'Arguments indexed both by name and by position are not allowed in the same array.'
);
}

return $hasIntegerIndex;
}

/**
Expand Down
151 changes: 0 additions & 151 deletions src/Infrastructure/ArrayDefinitionBuilder.php

This file was deleted.

0 comments on commit 2284a40

Please sign in to comment.