Skip to content

Commit a4f3ea9

Browse files
author
Tamas Szijarto
committed
[2.1][DependencyInjection] Incomplete error handling in the container
1 parent f152170 commit a4f3ea9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
258258
$service = $this->$method();
259259
} catch (\Exception $e) {
260260
unset($this->loading[$id]);
261+
262+
if (isset($this->services[$id])) {
263+
unset($this->services[$id]);
264+
}
265+
261266
throw $e;
262267
}
263268

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testGetServiceIds()
9898
$this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
9999

100100
$sc = new ProjectServiceContainer();
101-
$this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
101+
$this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
102102
}
103103

104104
/**
@@ -367,6 +367,25 @@ public function testGetThrowsException()
367367
}
368368
}
369369

370+
public function testGetThrowsExceptionOnServiceConfiguration()
371+
{
372+
$c = new ProjectServiceContainer();
373+
374+
try {
375+
$c->get('throws_exception_on_service_configuration');
376+
$this->fail('The container can not contain invalid service!');
377+
} catch (\Exception $e) {
378+
$this->assertEquals('Something was terribly wrong while trying to configure the service!', $e->getMessage());
379+
}
380+
381+
try {
382+
$c->get('throws_exception_on_service_configuration');
383+
$this->fail('The container can not contain invalid service!');
384+
} catch (\Exception $e) {
385+
$this->assertEquals('Something was terribly wrong while trying to configure the service!', $e->getMessage());
386+
}
387+
}
388+
370389
public function getInvalidParentScopes()
371390
{
372391
return array(
@@ -447,4 +466,13 @@ protected function getThrowExceptionService()
447466
{
448467
throw new \Exception('Something went terribly wrong!');
449468
}
469+
470+
protected function getThrowsExceptionOnServiceConfigurationService()
471+
{
472+
$this->services['throws_exception_on_service_configuration'] = $instance = new \stdClass();
473+
474+
throw new \Exception('Something was terribly wrong while trying to configure the service!');
475+
476+
return $instance;
477+
}
450478
}

0 commit comments

Comments
 (0)