Skip to content

Commit

Permalink
Merge pull request #8 from SergiX44/fix_delegate
Browse files Browse the repository at this point in the history
Fix delegate
  • Loading branch information
sergix44 authored Jul 2, 2023
2 parents dcae4bb + 1853225 commit 9925428
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 43 deletions.
16 changes: 8 additions & 8 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public function __construct()
*/
public function get(string $id)
{
if ($this->delegate !== null && $this->delegate->has($id)) {
return $this->delegate->get($id);
}

if (array_key_exists($id, $this->definitions)) {
return $this->definitions[$id]?->make($this);
}

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

try {
return $this->resolve($id);
} catch (Throwable $e) {
Expand All @@ -59,13 +59,13 @@ public function get(string $id)
*/
public function has(string $id): bool
{
// check if the delegate can resolve it, if defined
if ($this->delegate !== null && $this->delegate->has($id)) {
// check if is something we match right away
if (array_key_exists($id, $this->definitions)) {
return true;
}

// check if is something we match right away
if (array_key_exists($id, $this->definitions)) {
// check if the delegate can resolve it, if defined
if ($this->delegate !== null && $this->delegate->has($id)) {
return true;
}

Expand Down
60 changes: 60 additions & 0 deletions tests/Feature/ContainerDelegateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use SergiX44\Container\Container;
use SergiX44\Container\Tests\Fixtures\Resolve\AbstractClass;
use SergiX44\Container\Tests\Fixtures\Resolve\AnotherConcreteClass;
use SergiX44\Container\Tests\Fixtures\Resolve\ConcreteClass;
use SergiX44\Container\Tests\Fixtures\Resolve\SimpleClass;
use SergiX44\Container\Tests\Fixtures\Resolve\SimpleInterface;

it('can resolve a simple definition via delegator', function () {
$di = new \DI\ContainerBuilder();
$di->addDefinitions([
SimpleInterface::class => \DI\create(SimpleClass::class),
]);
$delegator = $di->build();

expect($delegator->has(SimpleInterface::class))->toBeTrue();

$container = new Container();

expect($container->has(SimpleInterface::class))->toBeFalse();

$container->delegate($delegator);

expect($container->has(SimpleInterface::class))->toBeTrue()
->and($container->get(SimpleInterface::class))->toBeInstanceOf(SimpleClass::class);
});

it('can resolve a simple definition via himself as delegator', function () {
$delegator = new Container();
$delegator->bind(SimpleInterface::class, SimpleClass::class);

expect($delegator->has(SimpleInterface::class))->toBeTrue();

$container = new Container();

expect($container->has(SimpleInterface::class))->toBeFalse();

$container->delegate($delegator);

expect($container->has(SimpleInterface::class))->toBeTrue()
->and($container->get(SimpleInterface::class))->toBeInstanceOf(SimpleClass::class);
});

it('returns his definition instead of the delegator', function () {
$delegator = new Container();
$delegator->bind(AbstractClass::class, ConcreteClass::class);

expect($delegator->has(AbstractClass::class))->toBeTrue();

$container = new Container();
$container->bind(AbstractClass::class, AnotherConcreteClass::class);

expect($container->has(AbstractClass::class))->toBeTrue();

$container->delegate($delegator);

expect($container->has(AbstractClass::class))->toBeTrue()
->and($container->get(AbstractClass::class))->toBeInstanceOf(AnotherConcreteClass::class);
});
35 changes: 0 additions & 35 deletions tests/Feature/ContainerResolveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,41 +148,6 @@
expect(spl_object_id($get))->toBe(spl_object_id($i));
});

it('can resolve a simple definition via delegator', function () {
$di = new \DI\ContainerBuilder();
$di->addDefinitions([
SimpleInterface::class => \DI\create(SimpleClass::class),
]);
$delegator = $di->build();

expect($delegator->has(SimpleInterface::class))->toBeTrue();

$container = new Container();

expect($container->has(SimpleInterface::class))->toBeFalse();

$container->delegate($delegator);

expect($container->has(SimpleInterface::class))->toBeTrue()
->and($container->get(SimpleInterface::class))->toBeInstanceOf(SimpleClass::class);
});

it('can resolve a simple definition via himself as delegator', function () {
$delegator = new Container();
$delegator->bind(SimpleInterface::class, SimpleClass::class);

expect($delegator->has(SimpleInterface::class))->toBeTrue();

$container = new Container();

expect($container->has(SimpleInterface::class))->toBeFalse();

$container->delegate($delegator);

expect($container->has(SimpleInterface::class))->toBeTrue()
->and($container->get(SimpleInterface::class))->toBeInstanceOf(SimpleClass::class);
});

it('support abstract as string', function () {
$container = new Container();

Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/Resolve/AnotherConcreteClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SergiX44\Container\Tests\Fixtures\Resolve;

class AnotherConcreteClass extends AbstractClass
{
}

0 comments on commit 9925428

Please sign in to comment.