|
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; |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: