Skip to content

Commit

Permalink
feat: add make() method
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 19, 2020
1 parent 2e3b986 commit c0bc43a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@ public function unset(string $id): void
{
unset($this->instances[$id]);
}

/**
* {@inheritDoc}
*/
public function make(string $concrete)
{
$instance = $this->resolver->resolve($concrete);

if (is_callable($instance)) {
return $this->resolver->handle($instance);
}

return $instance;
}
}
8 changes: 8 additions & 0 deletions src/Container/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public function set(string $id, $instance): void;
* @return void
*/
public function unset(string $id): void;

/**
* Resolve an instance without adding it to the stack.
*
* @param string $concrete
* @return mixed
*/
public function make(string $concrete);
}
23 changes: 22 additions & 1 deletion test/spec/Container.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Projek\Container;
use Projek\Container\{ContainerInterface, Exception, NotFoundException };
use Psr\Container\ContainerInterface as PsrContainer;
use Stubs\ { Dummy, AbstractFoo, ConcreteBar, ServiceProvider};
use Stubs\{Dummy, AbstractFoo, ConcreteBar, ServiceProvider};
use function Kahlan\describe;
use function Kahlan\expect;

Expand Down Expand Up @@ -102,6 +102,10 @@
});

it('Should throw exception when setting incorrect param', function () {
expect(function () {
$this->c->make(AbstractFoo::class);
})->toThrow(Exception::notInstantiable(AbstractFoo::class));

expect(function () {
$this->c->set('foo', AbstractFoo::class);
})->toThrow(Exception::notInstantiable(AbstractFoo::class));
Expand All @@ -118,4 +122,21 @@
$this->c->set('foo', null);
})->toThrow(Exception::unresolvable('NULL'));
});

it('Should make an instance without adding to the stack', function () {
// Dependencies.
$this->c->set('dummy', Dummy::class);
$this->c->set(AbstractFoo::class, ConcreteBar::class);

$instances = [
Stubs\CallableClass::class => AbstractFoo::class,
Stubs\InstantiableClass::class => Stubs\InstantiableClass::class,
];

foreach ($instances as $concrete => $instance) {
expect($this->c->has($concrete))->toBeFalsy();
expect($this->c->make($concrete))->toBeAnInstanceOf($instance);
expect($this->c->has($concrete))->toBeFalsy();
}
});
});
21 changes: 21 additions & 0 deletions test/stub/CallableClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Stubs;

class CallableClass
{
/**
* @var stdClass
*/
public $dummy;

public function __construct($dummy)
{
$this->dummy = $dummy;
}

public function __invoke(AbstractFoo $foo)
{
return $foo;
}
}
16 changes: 16 additions & 0 deletions test/stub/InstantiableClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Stubs;

class InstantiableClass
{
/**
* @var stdClass
*/
public $dummy;

public function __construct($dummy)
{
$this->dummy = $dummy;
}
}

0 comments on commit c0bc43a

Please sign in to comment.