Completed
Push — master ( 335697...3db01f )
by
unknown
27:48 queued 12:55
created

ConsoleCommandPass::process()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 22
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 30
rs 8.4444
1
<?php
2
declare(strict_types = 1);
3
namespace TYPO3\CMS\Core\DependencyInjection;
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
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TYPO3\CMS\Core\Dependenc...ection\ContainerBuilder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
use TYPO3\CMS\Core\Console\CommandRegistry;
21
22
/**
23
 * @internal
24
 */
25
final class ConsoleCommandPass implements CompilerPassInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $tagName;
31
32
    /**
33
     * @param string $tagName
34
     */
35
    public function __construct(string $tagName)
36
    {
37
        $this->tagName = $tagName;
38
    }
39
40
    /**
41
     * @param ContainerBuilder $container
42
     */
43
    public function process(ContainerBuilder $container)
44
    {
45
        $commandRegistryDefinition = $container->findDefinition(CommandRegistry::class);
46
        if (!$commandRegistryDefinition) {
0 ignored issues
show
introduced by
$commandRegistryDefinition is of type Symfony\Component\DependencyInjection\Definition, thus it always evaluated to true.
Loading history...
47
            return;
48
        }
49
50
        foreach ($container->findTaggedServiceIds($this->tagName) as $serviceName => $tags) {
51
            $commandServiceDefinition = $container->findDefinition($serviceName)->setPublic(true);
52
            $commandName = null;
53
            $aliases = [];
54
            foreach ($tags as $attributes) {
55
                if (!isset($attributes['command'])) {
56
                    continue;
57
                }
58
                $commandRegistryDefinition->addMethodCall('addLazyCommand', [
59
                    $attributes['command'],
60
                    $serviceName,
61
                    (bool)($attributes['schedulable'] ?? true)
62
                ]);
63
                $isAlias = isset($commandName) || ($attributes['alias'] ?? false);
64
                if (!$isAlias) {
65
                    $commandName = $attributes['command'];
66
                } else {
67
                    $aliases[] = $attributes['command'];
68
                }
69
            }
70
            $commandServiceDefinition->addMethodCall('setName', [$commandName]);
71
            if ($aliases) {
72
                $commandServiceDefinition->addMethodCall('setAliases', [$aliases]);
73
            }
74
        }
75
    }
76
}
77