-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathConsoleBundle.php
More file actions
106 lines (92 loc) · 4.77 KB
/
ConsoleBundle.php
File metadata and controls
106 lines (92 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console;
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\Console\ArgumentResolver\ValueResolver\ValueResolverInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\AsTargetedValueResolver;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
use Symfony\Component\Console\DependencyInjection\ConsoleArgumentValueResolverPass;
use Symfony\Component\Console\DependencyInjection\RegisterCommandArgumentLocatorsPass;
use Symfony\Component\Console\DependencyInjection\RemoveEmptyCommandArgumentLocatorsPass;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Kernel\AbstractBundle;
use Symfony\Component\DependencyInjection\Kernel\RequiredBundle;
use Symfony\Component\DependencyInjection\Kernel\ServicesBundle;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Dotenv\Command\DebugCommand as DotenvDebugCommand;
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
#[RequiredBundle(ServicesBundle::class)]
class ConsoleBundle extends AbstractBundle
{
public function getPath(): string
{
return $this->path ??= __DIR__;
}
public function build(ContainerBuilder $container): void
{
$this->addCompilerPassIfExists($container, AddEventAliasesPass::class, arguments: [ConsoleEvents::ALIASES, [], [ConsoleEvents::COMMAND, ConsoleEvents::TERMINATE, ConsoleEvents::ERROR]]);
$container->addCompilerPass(new RegisterCommandArgumentLocatorsPass());
$container->addCompilerPass(new RemoveEmptyCommandArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->addCompilerPass(new ConsoleArgumentValueResolverPass());
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
}
public function loadExtension(array $config, ContainerConfigurator $configurator, ContainerBuilder $container): void
{
$configurator->import('Resources/config/console.php');
$container->registerForAutoconfiguration(Command::class)
->addTag('console.command')
->addTag('console.command.service_arguments');
$container->registerForAutoconfiguration(ValueResolverInterface::class)
->addTag('console.argument_value_resolver');
$container->registerAttributeForAutoconfiguration(AsCommand::class, static function (ChildDefinition $definition, AsCommand $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
$tagAttributes = [
'command' => $attribute->name,
'description' => $attribute->description,
'help' => $attribute->help ?? null,
];
if ($reflector instanceof \ReflectionMethod) {
$tagAttributes['method'] = $reflector->getName();
}
$definition->addTag('console.command', $tagAttributes);
$definition->addTag('console.command.service_arguments');
});
$container->registerAttributeForAutoconfiguration(AsTargetedValueResolver::class, static function (ChildDefinition $definition, AsTargetedValueResolver $attribute): void {
$definition->addTag('console.targeted_value_resolver', $attribute->name ? ['name' => $attribute->name] : []);
});
if (!class_exists(DotenvDebugCommand::class)) {
$container->removeDefinition('console.command.dotenv_debug');
}
if (!interface_exists(EventDispatcherInterface::class)) {
$container->removeDefinition('console.error_listener');
}
}
/**
* @template T of CompilerPassInterface
*
* @param class-string<T> $class
*
* @return T|null
*/
private function addCompilerPassIfExists(ContainerBuilder $container, string $class, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0, array $arguments = []): CompilerPassInterface
{
$container->addResource(new ClassExistenceResource($class));
$pass = null;
if (class_exists($class)) {
$container->addCompilerPass($pass = new $class(...$arguments), $type, $priority);
}
return $pass;
}
}