|
6 | 6 | use a9f\Fractor\Configuration\ConfigurationFactory;
|
7 | 7 | use a9f\Fractor\Configuration\Option;
|
8 | 8 | use a9f\Fractor\Configuration\ValueObject\Configuration;
|
| 9 | +use a9f\Fractor\FractorApplication; |
| 10 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 11 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
9 | 12 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
10 | 13 | use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
11 | 14 | use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
|
|
34 | 37 | ]
|
35 | 38 | );
|
36 | 39 |
|
| 40 | + $containerBuilder->registerAttributeForAutoconfiguration( |
| 41 | + AsCommand::class, |
| 42 | + static function (ChildDefinition $definition, AsCommand $attribute): void { |
| 43 | + $commands = explode('|', $attribute->name); |
| 44 | + $hidden = false; |
| 45 | + $name = array_shift($commands); |
| 46 | + |
| 47 | + if ($name === '') { |
| 48 | + // Symfony AsCommand attribute encodes hidden flag as an empty command name |
| 49 | + $hidden = true; |
| 50 | + $name = array_shift($commands); |
| 51 | + } |
| 52 | + |
| 53 | + if ($name === null) { |
| 54 | + // This happens in case no name and no aliases are given |
| 55 | + // @todo Throw exception |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $definition->addTag( |
| 60 | + 'console.command', |
| 61 | + [ |
| 62 | + 'command' => $name, |
| 63 | + 'description' => $attribute->description, |
| 64 | + 'hidden' => $hidden, |
| 65 | + ] |
| 66 | + ); |
| 67 | + |
| 68 | + foreach ($commands as $name) { |
| 69 | + $definition->addTag( |
| 70 | + 'console.command', |
| 71 | + [ |
| 72 | + 'command' => $name, |
| 73 | + 'hidden' => $hidden, |
| 74 | + 'alias' => true, |
| 75 | + ] |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + ); |
| 80 | + |
37 | 81 | $services->set('parameter_bag', ContainerBag::class)
|
38 | 82 | ->args([
|
39 | 83 | service('service_container'),
|
40 | 84 | ])
|
41 | 85 | ->alias(ContainerBagInterface::class, 'parameter_bag')
|
42 | 86 | ->alias(ParameterBagInterface::class, 'parameter_bag');
|
43 | 87 |
|
| 88 | + $services->set(FractorApplication::class)->call('setCommandLoader', [service('console.command_loader')]); |
44 | 89 | $services->set(Configuration::class)->factory([service(ConfigurationFactory::class), 'create']);
|
45 | 90 | $services->set(FractorRunner::class)->arg('$processors', tagged_iterator('fractor.file_processor'));
|
46 | 91 | $services->set(AllowedFileExtensionsResolver::class)->arg('$processors', tagged_iterator('fractor.file_processor'));
|
|
0 commit comments