Skip to content

Commit

Permalink
Let callable type be used. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed May 10, 2020
1 parent 34353a1 commit 479e2bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
29 changes: 11 additions & 18 deletions src/Container.php
Expand Up @@ -15,7 +15,7 @@
use Reflector;

use function array_key_exists;
use function is_string;
use function is_callable;

/**
* Class Container.
Expand Down Expand Up @@ -60,7 +60,7 @@ public function __construct(array $config = [])
$this->share = [];

$self = $this;
$this->share(ContainerInterface::class, function () use ($self) {
$this->share(ContainerInterface::class, static function () use ($self) {
return $self;
});
$this->alias(ContainerInterface::class, Container::class);
Expand All @@ -87,13 +87,17 @@ public function alias($entry, $alias)
* Factory binding.
*
* @param string $id
* @param Closure $factory
* @param callable|mixed $factory
*
* @return void
*/
public function factory($id, Closure $factory)
public function factory(string $id, $factory)
{
$this->bindings[$id] = $factory;
$this->bindings[$id] = is_callable($factory) ?
($factory instanceof Closure ?
$factory :
Closure::fromCallable($factory)) :
$factory;
}

/**
Expand Down Expand Up @@ -194,22 +198,11 @@ public function offsetUnset($offset)
* @param string $id
* @param mixed $factory
*
* @throws ContainerException
*
* @return void
*/
public function set($id, $factory)
public function set(string $id, $factory)
{
if (!is_string($id)) {
throw ContainerException::entryType();
}

if ($factory instanceof Closure) {
$this->factory($id, $factory);

return;
}
$this->bindings[$id] = $factory;
$this->factory($id, $factory);
}

/**
Expand Down
31 changes: 21 additions & 10 deletions tests/ContainerTest.php
Expand Up @@ -5,7 +5,6 @@
namespace Tests;

use Gravatalonga\Container\Container;
use Gravatalonga\Container\ContainerException;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
Expand All @@ -21,11 +20,32 @@ public function testCanBindDependency()
{
$rand = mt_rand(0, 10);
$container = new Container();

$container->factory('random', static function () use ($rand) {
return $rand;
});

$class = new class($rand) {
/**
* @var int
*/
private $rand;

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

public function get(): int
{
return $this->rand;
}
};

$container->factory('random1', [$class, 'get']);

self::assertEquals($rand, $container->get('random'));
self::assertEquals($rand, $container->get('random1'));
}

public function testCanCheckIfEntryExistOnContainer()
Expand Down Expand Up @@ -89,15 +109,6 @@ public function testCanGetValueFromContainer()
self::assertTrue($container->get('config'));
}

public function testCanOnlyAcceptStringForEntry()
{
$this->expectException(ContainerException::class);
$this->expectExceptionMessage('Entry type must be string');
$container = new Container();
$container->set(static function () {
}, '123');
}

public function testCanSetDirectValueRatherThanCallback()
{
$container = new Container();
Expand Down

0 comments on commit 479e2bc

Please sign in to comment.