Update Container DI to able unbind resolved entry (#4683)

* Added `Container::unset()` to unbind an arbitrary resolved entry.

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
Gilberto Marcomini Junior 2022-04-15 05:48:24 -03:00 committed by GitHub
parent f28f811b86
commit cd6b3e7a8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 1 deletions

View File

@ -14,7 +14,7 @@ jobs:
matrix:
os: [ ubuntu-latest ]
php-version: [ '7.4', '8.0' ]
sw-version: [ 'v4.5.11', 'v4.6.7', 'v4.7.1', 'v4.8.7', 'master' ]
sw-version: [ 'v4.5.11', 'v4.6.7', 'v4.7.1', 'v4.8.8', 'master' ]
exclude:
- php-version: '7.4'
sw-version: 'master'

View File

@ -8,6 +8,7 @@
## Added
- [#4576](https://github.com/hyperf/hyperf/pull/4576) Support `path_prefix` for `node` when using `rpc-client`.
- [#4683](https://github.com/hyperf/hyperf/pull/4683) Added `Container::unbind()` to unbind an arbitrary resolved entry.
# v2.2.30 - 2022-04-04

View File

@ -38,6 +38,11 @@ interface ContainerInterface extends PsrContainerInterface
*/
public function set(string $name, $entry);
/**
* Unbind an arbitrary resolved entry.
*/
public function unbind(string $name);
/**
* Bind an arbitrary definition to an identifier.
* Useful for testing 'make'.

View File

@ -92,6 +92,16 @@ class Container implements HyperfContainerInterface
$this->resolvedEntries[$name] = $entry;
}
/**
* Unbind an arbitrary resolved entry.
*/
public function unbind(string $name)
{
if ($this->has($name)) {
unset($this->resolvedEntries[$name]);
}
}
/**
* Bind an arbitrary definition to an identifier.
* Useful for testing 'make'.

View File

@ -66,4 +66,16 @@ class ContainerTest extends TestCase
{
$this->assertInstanceOf(Container::class, new ContainerProxy());
}
public function testUnset()
{
$container = new Container(new DefinitionSource([]));
$container->set('test', $id = uniqid());
$this->assertTrue($container->has('test'));
$this->assertSame($id, $container->get('test'));
$container->unbind('test');
$this->assertFalse($container->has('test'));
}
}