Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 50fe6a3

Browse filesBrowse files
committed
feature #23915 [DI] Allow get available services from service locator (Koc)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Allow get available services from service locator | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the 3.4, legacy code removals go to the master branch. - Please fill in this template according to the PR you're about to submit. - Replace this comment by a description of what your PR is solving. --> Sometimes we are using service locators and throw context specific exceptions if service not found inside it. Would be nice inform user about available services in our custom exception, like: ```php try { return $this->transports->get($transport); } catch (NotFoundExceptionInterface $e) { $availableTransports = method_exists($e, 'getAlternatives') ? $e->getAlternatives() : []; throw TransportNotFoundException::create($transport, $availableTransports, $e); } ``` Commits ------- 4993b1c Allow to get alternatives when ServiceNotFoundException occurs.
2 parents 19e6f0c + 4993b1c commit 50fe6a3
Copy full SHA for 50fe6a3

File tree

2 files changed

+16
-2
lines changed
Filter options

2 files changed

+16
-2
lines changed

‎src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ServiceNotFoundException extends InvalidArgumentException implements NotFo
2222
{
2323
private $id;
2424
private $sourceId;
25+
private $alternatives;
2526

2627
public function __construct($id, $sourceId = null, \Exception $previous = null, array $alternatives = array())
2728
{
@@ -44,6 +45,7 @@ public function __construct($id, $sourceId = null, \Exception $previous = null,
4445

4546
$this->id = $id;
4647
$this->sourceId = $sourceId;
48+
$this->alternatives = $alternatives;
4749
}
4850

4951
public function getId()
@@ -55,4 +57,9 @@ public function getSourceId()
5557
{
5658
return $this->sourceId;
5759
}
60+
61+
public function getAlternatives()
62+
{
63+
return $this->alternatives;
64+
}
5865
}

‎src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1516
use Symfony\Component\DependencyInjection\ServiceLocator;
1617

1718
class ServiceLocatorTest extends TestCase
@@ -58,7 +59,7 @@ public function testGetDoesNotMemoize()
5859

5960
/**
6061
* @expectedException \Psr\Container\NotFoundExceptionInterface
61-
* @expectedExceptionMessage You have requested a non-existent service "dummy"
62+
* @expectedExceptionMessage You have requested a non-existent service "dummy". Did you mean one of these: "foo", "bar"?
6263
*/
6364
public function testGetThrowsOnUndefinedService()
6465
{
@@ -67,7 +68,13 @@ public function testGetThrowsOnUndefinedService()
6768
'bar' => function () { return 'baz'; },
6869
));
6970

70-
$locator->get('dummy');
71+
try {
72+
$locator->get('dummy');
73+
} catch (ServiceNotFoundException $e) {
74+
$this->assertSame(array('foo', 'bar'), $e->getAlternatives());
75+
76+
throw $e;
77+
}
7178
}
7279

7380
public function testInvoke()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.