Skip to content

Commit

Permalink
feat: clonable container with new resolver instance
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
  • Loading branch information
feryardiant committed Jun 22, 2020
1 parent 3542bc7 commit 0db945e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Projek;

use Projek\Container\ContainerInterface;
use Projek\Container\NotFoundException;
use Projek\Container\Resolver;
use Projek\Container\{ContainerInterface, NotFoundException, Resolver};
use Psr\Container\ContainerInterface as PsrContainerInterface;

class Container implements ContainerInterface
Expand Down Expand Up @@ -50,6 +48,14 @@ public function __construct(array $instances = [])
}
}

/**
* Create new resolver instance when get cloned.
*/
public function __clone()
{
$this->resolver = new Resolver($this);
}

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 11 additions & 0 deletions test/spec/Container.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,15 @@ public function get($item) {
expect($somewhere->get($key))->toBe($here->get($key));
}
});

it('Should be cloned with new resolver instance', function () {
// Dependencies.
$this->c->set('dummy', Dummy::class);

$c = clone $this->c;
$c->set(AbstractFoo::class, ConcreteBar::class);

expect($this->c->has(AbstractFoo::class))->toBeFalsy();
expect($c->has('dummy'))->toBeTruthy();
});
});

0 comments on commit 0db945e

Please sign in to comment.