Skip to content

Commit

Permalink
FIX: Extend must be a basic value and removing necessity to make it o…
Browse files Browse the repository at this point in the history
…nly on closure
  • Loading branch information
gravataLonga committed Feb 25, 2022
1 parent 6e5a93c commit 7fbbe3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
11 changes: 8 additions & 3 deletions src/Container.php
Expand Up @@ -98,18 +98,19 @@ public function alias($entry, $alias)

/**
* @param string $id
* @param callable|mixed $factory
*
* @throws NotFoundContainerException
*
* @return void
*/
public function extend($id, callable $factory)
public function extend($id, $factory)
{
if (!$this->has($id)) {
throw NotFoundContainerException::entryNotFound($id);
}

$factory = $factory instanceof Closure ? $factory : Closure::fromCallable($factory);
$factory = $this->prepareEntry($factory);

if (true === array_key_exists($id, $this->resolved)) {
unset($this->resolved[$id]);
Expand Down Expand Up @@ -362,7 +363,11 @@ private function resolve(string $id, array $arguments = [])
$get = $this->resolveEntry($id, $arguments);

foreach ($this->getExtenders($id) as $extend) {
$get = $extend($this, $get);
if (is_callable($extend)) {
$get = $extend($this, $get);
continue;
}
$get = $extend;
}

return $get;
Expand Down
22 changes: 11 additions & 11 deletions tests/ExtendedMethodTest.php
Expand Up @@ -133,16 +133,6 @@ public function testThrowExceptionIfAttemptExtendedNotFoundEntry()
});
}

public function testThrowExceptionIfExtendedIsNotCallable()
{
$this->expectException(TypeError::class);
$container = new Container();
$container->factory('hello', static function () {
return 'Hello';
});
$container->extend('hello', 'Olá');
}

public function testWhenUnsetItRemoveExtendedEntriesAlso()
{
$container = new Container();
Expand All @@ -161,6 +151,16 @@ public function testWhenUnsetItRemoveExtendedEntriesAlso()
self::assertArrayNotHasKey('hello', $ex->getValue($container));
}

public function testCanExtendOnBasicValues ()
{
$container = new Container();
$container->factory('hello', 123);
$container->extend('hello', 456);

$this->assertNotEmpty($container->get('hello'));
$this->assertEquals(456, $container->get('hello'));
}

private function newClass($arg = null)
{
return new class($arg) {
Expand All @@ -185,4 +185,4 @@ public function setName(ContainerInterface $c, $hello)
}
};
}
}
}

0 comments on commit 7fbbe3f

Please sign in to comment.