matomo
/
app
/
vendor
/
prefixed
/
symfony
/
console
/
DependencyInjection
/
AddConsoleCommandPass.php
AddConsoleCommandPass.php
119 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\Console\DependencyInjection; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\Console\Command\Command; |
| 14 | use Matomo\Dependencies\Symfony\Component\Console\Command\LazyCommand; |
| 15 | use Matomo\Dependencies\Symfony\Component\Console\CommandLoader\ContainerCommandLoader; |
| 16 | use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
| 17 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 18 | use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
| 19 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 21 | use Symfony\Component\DependencyInjection\Reference; |
| 22 | use Symfony\Component\DependencyInjection\TypedReference; |
| 23 | /** |
| 24 | * Registers console commands. |
| 25 | * |
| 26 | * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 27 | */ |
| 28 | class AddConsoleCommandPass implements CompilerPassInterface |
| 29 | { |
| 30 | private $commandLoaderServiceId; |
| 31 | private $commandTag; |
| 32 | private $noPreloadTag; |
| 33 | private $privateTagName; |
| 34 | public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload', string $privateTagName = 'container.private') |
| 35 | { |
| 36 | if (0 < \func_num_args()) { |
| 37 | trigger_deprecation('symfony/console', '5.3', 'Configuring "%s" is deprecated.', __CLASS__); |
| 38 | } |
| 39 | $this->commandLoaderServiceId = $commandLoaderServiceId; |
| 40 | $this->commandTag = $commandTag; |
| 41 | $this->noPreloadTag = $noPreloadTag; |
| 42 | $this->privateTagName = $privateTagName; |
| 43 | } |
| 44 | public function process(ContainerBuilder $container) |
| 45 | { |
| 46 | $commandServices = $container->findTaggedServiceIds($this->commandTag, \true); |
| 47 | $lazyCommandMap = []; |
| 48 | $lazyCommandRefs = []; |
| 49 | $serviceIds = []; |
| 50 | foreach ($commandServices as $id => $tags) { |
| 51 | $definition = $container->getDefinition($id); |
| 52 | $definition->addTag($this->noPreloadTag); |
| 53 | $class = $container->getParameterBag()->resolveValue($definition->getClass()); |
| 54 | if (isset($tags[0]['command'])) { |
| 55 | $aliases = $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 | $aliases = str_replace('%', '%%', $class::getDefaultName() ?? ''); |
| 64 | } |
| 65 | $aliases = explode('|', $aliases ?? ''); |
| 66 | $commandName = array_shift($aliases); |
| 67 | if ($isHidden = '' === $commandName) { |
| 68 | $commandName = array_shift($aliases); |
| 69 | } |
| 70 | if (null === $commandName) { |
| 71 | if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) { |
| 72 | $commandId = 'console.command.public_alias.' . $id; |
| 73 | $container->setAlias($commandId, $id)->setPublic(\true); |
| 74 | $id = $commandId; |
| 75 | } |
| 76 | $serviceIds[] = $id; |
| 77 | continue; |
| 78 | } |
| 79 | $description = $tags[0]['description'] ?? null; |
| 80 | unset($tags[0]); |
| 81 | $lazyCommandMap[$commandName] = $id; |
| 82 | $lazyCommandRefs[$id] = new TypedReference($id, $class); |
| 83 | foreach ($aliases as $alias) { |
| 84 | $lazyCommandMap[$alias] = $id; |
| 85 | } |
| 86 | foreach ($tags as $tag) { |
| 87 | if (isset($tag['command'])) { |
| 88 | $aliases[] = $tag['command']; |
| 89 | $lazyCommandMap[$tag['command']] = $id; |
| 90 | } |
| 91 | $description = $description ?? $tag['description'] ?? null; |
| 92 | } |
| 93 | $definition->addMethodCall('setName', [$commandName]); |
| 94 | if ($aliases) { |
| 95 | $definition->addMethodCall('setAliases', [$aliases]); |
| 96 | } |
| 97 | if ($isHidden) { |
| 98 | $definition->addMethodCall('setHidden', [\true]); |
| 99 | } |
| 100 | if (!$description) { |
| 101 | if (!($r = $container->getReflectionClass($class))) { |
| 102 | throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
| 103 | } |
| 104 | if (!$r->isSubclassOf(Command::class)) { |
| 105 | throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class)); |
| 106 | } |
| 107 | $description = str_replace('%', '%%', $class::getDefaultDescription() ?? ''); |
| 108 | } |
| 109 | if ($description) { |
| 110 | $definition->addMethodCall('setDescription', [$description]); |
| 111 | $container->register('.' . $id . '.lazy', LazyCommand::class)->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]); |
| 112 | $lazyCommandRefs[$id] = new Reference('.' . $id . '.lazy'); |
| 113 | } |
| 114 | } |
| 115 | $container->register($this->commandLoaderServiceId, ContainerCommandLoader::class)->setPublic(\true)->addTag($this->noPreloadTag)->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]); |
| 116 | $container->setParameter('console.command.ids', $serviceIds); |
| 117 | } |
| 118 | } |
| 119 |