|
| 1 | +<?php |
| 2 | + |
| 3 | +use a9f\FractorDocGenerator\Console\Factory\SymfonyStyleFactory; |
| 4 | +use a9f\FractorDocGenerator\Differ\DifferFactory; |
| 5 | +use a9f\FractorDocGenerator\FractorDocGeneratorApplication; |
| 6 | +use a9f\FractorDocGenerator\Printer\CodeSamplePrinter; |
| 7 | +use SebastianBergmann\Diff\Differ; |
| 8 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 9 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 10 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 12 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 13 | +use Symplify\RuleDocGenerator\Contract\RuleCodeSamplePrinterInterface; |
| 14 | +use Symplify\RuleDocGenerator\FileSystem\ClassByTypeFinder; |
| 15 | +use Symplify\RuleDocGenerator\RuleDefinitionsResolver; |
| 16 | +use Symplify\RuleDocGenerator\Text\KeywordHighlighter; |
| 17 | +use function Symfony\Component\DependencyInjection\Loader\Configurator\service; |
| 18 | +use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator; |
| 19 | + |
| 20 | +return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void { |
| 21 | + $services = $containerConfigurator->services(); |
| 22 | + $services->defaults() |
| 23 | + ->autowire() |
| 24 | + ->public() |
| 25 | + ->autoconfigure(); |
| 26 | + |
| 27 | + $services->load('a9f\\FractorDocGenerator\\', __DIR__ . '/../src/'); |
| 28 | + $services->set(ClassByTypeFinder::class); |
| 29 | + $services->set(RuleDefinitionsResolver::class); |
| 30 | + $services->set(KeywordHighlighter::class); |
| 31 | + $services->set(Differ::class)->factory([service(DifferFactory::class), 'create']); |
| 32 | + |
| 33 | + $services->set(FractorDocGeneratorApplication::class) |
| 34 | + ->call('setCommandLoader', [service('console.command_loader')]) |
| 35 | + ->public(); |
| 36 | + |
| 37 | + $services->set(SymfonyStyle::class)->factory([service(SymfonyStyleFactory::class), 'create']); |
| 38 | + |
| 39 | + $containerBuilder->registerAttributeForAutoconfiguration( |
| 40 | + AsCommand::class, |
| 41 | + static function (ChildDefinition $definition, AsCommand $attribute): void { |
| 42 | + $commands = explode('|', $attribute->name); |
| 43 | + $hidden = false; |
| 44 | + $name = array_shift($commands); |
| 45 | + |
| 46 | + if ($name === '') { |
| 47 | + // Symfony AsCommand attribute encodes hidden flag as an empty command name |
| 48 | + $hidden = true; |
| 49 | + $name = array_shift($commands); |
| 50 | + } |
| 51 | + |
| 52 | + if ($name === null) { |
| 53 | + // This happens in case no name and no aliases are given |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $definition->addTag( |
| 58 | + 'console.command', |
| 59 | + [ |
| 60 | + 'command' => $name, |
| 61 | + 'description' => $attribute->description, |
| 62 | + 'hidden' => $hidden, |
| 63 | + ] |
| 64 | + ); |
| 65 | + |
| 66 | + foreach ($commands as $name) { |
| 67 | + $definition->addTag( |
| 68 | + 'console.command', |
| 69 | + [ |
| 70 | + 'command' => $name, |
| 71 | + 'hidden' => $hidden, |
| 72 | + 'alias' => true, |
| 73 | + ] |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + $services->set(CodeSamplePrinter::class)->arg('$ruleCodeSamplePrinters', tagged_iterator('fractor_doc_generator.rule_code_sample_printer')); |
| 80 | + |
| 81 | + $containerBuilder->registerForAutoconfiguration(RuleCodeSamplePrinterInterface::class)->addTag('fractor_doc_generator.rule_code_sample_printer'); |
| 82 | +}; |
0 commit comments