-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Create an util class to determine services implementing a FQCN #20940
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Symfony/Component/DependencyInjection/Tests/Fixtures/BadParent.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
class BadParent extends ThisDoesNotExist | ||
{ | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/DependencyInjection/Tests/Util/ServiceTypeHelperTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Util; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\BadParent; | ||
use Symfony\Component\DependencyInjection\Util\ServiceTypeHelper; | ||
|
||
class ServiceTypeHelperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testIgnoreServiceWithClassNotExisting() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('class_not_exist', 'NotExistingClass'); | ||
|
||
$helper = new ServiceTypeHelper($container); | ||
$this->assertEmpty($helper->getOfType('NotExistingClass')); | ||
} | ||
|
||
public function testIgnoreServiceWithParentNotExisting() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('bad_parent', BadParent::class); | ||
|
||
$helper = new ServiceTypeHelper($container); | ||
$this->assertEmpty($helper->getOfType(BadParent::class)); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
src/Symfony/Component/DependencyInjection/Util/ServiceTypeHelper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Util; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* Help finding services corresponding to a type. | ||
* Be aware that the map is constructed once, at the first call to {@link getOfType()}. | ||
* | ||
* @author Guilhem N. <egetick@gmail.com> | ||
*/ | ||
final class ServiceTypeHelper | ||
{ | ||
private static $classNames = array(); | ||
private $container; | ||
private $typeMap; | ||
|
||
public function __construct(ContainerBuilder $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Resolves services implementing a type. | ||
* | ||
* @param string $type a class or an interface | ||
* | ||
* @return string[] the services implementing the type | ||
*/ | ||
public function getOfType($type) | ||
{ | ||
if (null === $this->typeMap) { | ||
$this->populateAvailableTypes(); | ||
} | ||
|
||
if (!isset($this->typeMap[$type])) { | ||
return array(); | ||
} | ||
|
||
return $this->typeMap[$type]; | ||
} | ||
|
||
/** | ||
* Resets the type map. | ||
*/ | ||
public function reset() | ||
{ | ||
$this->typeMap = null; | ||
} | ||
|
||
/** | ||
* Populates the list of available types. | ||
*/ | ||
private function populateAvailableTypes() | ||
{ | ||
$throwingAutoloader = function ($class) { | ||
throw new \ReflectionException(sprintf('Class %s does not exist', $class)); | ||
}; | ||
spl_autoload_register($throwingAutoloader); | ||
|
||
try { | ||
$this->typeMap = array(); | ||
foreach ($this->container->getDefinitions() as $id => $definition) { | ||
$this->populateAvailableType($id, $definition); | ||
} | ||
} finally { | ||
spl_autoload_unregister($throwingAutoloader); | ||
} | ||
} | ||
|
||
/** | ||
* Populates the list of available types for a given definition. | ||
* | ||
* @param string $id | ||
* @param Definition $definition | ||
*/ | ||
private function populateAvailableType($id, Definition $definition) | ||
{ | ||
// Never use abstract services | ||
if ($definition->isAbstract()) { | ||
return; | ||
} | ||
|
||
if (null === ($class = $this->getClass($definition))) { | ||
return; | ||
} | ||
|
||
$types = array(); | ||
if ($interfaces = class_implements($class)) { | ||
$types = $interfaces; | ||
} | ||
|
||
do { | ||
$types[] = $class; | ||
} while ($class = get_parent_class($class)); | ||
|
||
foreach ($types as $type) { | ||
if (!isset($this->typeMap[$type])) { | ||
$this->typeMap[$type] = array(); | ||
} | ||
|
||
$this->typeMap[$type][] = $id; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the class associated with the given service. | ||
* | ||
* @param Definition $definition | ||
* | ||
* @return string|null | ||
*/ | ||
private function getClass(Definition $definition) | ||
{ | ||
// Cannot use reflection if the class isn't set | ||
if (!$class = $definition->getClass()) { | ||
return; | ||
} | ||
|
||
// Normalize the class name (`\Foo` -> `Foo`) | ||
$class = $this->container->getParameterBag()->resolveValue($class); | ||
if (array_key_exists($class, self::$classNames)) { | ||
return self::$classNames[$class]; | ||
} | ||
|
||
try { | ||
$name = (new \ReflectionClass($class))->name; | ||
} catch (\ReflectionException $e) { | ||
$name = null; | ||
} | ||
|
||
return self::$classNames[$class] = $name; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this works fine only if you have a fallback autoloader throwing a ReflectionException as a last resort to avoid the fatal error. As you extracted the logic, you need to handle this requirement in your new service
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed in the
AutowirePass
because it fetches parameters type hint but this helper doesn't use anything else than the class name, is it still needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the class may extend a non-existent parent class (if the service relies on an optional dependency which is not installed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it's updated in 98481b9.