|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MakerBundle\Maker; |
| 13 | + |
| 14 | +use Psr\Container\ContainerInterface; |
| 15 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 16 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 17 | +use Symfony\Bundle\MakerBundle\Generator; |
| 18 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 19 | +use Symfony\Bundle\MakerBundle\Str; |
| 20 | +use Symfony\Bundle\MakerBundle\Util\DecoratorInfo; |
| 21 | +use Symfony\Bundle\MakerBundle\Validator; |
| 22 | +use Symfony\Component\Console\Command\Command; |
| 23 | +use Symfony\Component\Console\Input\InputArgument; |
| 24 | +use Symfony\Component\Console\Input\InputInterface; |
| 25 | +use Symfony\Component\Console\Question\Question; |
| 26 | +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Benjamin Georgeault <[email protected]> |
| 30 | + */ |
| 31 | +final class MakeDecorator extends AbstractMaker |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @param array<string> $ids |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + private readonly ContainerInterface $container, |
| 38 | + private readonly array $ids, |
| 39 | + ) { |
| 40 | + } |
| 41 | + |
| 42 | + public static function getCommandName(): string |
| 43 | + { |
| 44 | + return 'make:decorator'; |
| 45 | + } |
| 46 | + |
| 47 | + public static function getCommandDescription(): string |
| 48 | + { |
| 49 | + return 'Create CRUD for Doctrine entity class'; |
| 50 | + } |
| 51 | + |
| 52 | + public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
| 53 | + { |
| 54 | + $command |
| 55 | + ->addArgument('id', InputArgument::OPTIONAL, 'The ID of the service to decorate.') |
| 56 | + ->addArgument('decorator-class', InputArgument::OPTIONAL, \sprintf('The class name of the service to create (e.g. <fg=yellow>%sDecorator</>)', Str::asClassName(Str::getRandomTerm()))) |
| 57 | + ->setHelp($this->getHelpFileContents('MakeDecorator.txt')) |
| 58 | + ; |
| 59 | + } |
| 60 | + |
| 61 | + public function configureDependencies(DependencyBuilder $dependencies): void |
| 62 | + { |
| 63 | + $dependencies->addClassDependency( |
| 64 | + AsDecorator::class, |
| 65 | + 'dependency-injection', |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
| 70 | + { |
| 71 | + // Ask for service id. |
| 72 | + if (null === $input->getArgument('id')) { |
| 73 | + $argument = $command->getDefinition()->getArgument('id'); |
| 74 | + |
| 75 | + ($question = new Question($argument->getDescription())) |
| 76 | + ->setAutocompleterValues($this->ids) |
| 77 | + ->setValidator(fn ($answer) => Validator::serviceExists($answer, $this->ids)) |
| 78 | + ->setMaxAttempts(3); |
| 79 | + |
| 80 | + $input->setArgument('id', $io->askQuestion($question)); |
| 81 | + } |
| 82 | + |
| 83 | + $id = $input->getArgument('id'); |
| 84 | + |
| 85 | + // Ask for decorator classname. |
| 86 | + if (null === $input->getArgument('decorator-class')) { |
| 87 | + $argument = $command->getDefinition()->getArgument('decorator-class'); |
| 88 | + |
| 89 | + $basename = Str::getShortClassName(match (true) { |
| 90 | + interface_exists($id) => Str::removeSuffix($id, 'Interface'), |
| 91 | + class_exists($id) => $id, |
| 92 | + default => Str::asClassName($id), |
| 93 | + }); |
| 94 | + |
| 95 | + $defaultClass = Str::asClassName(\sprintf('%s Decorator', $basename)); |
| 96 | + |
| 97 | + ($question = new Question($argument->getDescription(), $defaultClass)) |
| 98 | + ->setValidator(fn ($answer) => Validator::validateClassName(Validator::classDoesNotExist($answer))) |
| 99 | + ->setMaxAttempts(3); |
| 100 | + |
| 101 | + $input->setArgument('decorator-class', $io->askQuestion($question)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
| 106 | + { |
| 107 | + $id = $input->getArgument('id'); |
| 108 | + |
| 109 | + $classNameDetails = $generator->createClassNameDetails( |
| 110 | + Validator::validateClassName(Validator::classDoesNotExist($input->getArgument('decorator-class'))), |
| 111 | + '', |
| 112 | + ); |
| 113 | + |
| 114 | + $decoratedInfo = $this->createDecoratorInfo($id, $classNameDetails->getFullName()); |
| 115 | + $classData = $decoratedInfo->getClassData(); |
| 116 | + |
| 117 | + $generator->generateClassFromClassData( |
| 118 | + $classData, |
| 119 | + 'decorator/Decorator.tpl.php', |
| 120 | + [ |
| 121 | + 'decorated_info' => $decoratedInfo, |
| 122 | + ], |
| 123 | + ); |
| 124 | + |
| 125 | + $generator->writeChanges(); |
| 126 | + |
| 127 | + $this->writeSuccessMessage($io); |
| 128 | + } |
| 129 | + |
| 130 | + private function createDecoratorInfo(string $id, string $decoratorClass): DecoratorInfo |
| 131 | + { |
| 132 | + return new DecoratorInfo( |
| 133 | + $decoratorClass, |
| 134 | + match (true) { |
| 135 | + class_exists($id), interface_exists($id) => $id, |
| 136 | + default => $this->container->get($id)::class, |
| 137 | + }, |
| 138 | + $id, |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
0 commit comments