This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php

99 lines
3.6 KiB
PHP
Raw Normal View History

<?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\Console\DependencyInjection;
use Symfony\Component\Console\Command\Command;
2017-07-12 10:59:19 +01:00
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2017-07-12 10:59:19 +01:00
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2017-07-12 10:59:19 +01:00
use Symfony\Component\DependencyInjection\TypedReference;
/**
* Registers console commands.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class AddConsoleCommandPass implements CompilerPassInterface
{
2017-07-12 10:59:19 +01:00
private $commandLoaderServiceId;
private $commandTag;
2017-10-28 19:15:32 +01:00
public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command')
2017-07-12 10:59:19 +01:00
{
$this->commandLoaderServiceId = $commandLoaderServiceId;
$this->commandTag = $commandTag;
}
public function process(ContainerBuilder $container)
{
2017-07-12 10:59:19 +01:00
$commandServices = $container->findTaggedServiceIds($this->commandTag, true);
$lazyCommandMap = array();
$lazyCommandRefs = array();
$serviceIds = array();
foreach ($commandServices as $id => $tags) {
$definition = $container->getDefinition($id);
$class = $container->getParameterBag()->resolveValue($definition->getClass());
if (isset($tags[0]['command'])) {
$commandName = $tags[0]['command'];
} else {
if (!$r = $container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
}
if (!$r->isSubclassOf(Command::class)) {
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
}
$commandName = $class::getDefaultName();
}
if (null === $commandName) {
if (!$definition->isPublic() || $definition->isPrivate()) {
2017-12-24 05:41:21 +00:00
$commandId = 'console.command.public_alias.'.$id;
$container->setAlias($commandId, $id)->setPublic(true);
2017-07-12 10:59:19 +01:00
$id = $commandId;
}
2017-12-24 05:41:21 +00:00
$serviceIds[] = $id;
2017-07-12 10:59:19 +01:00
continue;
}
2017-07-12 10:59:19 +01:00
unset($tags[0]);
2017-07-12 10:59:19 +01:00
$lazyCommandMap[$commandName] = $id;
$lazyCommandRefs[$id] = new TypedReference($id, $class);
$aliases = array();
foreach ($tags as $tag) {
if (isset($tag['command'])) {
$aliases[] = $tag['command'];
$lazyCommandMap[$tag['command']] = $id;
2017-07-12 10:59:19 +01:00
}
}
2017-07-15 13:12:51 +01:00
$definition->addMethodCall('setName', array($commandName));
2017-07-12 10:59:19 +01:00
if ($aliases) {
$definition->addMethodCall('setAliases', array($aliases));
}
}
2017-07-12 10:59:19 +01:00
$container
->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
->setPublic(true)
2017-07-12 10:59:19 +01:00
->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));
$container->setParameter('console.command.ids', $serviceIds);
}
}