Skip to content

Commit

Permalink
Remove DependencyResolverInterface (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
yiiliveext committed Sep 18, 2021
1 parent ff76264 commit 33d9d1e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/DependencyResolver.php
Expand Up @@ -24,7 +24,7 @@
/**
* @internal
*/
final class DependencyResolver implements DependencyResolverInterface
final class DependencyResolver implements ContainerInterface
{
private ?ContainerInterface $container;

Expand Down Expand Up @@ -60,26 +60,22 @@ public function __construct(?ContainerInterface $container)
*
* @psalm-suppress InvalidThrow
*/
public function resolve(string $id)
public function get($id)
{
if (isset($this->definitions[$id])) {
return $this->getFromFactory($id);
}

if ($this->container !== null && $this->container->has($id)) {
return $this->container->get($id);
}

if (class_exists($id)) {
return $this->getFromFactory($id);
}

throw new NotInstantiableClassException($id);
}

public function resolveReference(string $id)
public function has($id): bool
{
return $this->getFromFactory($id);
return isset($this->definitions[$id]) || ($this->container !== null && $this->container->has($id)) || class_exists($id);
}

/**
Expand Down Expand Up @@ -112,13 +108,16 @@ public function create($config)
$definition = $this->createDefinition($config);

if ($definition instanceof ArrayDefinition) {
$definition->setReferenceContainer($this);
$this->creatingIds[$definition->getClass()] = 1;
}
try {
return $definition->resolve($this);
$container = ($this->container === null || $definition instanceof ReferenceInterface) ? $this : $this->container;
return $definition->resolve($container);
} finally {
if ($definition instanceof ArrayDefinition) {
unset($this->creatingIds[$definition->getClass()]);
$definition->setReferenceContainer(null);
}
}
}
Expand Down Expand Up @@ -165,11 +164,27 @@ private function getFromFactory(string $id)
));
}

$definition = $this->getDefinition($id);
if ($definition instanceof ArrayDefinition) {
$definition->setReferenceContainer($this);
}
$this->creatingIds[$id] = 1;
try {
return $this->getDefinition($id)->resolve($this);
$container = ($this->container === null || $definition instanceof ReferenceInterface) ? $this : $this->container;
try {
return $definition->resolve($container);
} catch (NotFoundExceptionInterface $e) {
if ($container === $this) {
throw $e;
}

return $definition->resolve($this);
}
} finally {
unset($this->creatingIds[$id]);
if ($definition instanceof ArrayDefinition) {
$definition->setReferenceContainer(null);
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Support/ColorRed.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Factory\Tests\Support;

/**
* Class ColorRed
*/
final class ColorRed implements ColorInterface
{
private const COLOR_RED = 'red';

public function getColor(): string
{
return self::COLOR_RED;
}
}

42 changes: 42 additions & 0 deletions tests/Unit/FactoryTest.php
Expand Up @@ -30,6 +30,7 @@
use Yiisoft\Factory\Tests\Support\ExcessiveConstructorParameters;
use Yiisoft\Factory\Tests\Support\Firefighter;
use Yiisoft\Factory\Tests\Support\ColorPink;
use Yiisoft\Factory\Tests\Support\ColorRed;
use Yiisoft\Factory\Tests\Support\Cube;
use Yiisoft\Factory\Tests\Support\EngineInterface;
use Yiisoft\Factory\Tests\Support\EngineMarkOne;
Expand Down Expand Up @@ -337,6 +338,47 @@ public function testDoNotFallbackToContainerForReference(): void
$this->assertInstanceOf(EngineMarkTwo::class, $engine);
}

public function testDoNotFallbackToContainerForReferenceInConstructorOfArrayDefinition(): void
{
$factory = new Factory(
new SimpleContainer([
EngineInterface::class => new EngineMarkOne(),
]),
[
EngineInterface::class => new EngineMarkTwo(),
Car::class => [
'__construct()' => [
Reference::to(EngineInterface::class)
],
],
]
);

$car = $factory->create(Car::class);
$this->assertInstanceOf(EngineMarkTwo::class, $car->getEngine());
}

public function testDoNotFallbackToContainerForReferenceInMethodOfArrayDefinition(): void
{
$factory = new Factory(
new SimpleContainer([
EngineInterface::class => new EngineMarkOne(),
ColorInterface::class => new ColorRed(),
]),
[
ColorInterface::class => new ColorPink(),
Car::class => [
'setColor()' => [
Reference::to(ColorInterface::class)
],
],
]
);

$car = $factory->create(Car::class);
$this->assertInstanceOf(ColorPink::class, $car->getColor());
}

public function testExceptionAndDoNotFallbackToContainerForReference(): void
{
$factory = new Factory(
Expand Down

0 comments on commit 33d9d1e

Please sign in to comment.