| Current Path : /home/rtorresani/www/vendor/rector/rector/src/DependencyInjection/ |
| Current File : //home/rtorresani/www/vendor/rector/rector/src/DependencyInjection/DefinitionFinder.php |
<?php
declare (strict_types=1);
namespace Rector\Core\DependencyInjection;
use Rector\Core\DependencyInjection\Exception\DefinitionForTypeNotFoundException;
use RectorPrefix202304\Symfony\Component\DependencyInjection\ContainerBuilder;
use RectorPrefix202304\Symfony\Component\DependencyInjection\Definition;
use Throwable;
/**
* @api
* @see \Rector\Core\Tests\DependencyInjection\DefinitionFinderTest
*/
final class DefinitionFinder
{
/**
* @return Definition[]
*/
public function findAllByType(ContainerBuilder $containerBuilder, string $type) : array
{
$definitions = [];
$containerBuilderDefinitions = $containerBuilder->getDefinitions();
foreach ($containerBuilderDefinitions as $name => $definition) {
$class = $definition->getClass() ?: $name;
if (!$this->doesClassExists($class)) {
continue;
}
if (\is_a($class, $type, \true)) {
$definitions[$name] = $definition;
}
}
return $definitions;
}
public function getByType(ContainerBuilder $containerBuilder, string $type) : Definition
{
$definition = $this->getByTypeIfExists($containerBuilder, $type);
if ($definition instanceof Definition) {
return $definition;
}
throw new DefinitionForTypeNotFoundException(\sprintf('Definition for type "%s" was not found.', $type));
}
private function getByTypeIfExists(ContainerBuilder $containerBuilder, string $type) : ?Definition
{
$containerBuilderDefinitions = $containerBuilder->getDefinitions();
foreach ($containerBuilderDefinitions as $name => $definition) {
$class = $definition->getClass() ?: $name;
if (!$this->doesClassExists($class)) {
continue;
}
if (\is_a($class, $type, \true)) {
return $definition;
}
}
return null;
}
private function doesClassExists(string $class) : bool
{
try {
return \class_exists($class);
} catch (Throwable $exception) {
return \false;
}
}
}