@@ -31,80 +31,80 @@ |
||
31 | 31 | */ |
32 | 32 | final class FullLifeCycleCommand implements Command, InitializableCommand, InteractiveCommand |
33 | 33 | { |
34 | - private bool $initialized = false; |
|
35 | - private QuestionHelper $questionHelper; |
|
36 | - |
|
37 | - public function __construct(QuestionHelper $questionHelper) |
|
38 | - { |
|
39 | - $this->questionHelper = $questionHelper; |
|
40 | - } |
|
41 | - |
|
42 | - public function getConfiguration(): Configuration |
|
43 | - { |
|
44 | - return new Configuration( |
|
45 | - 'app:full-life-cycle', |
|
46 | - '', |
|
47 | - '', |
|
48 | - [ |
|
49 | - new InputArgument( |
|
50 | - 'username', |
|
51 | - InputArgument::REQUIRED, |
|
52 | - 'Name of the user', |
|
53 | - ), |
|
54 | - ], |
|
55 | - ); |
|
56 | - } |
|
57 | - |
|
58 | - public function initialize(IO $io): void |
|
59 | - { |
|
60 | - $this->initialized = true; |
|
61 | - } |
|
62 | - |
|
63 | - public function interact(IO $io): void |
|
64 | - { |
|
65 | - if (!$this->initialized) { |
|
66 | - throw new LogicException('Expected the command to be initialized'); |
|
67 | - } |
|
68 | - |
|
69 | - $input = $io->getInput(); |
|
70 | - |
|
71 | - if ($input->getArgument('username')) { |
|
72 | - return; |
|
73 | - } |
|
74 | - |
|
75 | - $username = $this->askUsername($io); |
|
76 | - |
|
77 | - $input->setArgument('username', $username); |
|
78 | - } |
|
79 | - |
|
80 | - public function execute(IO $io): int |
|
81 | - { |
|
82 | - $io->newLine(); |
|
83 | - $io->writeln(self::getUsername($io)); |
|
84 | - |
|
85 | - return ExitCode::SUCCESS; |
|
86 | - } |
|
87 | - |
|
88 | - /** @psalm-suppress InvalidNullableReturnType, InvalidReturnStatement, InvalidReturnType, NullableReturnStatement */ |
|
89 | - private static function getUsername(IO $io): string |
|
90 | - { |
|
91 | - return $io->getInput()->getArgument('username'); |
|
92 | - } |
|
93 | - |
|
94 | - private function askUsername(IO $io): string |
|
95 | - { |
|
96 | - $question = new Question('Please choose a username:'); |
|
97 | - |
|
98 | - $question->setValidator( |
|
99 | - function (string $username) { |
|
100 | - if ('' === $username) { |
|
101 | - throw new Exception('Username can not be empty'); |
|
102 | - } |
|
103 | - |
|
104 | - return $username; |
|
105 | - }, |
|
106 | - ); |
|
107 | - |
|
108 | - return $this->questionHelper->ask($io, $question); |
|
109 | - } |
|
34 | + private bool $initialized = false; |
|
35 | + private QuestionHelper $questionHelper; |
|
36 | + |
|
37 | + public function __construct(QuestionHelper $questionHelper) |
|
38 | + { |
|
39 | + $this->questionHelper = $questionHelper; |
|
40 | + } |
|
41 | + |
|
42 | + public function getConfiguration(): Configuration |
|
43 | + { |
|
44 | + return new Configuration( |
|
45 | + 'app:full-life-cycle', |
|
46 | + '', |
|
47 | + '', |
|
48 | + [ |
|
49 | + new InputArgument( |
|
50 | + 'username', |
|
51 | + InputArgument::REQUIRED, |
|
52 | + 'Name of the user', |
|
53 | + ), |
|
54 | + ], |
|
55 | + ); |
|
56 | + } |
|
57 | + |
|
58 | + public function initialize(IO $io): void |
|
59 | + { |
|
60 | + $this->initialized = true; |
|
61 | + } |
|
62 | + |
|
63 | + public function interact(IO $io): void |
|
64 | + { |
|
65 | + if (!$this->initialized) { |
|
66 | + throw new LogicException('Expected the command to be initialized'); |
|
67 | + } |
|
68 | + |
|
69 | + $input = $io->getInput(); |
|
70 | + |
|
71 | + if ($input->getArgument('username')) { |
|
72 | + return; |
|
73 | + } |
|
74 | + |
|
75 | + $username = $this->askUsername($io); |
|
76 | + |
|
77 | + $input->setArgument('username', $username); |
|
78 | + } |
|
79 | + |
|
80 | + public function execute(IO $io): int |
|
81 | + { |
|
82 | + $io->newLine(); |
|
83 | + $io->writeln(self::getUsername($io)); |
|
84 | + |
|
85 | + return ExitCode::SUCCESS; |
|
86 | + } |
|
87 | + |
|
88 | + /** @psalm-suppress InvalidNullableReturnType, InvalidReturnStatement, InvalidReturnType, NullableReturnStatement */ |
|
89 | + private static function getUsername(IO $io): string |
|
90 | + { |
|
91 | + return $io->getInput()->getArgument('username'); |
|
92 | + } |
|
93 | + |
|
94 | + private function askUsername(IO $io): string |
|
95 | + { |
|
96 | + $question = new Question('Please choose a username:'); |
|
97 | + |
|
98 | + $question->setValidator( |
|
99 | + function (string $username) { |
|
100 | + if ('' === $username) { |
|
101 | + throw new Exception('Username can not be empty'); |
|
102 | + } |
|
103 | + |
|
104 | + return $username; |
|
105 | + }, |
|
106 | + ); |
|
107 | + |
|
108 | + return $this->questionHelper->ask($io, $question); |
|
109 | + } |
|
110 | 110 | } |
@@ -96,7 +96,7 @@ |
||
96 | 96 | $question = new Question('Please choose a username:'); |
97 | 97 | |
98 | 98 | $question->setValidator( |
99 | - function (string $username) { |
|
99 | + function(string $username) { |
|
100 | 100 | if ('' === $username) { |
101 | 101 | throw new Exception('Username can not be empty'); |
102 | 102 | } |
@@ -21,27 +21,27 @@ |
||
21 | 21 | |
22 | 22 | final class SimpleLazyCommand implements LazyCommand |
23 | 23 | { |
24 | - public function __construct(StatefulService $service) |
|
25 | - { |
|
26 | - $service->call(); |
|
27 | - } |
|
28 | - |
|
29 | - public static function getName(): string |
|
30 | - { |
|
31 | - return 'app:lazy'; |
|
32 | - } |
|
33 | - |
|
34 | - public function getConfiguration(): Configuration |
|
35 | - { |
|
36 | - return new Configuration( |
|
37 | - self::getName(), // TODO: add test to check that the lazy name takes over |
|
38 | - '', |
|
39 | - '', |
|
40 | - ); |
|
41 | - } |
|
42 | - |
|
43 | - public function execute(IO $io): int |
|
44 | - { |
|
45 | - return ExitCode::SUCCESS; |
|
46 | - } |
|
24 | + public function __construct(StatefulService $service) |
|
25 | + { |
|
26 | + $service->call(); |
|
27 | + } |
|
28 | + |
|
29 | + public static function getName(): string |
|
30 | + { |
|
31 | + return 'app:lazy'; |
|
32 | + } |
|
33 | + |
|
34 | + public function getConfiguration(): Configuration |
|
35 | + { |
|
36 | + return new Configuration( |
|
37 | + self::getName(), // TODO: add test to check that the lazy name takes over |
|
38 | + '', |
|
39 | + '', |
|
40 | + ); |
|
41 | + } |
|
42 | + |
|
43 | + public function execute(IO $io): int |
|
44 | + { |
|
45 | + return ExitCode::SUCCESS; |
|
46 | + } |
|
47 | 47 | } |
@@ -34,7 +34,7 @@ |
||
34 | 34 | public function getConfiguration(): Configuration |
35 | 35 | { |
36 | 36 | return new Configuration( |
37 | - self::getName(), // TODO: add test to check that the lazy name takes over |
|
37 | + self::getName(), // TODO: add test to check that the lazy name takes over |
|
38 | 38 | '', |
39 | 39 | '', |
40 | 40 | ); |
@@ -19,8 +19,7 @@ |
||
19 | 19 | use Fidry\Console\IO; |
20 | 20 | use Fidry\Console\Tests\StatefulService; |
21 | 21 | |
22 | -final class SimpleLazyCommand implements LazyCommand |
|
23 | -{ |
|
22 | +final class SimpleLazyCommand implements LazyCommand { |
|
24 | 23 | public function __construct(StatefulService $service) |
25 | 24 | { |
26 | 25 | $service->call(); |
@@ -21,26 +21,26 @@ |
||
21 | 21 | |
22 | 22 | final class CommandWithService implements Command |
23 | 23 | { |
24 | - private StatefulService $service; |
|
25 | - |
|
26 | - public function __construct(StatefulService $service) |
|
27 | - { |
|
28 | - $this->service = $service; |
|
29 | - } |
|
30 | - |
|
31 | - public function getConfiguration(): Configuration |
|
32 | - { |
|
33 | - return new Configuration( |
|
34 | - 'app:with-service', |
|
35 | - '', |
|
36 | - '', |
|
37 | - ); |
|
38 | - } |
|
39 | - |
|
40 | - public function execute(IO $io): int |
|
41 | - { |
|
42 | - $this->service->call(); |
|
43 | - |
|
44 | - return ExitCode::SUCCESS; |
|
45 | - } |
|
24 | + private StatefulService $service; |
|
25 | + |
|
26 | + public function __construct(StatefulService $service) |
|
27 | + { |
|
28 | + $this->service = $service; |
|
29 | + } |
|
30 | + |
|
31 | + public function getConfiguration(): Configuration |
|
32 | + { |
|
33 | + return new Configuration( |
|
34 | + 'app:with-service', |
|
35 | + '', |
|
36 | + '', |
|
37 | + ); |
|
38 | + } |
|
39 | + |
|
40 | + public function execute(IO $io): int |
|
41 | + { |
|
42 | + $this->service->call(); |
|
43 | + |
|
44 | + return ExitCode::SUCCESS; |
|
45 | + } |
|
46 | 46 | } |
@@ -19,8 +19,7 @@ |
||
19 | 19 | use Fidry\Console\IO; |
20 | 20 | use Fidry\Console\Tests\StatefulService; |
21 | 21 | |
22 | -final class CommandWithService implements Command |
|
23 | -{ |
|
22 | +final class CommandWithService implements Command { |
|
24 | 23 | private StatefulService $service; |
25 | 24 | |
26 | 25 | public function __construct(StatefulService $service) |
@@ -27,54 +27,54 @@ |
||
27 | 27 | */ |
28 | 28 | final class CommandWithArgumentAndOption implements Command |
29 | 29 | { |
30 | - public function getConfiguration(): Configuration |
|
31 | - { |
|
32 | - return new Configuration( |
|
33 | - 'app:print-arg-opt', |
|
34 | - '', |
|
35 | - '', |
|
36 | - [ |
|
37 | - new InputArgument( |
|
38 | - 'arg', |
|
39 | - InputArgument::REQUIRED, |
|
40 | - 'An arbitrary string', |
|
41 | - ), |
|
42 | - ], |
|
43 | - [ |
|
44 | - new InputOption( |
|
45 | - 'opt', |
|
46 | - 'o', |
|
47 | - InputOption::VALUE_NONE, |
|
48 | - ), |
|
49 | - ], |
|
50 | - ); |
|
51 | - } |
|
30 | + public function getConfiguration(): Configuration |
|
31 | + { |
|
32 | + return new Configuration( |
|
33 | + 'app:print-arg-opt', |
|
34 | + '', |
|
35 | + '', |
|
36 | + [ |
|
37 | + new InputArgument( |
|
38 | + 'arg', |
|
39 | + InputArgument::REQUIRED, |
|
40 | + 'An arbitrary string', |
|
41 | + ), |
|
42 | + ], |
|
43 | + [ |
|
44 | + new InputOption( |
|
45 | + 'opt', |
|
46 | + 'o', |
|
47 | + InputOption::VALUE_NONE, |
|
48 | + ), |
|
49 | + ], |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | |
53 | - public function execute(IO $io): int |
|
54 | - { |
|
55 | - $arg = self::getArg($io); |
|
56 | - $opt = self::getOpt($io); |
|
53 | + public function execute(IO $io): int |
|
54 | + { |
|
55 | + $arg = self::getArg($io); |
|
56 | + $opt = self::getOpt($io); |
|
57 | 57 | |
58 | - $io->writeln( |
|
59 | - sprintf( |
|
60 | - 'arg: %s; opt: %s', |
|
61 | - $arg, |
|
62 | - $opt ? 'true' : 'false', |
|
63 | - ), |
|
64 | - ); |
|
58 | + $io->writeln( |
|
59 | + sprintf( |
|
60 | + 'arg: %s; opt: %s', |
|
61 | + $arg, |
|
62 | + $opt ? 'true' : 'false', |
|
63 | + ), |
|
64 | + ); |
|
65 | 65 | |
66 | - return ExitCode::SUCCESS; |
|
67 | - } |
|
66 | + return ExitCode::SUCCESS; |
|
67 | + } |
|
68 | 68 | |
69 | - /** @psalm-suppress InvalidNullableReturnType, InvalidReturnStatement, InvalidReturnType, NullableReturnStatement */ |
|
70 | - private static function getArg(IO $io): string |
|
71 | - { |
|
72 | - return $io->getInput()->getArgument('arg'); |
|
73 | - } |
|
69 | + /** @psalm-suppress InvalidNullableReturnType, InvalidReturnStatement, InvalidReturnType, NullableReturnStatement */ |
|
70 | + private static function getArg(IO $io): string |
|
71 | + { |
|
72 | + return $io->getInput()->getArgument('arg'); |
|
73 | + } |
|
74 | 74 | |
75 | - /** @psalm-suppress InvalidNullableReturnType, InvalidReturnStatement, InvalidReturnType, NullableReturnStatement */ |
|
76 | - private static function getOpt(IO $io): bool |
|
77 | - { |
|
78 | - return $io->getInput()->getOption('opt'); |
|
79 | - } |
|
75 | + /** @psalm-suppress InvalidNullableReturnType, InvalidReturnStatement, InvalidReturnType, NullableReturnStatement */ |
|
76 | + private static function getOpt(IO $io): bool |
|
77 | + { |
|
78 | + return $io->getInput()->getOption('opt'); |
|
79 | + } |
|
80 | 80 | } |
@@ -25,8 +25,7 @@ |
||
25 | 25 | * A simple command with a required argument and displaying the argument value |
26 | 26 | * and option during its execution. |
27 | 27 | */ |
28 | -final class CommandWithArgumentAndOption implements Command |
|
29 | -{ |
|
28 | +final class CommandWithArgumentAndOption implements Command { |
|
30 | 29 | public function getConfiguration(): Configuration |
31 | 30 | { |
32 | 31 | return new Configuration( |
@@ -23,25 +23,25 @@ |
||
23 | 23 | |
24 | 24 | final class CommandAwareCommand implements Command, CommandAware, InitializableCommand |
25 | 25 | { |
26 | - use CommandAwareness; |
|
27 | - |
|
28 | - public function getConfiguration(): Configuration |
|
29 | - { |
|
30 | - return new Configuration( |
|
31 | - 'app:cmd-aware', |
|
32 | - '', |
|
33 | - '', |
|
34 | - ); |
|
35 | - } |
|
36 | - |
|
37 | - public function initialize(IO $io): void |
|
38 | - { |
|
39 | - $command = $this->getCommandRegistry()->getCommand('app:with-service'); |
|
40 | - $command->execute($io); |
|
41 | - } |
|
42 | - |
|
43 | - public function execute(IO $io): int |
|
44 | - { |
|
45 | - return ExitCode::SUCCESS; |
|
46 | - } |
|
26 | + use CommandAwareness; |
|
27 | + |
|
28 | + public function getConfiguration(): Configuration |
|
29 | + { |
|
30 | + return new Configuration( |
|
31 | + 'app:cmd-aware', |
|
32 | + '', |
|
33 | + '', |
|
34 | + ); |
|
35 | + } |
|
36 | + |
|
37 | + public function initialize(IO $io): void |
|
38 | + { |
|
39 | + $command = $this->getCommandRegistry()->getCommand('app:with-service'); |
|
40 | + $command->execute($io); |
|
41 | + } |
|
42 | + |
|
43 | + public function execute(IO $io): int |
|
44 | + { |
|
45 | + return ExitCode::SUCCESS; |
|
46 | + } |
|
47 | 47 | } |
@@ -24,17 +24,17 @@ |
||
24 | 24 | */ |
25 | 25 | final class SimpleCommand implements Command |
26 | 26 | { |
27 | - public function getConfiguration(): Configuration |
|
28 | - { |
|
29 | - return new Configuration( |
|
30 | - 'app:foo', |
|
31 | - 'Description content', |
|
32 | - 'Command name: "%command.name%", command full name: "%command.full_name%"', |
|
33 | - ); |
|
34 | - } |
|
27 | + public function getConfiguration(): Configuration |
|
28 | + { |
|
29 | + return new Configuration( |
|
30 | + 'app:foo', |
|
31 | + 'Description content', |
|
32 | + 'Command name: "%command.name%", command full name: "%command.full_name%"', |
|
33 | + ); |
|
34 | + } |
|
35 | 35 | |
36 | - public function execute(IO $io): int |
|
37 | - { |
|
38 | - return ExitCode::SUCCESS; |
|
39 | - } |
|
36 | + public function execute(IO $io): int |
|
37 | + { |
|
38 | + return ExitCode::SUCCESS; |
|
39 | + } |
|
40 | 40 | } |
@@ -22,8 +22,7 @@ |
||
22 | 22 | * Most basic command: only has a name & description and does not do anything |
23 | 23 | * during execution. |
24 | 24 | */ |
25 | -final class SimpleCommand implements Command |
|
26 | -{ |
|
25 | +final class SimpleCommand implements Command { |
|
27 | 26 | public function getConfiguration(): Configuration |
28 | 27 | { |
29 | 28 | return new Configuration( |
@@ -20,21 +20,21 @@ |
||
20 | 20 | |
21 | 21 | final class Kernel extends SymfonyKernel |
22 | 22 | { |
23 | - public function registerBundles(): array |
|
24 | - { |
|
25 | - return [ |
|
26 | - new FrameworkBundle(), |
|
27 | - new FidryConsoleBundle(), |
|
28 | - ]; |
|
29 | - } |
|
23 | + public function registerBundles(): array |
|
24 | + { |
|
25 | + return [ |
|
26 | + new FrameworkBundle(), |
|
27 | + new FidryConsoleBundle(), |
|
28 | + ]; |
|
29 | + } |
|
30 | 30 | |
31 | - public function getProjectDir(): string |
|
32 | - { |
|
33 | - return __DIR__; |
|
34 | - } |
|
31 | + public function getProjectDir(): string |
|
32 | + { |
|
33 | + return __DIR__; |
|
34 | + } |
|
35 | 35 | |
36 | - public function registerContainerConfiguration(LoaderInterface $loader): void |
|
37 | - { |
|
38 | - $loader->load(__DIR__.'/services.yaml'); |
|
39 | - } |
|
36 | + public function registerContainerConfiguration(LoaderInterface $loader): void |
|
37 | + { |
|
38 | + $loader->load(__DIR__.'/services.yaml'); |
|
39 | + } |
|
40 | 40 | } |
@@ -18,8 +18,7 @@ |
||
18 | 18 | use Symfony\Component\Config\Loader\LoaderInterface; |
19 | 19 | use Symfony\Component\HttpKernel\Kernel as SymfonyKernel; |
20 | 20 | |
21 | -final class Kernel extends SymfonyKernel |
|
22 | -{ |
|
21 | +final class Kernel extends SymfonyKernel { |
|
23 | 22 | public function registerBundles(): array |
24 | 23 | { |
25 | 24 | return [ |
@@ -21,15 +21,15 @@ |
||
21 | 21 | |
22 | 22 | final class FidryConsoleExtension extends Extension |
23 | 23 | { |
24 | - private const SERVICES_DIR = __DIR__.'/../../resources/config'; |
|
24 | + private const SERVICES_DIR = __DIR__.'/../../resources/config'; |
|
25 | 25 | |
26 | - public function load(array $configs, ContainerBuilder $container): void |
|
27 | - { |
|
28 | - $loader = new XmlFileLoader($container, new FileLocator(self::SERVICES_DIR)); |
|
29 | - $loader->load('services.xml'); |
|
26 | + public function load(array $configs, ContainerBuilder $container): void |
|
27 | + { |
|
28 | + $loader = new XmlFileLoader($container, new FileLocator(self::SERVICES_DIR)); |
|
29 | + $loader->load('services.xml'); |
|
30 | 30 | |
31 | - $container |
|
32 | - ->registerForAutoconfiguration(Command::class) |
|
33 | - ->addTag('fidry.console_command'); |
|
34 | - } |
|
31 | + $container |
|
32 | + ->registerForAutoconfiguration(Command::class) |
|
33 | + ->addTag('fidry.console_command'); |
|
34 | + } |
|
35 | 35 | } |
@@ -19,8 +19,7 @@ |
||
19 | 19 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
20 | 20 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
21 | 21 | |
22 | -final class FidryConsoleExtension extends Extension |
|
23 | -{ |
|
22 | +final class FidryConsoleExtension extends Extension { |
|
24 | 23 | private const SERVICES_DIR = __DIR__.'/../../resources/config'; |
25 | 24 | |
26 | 25 | public function load(array $configs, ContainerBuilder $container): void |
@@ -28,85 +28,85 @@ |
||
28 | 28 | */ |
29 | 29 | final class AddConsoleCommandPass implements CompilerPassInterface |
30 | 30 | { |
31 | - public function process(ContainerBuilder $container): void |
|
32 | - { |
|
33 | - $container->addDefinitions( |
|
34 | - self::createDefinitions($container), |
|
35 | - ); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @return array<string, Definition> |
|
40 | - */ |
|
41 | - private static function createDefinitions(ContainerBuilder $containerBuilder): array |
|
42 | - { |
|
43 | - $tagsByServiceId = $containerBuilder->findTaggedServiceIds('fidry.console_command'); |
|
44 | - |
|
45 | - $commandDefinitions = []; |
|
46 | - |
|
47 | - foreach ($tagsByServiceId as $id => $_tags) { |
|
48 | - $commandDefinitions[$id] = self::createDefinition($id, $containerBuilder); |
|
49 | - } |
|
50 | - |
|
51 | - return $commandDefinitions; |
|
52 | - } |
|
53 | - |
|
54 | - private static function createDefinition( |
|
55 | - string $id, |
|
56 | - ContainerBuilder $containerBuilder |
|
57 | - ): Definition { |
|
58 | - $decoratedCommandDefinition = $containerBuilder->getDefinition($id); |
|
59 | - |
|
60 | - $commandName = self::getCommandName( |
|
61 | - $id, |
|
62 | - $decoratedCommandDefinition, |
|
63 | - $containerBuilder, |
|
64 | - ); |
|
65 | - |
|
66 | - $definition = new Definition( |
|
67 | - SymfonyCommand::class, |
|
68 | - [$decoratedCommandDefinition], |
|
69 | - ); |
|
70 | - |
|
71 | - $definition->setTags( |
|
72 | - [ |
|
73 | - 'console.command' => null !== $commandName |
|
74 | - ? [['command' => $commandName]] |
|
75 | - : [], |
|
76 | - ] |
|
77 | - ); |
|
78 | - |
|
79 | - return $definition; |
|
80 | - } |
|
81 | - |
|
82 | - private static function getCommandName( |
|
83 | - string $id, |
|
84 | - Definition $definition, |
|
85 | - ContainerBuilder $containerBuilder |
|
86 | - ): ?string { |
|
87 | - $className = $definition->getClass(); |
|
88 | - |
|
89 | - if (null === $className) { |
|
90 | - return null; |
|
91 | - } |
|
92 | - |
|
93 | - $classReflection = $containerBuilder->getReflectionClass($className); |
|
94 | - |
|
95 | - if (null === $classReflection) { |
|
96 | - throw new InvalidArgumentException( |
|
97 | - sprintf( |
|
98 | - 'Class "%s" used for service "%s" cannot be found.', |
|
99 | - $className, |
|
100 | - $id, |
|
101 | - ), |
|
102 | - ); |
|
103 | - } |
|
104 | - |
|
105 | - if ($classReflection->isSubclassOf(LazyCommand::class)) { |
|
106 | - /** @var class-string<LazyCommand> $className */ |
|
107 | - return $className::getName(); |
|
108 | - } |
|
109 | - |
|
110 | - return null; |
|
111 | - } |
|
31 | + public function process(ContainerBuilder $container): void |
|
32 | + { |
|
33 | + $container->addDefinitions( |
|
34 | + self::createDefinitions($container), |
|
35 | + ); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @return array<string, Definition> |
|
40 | + */ |
|
41 | + private static function createDefinitions(ContainerBuilder $containerBuilder): array |
|
42 | + { |
|
43 | + $tagsByServiceId = $containerBuilder->findTaggedServiceIds('fidry.console_command'); |
|
44 | + |
|
45 | + $commandDefinitions = []; |
|
46 | + |
|
47 | + foreach ($tagsByServiceId as $id => $_tags) { |
|
48 | + $commandDefinitions[$id] = self::createDefinition($id, $containerBuilder); |
|
49 | + } |
|
50 | + |
|
51 | + return $commandDefinitions; |
|
52 | + } |
|
53 | + |
|
54 | + private static function createDefinition( |
|
55 | + string $id, |
|
56 | + ContainerBuilder $containerBuilder |
|
57 | + ): Definition { |
|
58 | + $decoratedCommandDefinition = $containerBuilder->getDefinition($id); |
|
59 | + |
|
60 | + $commandName = self::getCommandName( |
|
61 | + $id, |
|
62 | + $decoratedCommandDefinition, |
|
63 | + $containerBuilder, |
|
64 | + ); |
|
65 | + |
|
66 | + $definition = new Definition( |
|
67 | + SymfonyCommand::class, |
|
68 | + [$decoratedCommandDefinition], |
|
69 | + ); |
|
70 | + |
|
71 | + $definition->setTags( |
|
72 | + [ |
|
73 | + 'console.command' => null !== $commandName |
|
74 | + ? [['command' => $commandName]] |
|
75 | + : [], |
|
76 | + ] |
|
77 | + ); |
|
78 | + |
|
79 | + return $definition; |
|
80 | + } |
|
81 | + |
|
82 | + private static function getCommandName( |
|
83 | + string $id, |
|
84 | + Definition $definition, |
|
85 | + ContainerBuilder $containerBuilder |
|
86 | + ): ?string { |
|
87 | + $className = $definition->getClass(); |
|
88 | + |
|
89 | + if (null === $className) { |
|
90 | + return null; |
|
91 | + } |
|
92 | + |
|
93 | + $classReflection = $containerBuilder->getReflectionClass($className); |
|
94 | + |
|
95 | + if (null === $classReflection) { |
|
96 | + throw new InvalidArgumentException( |
|
97 | + sprintf( |
|
98 | + 'Class "%s" used for service "%s" cannot be found.', |
|
99 | + $className, |
|
100 | + $id, |
|
101 | + ), |
|
102 | + ); |
|
103 | + } |
|
104 | + |
|
105 | + if ($classReflection->isSubclassOf(LazyCommand::class)) { |
|
106 | + /** @var class-string<LazyCommand> $className */ |
|
107 | + return $className::getName(); |
|
108 | + } |
|
109 | + |
|
110 | + return null; |
|
111 | + } |
|
112 | 112 | } |
@@ -26,8 +26,7 @@ |
||
26 | 26 | * Symfony commands. This allows to provide the API at minimal cost since it |
27 | 27 | * leverages all the FrameworkBundle work already done. |
28 | 28 | */ |
29 | -final class AddConsoleCommandPass implements CompilerPassInterface |
|
30 | -{ |
|
29 | +final class AddConsoleCommandPass implements CompilerPassInterface { |
|
31 | 30 | public function process(ContainerBuilder $container): void |
32 | 31 | { |
33 | 32 | $container->addDefinitions( |