Completed
Push — master ( 4bf7f4...98cf5d )
by
unknown
22:36
created

CommandRegistry::populateCommandsFromPackages()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 22
c 0
b 0
f 0
nc 15
nop 0
dl 0
loc 43
rs 8.0555
1
<?php
2
3
declare(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
18
namespace TYPO3\CMS\Core\Console;
19
20
use Psr\Container\ContainerInterface;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
23
use Symfony\Component\Console\Exception\CommandNotFoundException;
24
use TYPO3\CMS\Core\SingletonInterface;
25
26
/**
27
 * Registry for Symfony commands, populated via dependency injection tags
28
 */
29
class CommandRegistry implements CommandLoaderInterface, SingletonInterface
30
{
31
    /**
32
     * @var ContainerInterface
33
     */
34
    protected $container;
35
36
    /**
37
     * Map of command configurations with the command name as key
38
     *
39
     * @var array[]
40
     */
41
    protected $commandConfigurations = [];
42
43
    /**
44
     * @param ContainerInterface $container
45
     */
46
    public function __construct(ContainerInterface $container)
47
    {
48
        $this->container = $container;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function has($name)
55
    {
56
        return array_key_exists($name, $this->commandConfigurations);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function get($name)
63
    {
64
        try {
65
            return $this->getCommandByIdentifier($name);
66
        } catch (UnknownCommandException $e) {
67
            throw new CommandNotFoundException($e->getMessage(), [], 1567969355, $e);
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getNames()
75
    {
76
        return array_keys($this->commandConfigurations);
77
    }
78
79
    /**
80
     * Get all commands which are allowed for scheduling recurring commands.
81
     *
82
     * @return \Generator
83
     */
84
    public function getSchedulableCommands(): \Generator
85
    {
86
        foreach ($this->commandConfigurations as $commandName => $configuration) {
87
            if ($configuration['schedulable'] ?? true) {
88
                yield $commandName => $this->getInstance($configuration['serviceName']);
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param string $identifier
95
     * @throws UnknownCommandException
96
     * @return Command
97
     */
98
    public function getCommandByIdentifier(string $identifier): Command
99
    {
100
        if (!isset($this->commandConfigurations[$identifier])) {
101
            throw new UnknownCommandException(
102
                sprintf('Command "%s" has not been registered.', $identifier),
103
                1510906768
104
            );
105
        }
106
107
        return $this->getInstance($this->commandConfigurations[$identifier]['serviceName']);
108
    }
109
110
    protected function getInstance(string $service): Command
111
    {
112
        return $this->container->get($service);
113
    }
114
115
    /**
116
     * @internal
117
     */
118
    public function addLazyCommand(string $commandName, string $serviceName, bool $schedulable = true): void
119
    {
120
        $this->commandConfigurations[$commandName] = [
121
            'serviceName' => $serviceName,
122
            'schedulable' => $schedulable,
123
        ];
124
    }
125
}
126