| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\DependencyInjection; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Command\Command; |
| 15 |
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader; |
| 16 |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 17 |
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
| 18 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 |
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 20 |
use Symfony\Component\DependencyInjection\TypedReference; |
| 21 |
|
| 22 |
/** |
| 23 |
* Registers console commands. |
| 24 |
* |
| 25 |
* @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 26 |
*/ |
| 27 |
class AddConsoleCommandPass implements CompilerPassInterface |
| 28 |
{ |
| 29 |
private $commandLoaderServiceId; |
| 30 |
private $commandTag; |
| 31 |
private $noPreloadTag; |
| 32 |
private $privateTagName; |
| 33 |
|
| 34 |
public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload', string $privateTagName = 'container.private') |
| 35 |
{ |
| 36 |
$this->commandLoaderServiceId = $commandLoaderServiceId; |
| 37 |
$this->commandTag = $commandTag; |
| 38 |
$this->noPreloadTag = $noPreloadTag; |
| 39 |
$this->privateTagName = $privateTagName; |
| 40 |
} |
| 41 |
|
| 42 |
public function process(ContainerBuilder $container) |
| 43 |
{ |
| 44 |
$commandServices = $container->findTaggedServiceIds($this->commandTag, true); |
| 45 |
$lazyCommandMap = []; |
| 46 |
$lazyCommandRefs = []; |
| 47 |
$serviceIds = []; |
| 48 |
|
| 49 |
foreach ($commandServices as $id => $tags) { |
| 50 |
$definition = $container->getDefinition($id); |
| 51 |
$definition->addTag($this->noPreloadTag); |
| 52 |
$class = $container->getParameterBag()->resolveValue($definition->getClass()); |
| 53 |
|
| 54 |
if (isset($tags[0]['command'])) { |
| 55 |
$commandName = $tags[0]['command']; |
| 56 |
} else { |
| 57 |
if (!$r = $container->getReflectionClass($class)) { |
| 58 |
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
| 59 |
} |
| 60 |
if (!$r->isSubclassOf(Command::class)) { |
| 61 |
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class)); |
| 62 |
} |
| 63 |
$commandName = $class::getDefaultName(); |
| 64 |
} |
| 65 |
|
| 66 |
if (null === $commandName) { |
| 67 |
if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) { |
| 68 |
$commandId = 'console.command.public_alias.'.$id; |
| 69 |
$container->setAlias($commandId, $id)->setPublic(true); |
| 70 |
$id = $commandId; |
| 71 |
} |
| 72 |
$serviceIds[] = $id; |
| 73 |
|
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
unset($tags[0]); |
| 78 |
$lazyCommandMap[$commandName] = $id; |
| 79 |
$lazyCommandRefs[$id] = new TypedReference($id, $class); |
| 80 |
$aliases = []; |
| 81 |
|
| 82 |
foreach ($tags as $tag) { |
| 83 |
if (isset($tag['command'])) { |
| 84 |
$aliases[] = $tag['command']; |
| 85 |
$lazyCommandMap[$tag['command']] = $id; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$definition->addMethodCall('setName', [$commandName]); |
| 90 |
|
| 91 |
if ($aliases) { |
| 92 |
$definition->addMethodCall('setAliases', [$aliases]); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
$container |
| 97 |
->register($this->commandLoaderServiceId, ContainerCommandLoader::class) |
| 98 |
->setPublic(true) |
| 99 |
->addTag($this->noPreloadTag) |
| 100 |
->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]); |
| 101 |
|
| 102 |
$container->setParameter('console.command.ids', $serviceIds); |
| 103 |
} |
| 104 |
} |
| 105 |
|