Skip to content

[5.4] Container: Add a new method to remove extenders of abstract #19269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,20 @@ protected function getExtenders($abstract)
return [];
}

/**
* Remove all the extend closure of abstract.
*
* @param string $abstract
*/
public function forgetExtenders($abstract)
{
$abstract = $this->getAlias($abstract);

if (isset($this->extenders[$abstract])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is unnecessary, unset will do the check automatically

unset($this->extenders[$abstract]);
}
}

/**
* Drop all of the stale instances and aliases.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,32 @@ public function testExtendCanBeCalledBeforeBind()
$this->assertEquals('foobar', $container->make('foo'));
}

public function testUnsetExtend()
{
$container = new Container;
$container->bind('foo', function () {
$obj = new StdClass;
$obj->foo = 'bar';

return $obj;
});

$container->extend('foo', function ($obj, $container) {
$obj->bar = 'baz';

return $obj;
});

unset($container['foo']);
$container->forgetExtenders('foo');

$container->bind('foo', function () {
return 'foo';
});

$this->assertEquals('foo', $container->make('foo'));
}

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