8000 [DependencyInjection] Make it easy to get a service from its FQCN by GuilhemN · Pull Request #18300 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Make it easy to get a service from its FQCN #18300

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/Symfony/Component/DependencyInjection/Util/ServiceTypeHelper.php
8000 < 8FFE td class="blob-num blob-num-addition empty-cell">
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;

/**
* 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>
*/
class ServiceTypeHelper
{
private static $resolvedTypes = array();
private $container;
private $typeMap;

public function __construct(ContainerBuilder $container)
{
$this->container = $container;
}

/**
* Resolves services implementing a type.
*
* @param string $class
*/
public function getOfType($class)
{
if (null === $this->typeMap) {
$this->populateAvailableTypes();
}

if (!isset($this->typeMap[$class])) {
return array();
}

return $this->typeMap[$class];
}

/**
* Resets the type map.
*/
public function reset()
{
$this->typeMap = null;
}

/**
* Populates the list of available types.
*/
private function populateAvailableTypes()
{
$this->typeMap = array();
foreach ($this->container->getDefinitions() as $id => $definition) {
$this->populateAvailableType($id, $definition);
}
}

/**
* 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;
}

$class = $this->container->getParameterBag()->resolveValue($definition->getClass());
if (!$class) {
return;
}

if (isset(self::$resolvedTypes[$class])) {
$types = self::$resolvedTypes[$class];
} else {
$types = array();
if ($interfaces = class_implements($class)) {
$types = $interfaces;
}

do {
$types[] = $class;
} while ($class = get_parent_class($class));

self::$resolvedTypes[$class] = $types;
}

foreach ($types as $type) {
if (!isset($this->typeMap[$type])) {
$this->typeMap[$type] = array();
}

$this->typeMap[$type][] = $id;
}
}
}
0