1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18namespace TYPO3\CMS\Core\DependencyInjection;
19
20use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
21use Symfony\Component\DependencyInjection\ContainerBuilder;
22use TYPO3\CMS\Core\Console\CommandRegistry;
23
24/**
25 * @internal
26 */
27final class ConsoleCommandPass implements CompilerPassInterface
28{
29    /**
30     * @var string
31     */
32    private $tagName;
33
34    /**
35     * @param string $tagName
36     */
37    public function __construct(string $tagName)
38    {
39        $this->tagName = $tagName;
40    }
41
42    /**
43     * @param ContainerBuilder $container
44     */
45    public function process(ContainerBuilder $container)
46    {
47        if (!$container->hasDefinition(CommandRegistry::class)) {
48            return;
49        }
50        $commandRegistryDefinition = $container->findDefinition(CommandRegistry::class);
51
52        foreach ($container->findTaggedServiceIds($this->tagName) as $serviceName => $tags) {
53            $commandServiceDefinition = $container->findDefinition($serviceName)->setPublic(true);
54            $commandName = null;
55            $description = null;
56            $hidden = false;
57            $aliases = [];
58            foreach ($tags as $attributes) {
59                $command = $attributes['command'] ?? null;
60                $description = $attributes['description'] ?? $description;
61                $hidden = (bool)($attributes['hidden'] ?? $hidden);
62                $schedulable = (bool)($attributes['schedulable'] ?? true);
63                $aliasFor = null;
64                if ($command === null) {
65                    continue;
66                }
67
68                $isAlias = $commandName !== null || ($attributes['alias'] ?? false);
69                if (!$isAlias) {
70                    $commandName = $attributes['command'];
71                } else {
72                    $aliasFor = $commandName;
73                    $aliases[] = $attributes['command'];
74                }
75
76                $commandRegistryDefinition->addMethodCall('addLazyCommand', [
77                    $command,
78                    $serviceName,
79                    $description,
80                    $hidden,
81                    $schedulable,
82                    $aliasFor,
83                ]);
84            }
85            $commandServiceDefinition->addMethodCall('setName', [$commandName]);
86            if ($description) {
87                $commandServiceDefinition->addMethodCall('setDescription', [$description]);
88            }
89            if ($hidden) {
90                $commandServiceDefinition->addMethodCall('setHidden', [true]);
91            }
92            if ($aliases) {
93                $commandServiceDefinition->addMethodCall('setAliases', [$aliases]);
94            }
95        }
96    }
97}
98