Skip to content

Commit

Permalink
do not fail on "has" when autowiring is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
esler committed Jun 6, 2019
1 parent 462c702 commit ba7371b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function get($id) {
* @return bool
*/
public function has($id): bool {
// do not attempt create a service when "autowire" is disabled
if (!$this->autowireEnabled) {
return isset($this->instances[$id]);
}

try {
$this->get($id); // attempt create a service
} catch (ServiceNotFoundException $e) {
Expand Down
6 changes: 6 additions & 0 deletions tests/ServiceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ function testProperFailWhenFactoryIsDefinedBadly() {
$this->expectExceptionMessage('Empty result from factory, id: Poo');
$container->make('Poo');
}

function testHasMethodReturnsFalseNotAnExceptionIfAutowiringIsDisabled() {
$container = new ServiceContainer(['autowire' => false]);

$this->assertFalse($container->has(Foo::class));
}
}

interface CacheAdapterInterface {}
Expand Down

0 comments on commit ba7371b

Please sign in to comment.