|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 15 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 16 | +use Symfony\Bundle\MakerBundle\DependencyInjection\DecoratorHelper; |
| 17 | +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 18 | +use Symfony\Bundle\MakerBundle\Generator; |
| 19 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 20 | +use Symfony\Bundle\MakerBundle\Str; |
| 21 | +use Symfony\Bundle\MakerBundle\Util\DecoratorInfo; |
| 22 | +use Symfony\Bundle\MakerBundle\Validator; |
| 23 | +use Symfony\Component\Console\Command\Command; |
| 24 | +use Symfony\Component\Console\Input\InputArgument; |
| 25 | +use Symfony\Component\Console\Input\InputInterface; |
| 26 | +use Symfony\Component\Console\Input\InputOption; |
| 27 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 28 | +use Symfony\Component\Console\Question\Question; |
| 29 | +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Benjamin Georgeault <git@wedgesama.fr> |
| 33 | + */ |
| 34 | +final class MakeDecorator extends AbstractMaker |
| 35 | +{ |
| 36 | + public function __construct( |
| 37 | + private readonly DecoratorHelper $helper, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public static function getCommandName(): string |
| 42 | + { |
| 43 | + return 'make:decorator'; |
| 44 | + } |
| 45 | + |
| 46 | + public static function getCommandDescription(): string |
| 47 | + { |
| 48 | + return 'Create a decorator of a service'; |
| 49 | + } |
| 50 | + |
| 51 | + public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
| 52 | + { |
| 53 | + $command |
| 54 | + ->addArgument('id', InputArgument::OPTIONAL, 'The ID of the service to decorate.') |
| 55 | + ->addArgument('decorator-class', InputArgument::OPTIONAL, \sprintf('The class name of the service to create (e.g. <fg=yellow>%sDecorator</>)', Str::asClassName(Str::getRandomTerm()))) |
| 56 | + ->addOption('priority', null, InputOption::VALUE_REQUIRED, 'The priority of this decoration when multiple decorators are declared for the same service.') |
| 57 | + ->addOption('on-invalid', null, InputOption::VALUE_REQUIRED, 'The behavior to adopt when the decoration is invalid.') |
| 58 | + ->setHelp($this->getHelpFileContents('MakeDecorator.txt')) |
| 59 | + ; |
| 60 | + |
| 61 | + $inputConfig->setArgumentAsNonInteractive('id'); |
| 62 | + $inputConfig->setArgumentAsNonInteractive('decorator-class'); |
| 63 | + } |
| 64 | + |
| 65 | + public function configureDependencies(DependencyBuilder $dependencies): void |
| 66 | + { |
| 67 | + $dependencies->addClassDependency( |
| 68 | + AsDecorator::class, |
| 69 | + 'dependency-injection', |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
| 74 | + { |
| 75 | + // Ask for service id. |
| 76 | + if (null === $input->getArgument('id')) { |
| 77 | + $argument = $command->getDefinition()->getArgument('id'); |
| 78 | + |
| 79 | + ($question = new Question($argument->getDescription())) |
| 80 | + ->setAutocompleterValues($suggestIds = $this->helper->suggestIds()) |
| 81 | + ->setValidator(fn ($answer) => Validator::serviceExists($answer, $suggestIds)) |
| 82 | + ->setMaxAttempts(3); |
| 83 | + |
| 84 | + $input->setArgument('id', $io->askQuestion($question)); |
| 85 | + } |
| 86 | + |
| 87 | + $id = $input->getArgument('id'); |
| 88 | + if (null === $realId = $this->helper->getRealId($id)) { |
| 89 | + $guessCount = \count($guessRealIds = $this->helper->guessRealIds($id)); |
| 90 | + |
| 91 | + if (0 === $guessCount) { |
| 92 | + throw new RuntimeCommandException(\sprintf('Cannot find nor guess service for given id "%s".', $id)); |
| 93 | + } elseif (1 === $guessCount) { |
| 94 | + $question = new ConfirmationQuestion(\sprintf('<fg=green>Did you mean</> <fg=yellow>"%s"</> <fg=green>?</>', $guessRealIds[0]), true); |
| 95 | + |
| 96 | + if (!$io->askQuestion($question)) { |
| 97 | + throw new RuntimeCommandException(\sprintf('Cannot find nor guess service for given id "%s".', $id)); |
| 98 | + } |
| 99 | + |
| 100 | + $input->setArgument('id', $id = $guessRealIds[0]); |
| 101 | + } else { |
| 102 | + $input->setArgument( |
| 103 | + 'id', |
| 104 | + $id = $io->choice(\sprintf('Multiple services found for "%s", choose which one you want to decorate:', $id), $guessRealIds), |
| 105 | + ); |
| 106 | + } |
| 107 | + } else { |
| 108 | + $input->setArgument('id', $id = $realId); |
| 109 | + } |
| 110 | + |
| 111 | + // Ask for decorator classname. |
| 112 | + if (null === $input->getArgument('decorator-class')) { |
| 113 | + $argument = $command->getDefinition()->getArgument('decorator-class'); |
| 114 | + |
| 115 | + $basename = Str::getShortClassName(match (true) { |
| 116 | + interface_exists($id) => Str::removeSuffix($id, 'Interface'), |
| 117 | + class_exists($id) => $id, |
| 118 | + default => Str::asClassName($id), |
| 119 | + }); |
| 120 | + |
| 121 | + $defaultClass = Str::asClassName(\sprintf('%s Decorator', $basename)); |
| 122 | + |
| 123 | + ($question = new Question($argument->getDescription(), $defaultClass)) |
| 124 | + ->setValidator(fn ($answer) => Validator::validateClassName(Validator::classDoesNotExist($answer))) |
| 125 | + ->setMaxAttempts(3); |
| 126 | + |
| 127 | + $input->setArgument('decorator-class', $io->askQuestion($question)); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
| 132 | + { |
| 133 | + $id = $input->getArgument('id'); |
| 134 | + |
| 135 | + $classNameDetails = $generator->createClassNameDetails( |
| 136 | + Validator::validateClassName(Validator::classDoesNotExist($input->getArgument('decorator-class'))), |
| 137 | + '', |
| 138 | + ); |
| 139 | + |
| 140 | + $priority = $input->getOption('priority'); |
| 141 | + $onInvalid = $input->getOption('on-invalid'); |
| 142 | + |
| 143 | + $decoratedInfo = new DecoratorInfo( |
| 144 | + $classNameDetails->getFullName(), |
| 145 | + $id, |
| 146 | + $this->helper->getClass($id), |
| 147 | + empty($priority) ? null : $priority, |
| 148 | + null === $onInvalid || 1 === $onInvalid ? null : $onInvalid, |
| 149 | + ); |
| 150 | + |
| 151 | + $classData = $decoratedInfo->getClassData(); |
| 152 | + |
| 153 | + $generator->generateClassFromClassData( |
| 154 | + $classData, |
| 155 | + 'decorator/Decorator.tpl.php', |
| 156 | + [ |
| 157 | + 'decorated_info' => $decoratedInfo, |
| 158 | + ], |
| 159 | + ); |
| 160 | + |
| 161 | + $generator->writeChanges(); |
| 162 | + |
| 163 | + $this->writeSuccessMessage($io); |
| 164 | + } |
| 165 | +} |
0 commit comments