|
| 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 Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 15 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 16 | +use Symfony\Bundle\MakerBundle\Generator; |
| 17 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 18 | +use Symfony\Bundle\MakerBundle\Str; |
| 19 | +use Symfony\Component\Console\Command\Command; |
| 20 | +use Symfony\Component\Console\Input\InputArgument; |
| 21 | +use Symfony\Component\Console\Input\InputInterface; |
| 22 | +use Symfony\Component\Console\Question\Question; |
| 23 | +use Symfony\WebpackEncoreBundle\WebpackEncoreBundle; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Abdelilah Jabri <[email protected]> |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +final class MakeStimulusController extends AbstractMaker |
| 31 | +{ |
| 32 | + public static function getCommandName(): string |
| 33 | + { |
| 34 | + return 'make:stimulus-controller'; |
| 35 | + } |
| 36 | + |
| 37 | + public static function getCommandDescription(): string |
| 38 | + { |
| 39 | + return 'Creates a new Stimulus controller'; |
| 40 | + } |
| 41 | + |
| 42 | + public function configureCommand(Command $command, InputConfiguration $inputConfig) |
| 43 | + { |
| 44 | + $command |
| 45 | + ->addArgument('name', InputArgument::REQUIRED, 'The name of the Stimulus controller (e.g. <fg=yellow>hello</>)') |
| 46 | + ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeStimulusController.txt')); |
| 47 | + } |
| 48 | + |
| 49 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command) |
| 50 | + { |
| 51 | + $command->addArgument('extension', InputArgument::OPTIONAL); |
| 52 | + $command->addArgument('targets', InputArgument::OPTIONAL, '', []); |
| 53 | + $command->addArgument('values', InputArgument::OPTIONAL, '', []); |
| 54 | + |
| 55 | + $chosenExtension = $io->choice( |
| 56 | + 'Language (<fg=yellow>JavaScript</> or <fg=yellow>TypeScript</>)', |
| 57 | + [ |
| 58 | + 'js' => 'JavaScript', |
| 59 | + 'ts' => 'TypeScript', |
| 60 | + ] |
| 61 | + ); |
| 62 | + |
| 63 | + $input->setArgument('extension', $chosenExtension); |
| 64 | + |
| 65 | + $includeTargets = $io->confirm('Do you want to include targets?'); |
| 66 | + if ($includeTargets) { |
| 67 | + $targets = []; |
| 68 | + $isFirstTarget = true; |
| 69 | + while (true) { |
| 70 | + $newTarget = $this->askForNextTarget($io, $targets, $isFirstTarget); |
| 71 | + $isFirstTarget = false; |
| 72 | + |
| 73 | + if (null === $newTarget) { |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + $targets[] = $newTarget; |
| 78 | + } |
| 79 | + |
| 80 | + $input->setArgument('targets', $targets); |
| 81 | + } |
| 82 | + |
| 83 | + $includeValues = $io->confirm('Do you want to include values?'); |
| 84 | + if ($includeValues) { |
| 85 | + $values = []; |
| 86 | + $isFirstValue = true; |
| 87 | + while (true) { |
| 88 | + $newValue = $this->askForNextValue($io, $values, $isFirstValue); |
| 89 | + $isFirstValue = false; |
| 90 | + |
| 91 | + if (null === $newValue) { |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + $values[$newValue['name']] = $newValue; |
| 96 | + } |
| 97 | + |
| 98 | + $input->setArgument('values', $values); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) |
| 104 | + { |
| 105 | + $controllerName = Str::asSnakeCase($input->getArgument('name')); |
| 106 | + $chosenExtension = $input->getArgument('extension'); |
| 107 | + $targets = $input->getArgument('targets'); |
| 108 | + $values = $input->getArgument('values'); |
| 109 | + |
| 110 | + $targets = empty($targets) ? $targets : sprintf("['%s']", implode("', '", $targets)); |
| 111 | + |
| 112 | + $fileName = sprintf('%s_controller.%s', $controllerName, $chosenExtension); |
| 113 | + $filePath = sprintf('assets/controllers/%s', $fileName); |
| 114 | + |
| 115 | + $generator->generateFile( |
| 116 | + $filePath, |
| 117 | + 'stimulus/Controller.tpl.php', |
| 118 | + [ |
| 119 | + 'targets' => $targets, |
| 120 | + 'values' => $values, |
| 121 | + ] |
| 122 | + ); |
| 123 | + |
| 124 | + $generator->writeChanges(); |
| 125 | + |
| 126 | + $this->writeSuccessMessage($io); |
| 127 | + |
| 128 | + $io->text([ |
| 129 | + 'Next:', |
| 130 | + sprintf('- Open <info>%s</info> and add the code you need', $filePath), |
| 131 | + 'Find the documentation at <fg=yellow>https://github.com/symfony/stimulus-bridge</>', |
| 132 | + ]); |
| 133 | + } |
| 134 | + |
| 135 | + private function askForNextTarget(ConsoleStyle $io, array $targets, bool $isFirstTarget) |
| 136 | + { |
| 137 | + if ($isFirstTarget) { |
| 138 | + $questionText = 'New target name (press <return> to stop adding targets)'; |
| 139 | + } else { |
| 140 | + $questionText = 'Add another target? Enter the target name (or press <return> to stop adding targets)'; |
| 141 | + } |
| 142 | + |
| 143 | + $targetName = $io->ask($questionText, null, function ($name) use ($targets) { |
| 144 | + if (\in_array($name, $targets)) { |
| 145 | + throw new \InvalidArgumentException(sprintf('The "%s" target already exists.', $name)); |
| 146 | + } |
| 147 | + |
| 148 | + return $name; |
| 149 | + }); |
| 150 | + |
| 151 | + if (!$targetName) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + return $targetName; |
| 156 | + } |
| 157 | + |
| 158 | + private function askForNextValue(ConsoleStyle $io, array $values, bool $isFirstValue) |
| 159 | + { |
| 160 | + if ($isFirstValue) { |
| 161 | + $questionText = 'New value name (press <return> to stop adding values)'; |
| 162 | + } else { |
| 163 | + $questionText = 'Add another value? Enter the value name (or press <return> to stop adding values)'; |
| 164 | + } |
| 165 | + |
| 166 | + $valueName = $io->ask($questionText, null, function ($name) use ($values) { |
| 167 | + if (\array_key_exists($name, $values)) { |
| 168 | + throw new \InvalidArgumentException(sprintf('The "%s" value already exists.', $name)); |
| 169 | + } |
| 170 | + |
| 171 | + return $name; |
| 172 | + }); |
| 173 | + |
| 174 | + if (!$valueName) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + $defaultType = 'String'; |
| 179 | + // try to guess the type by the value name prefix/suffix |
| 180 | + // convert to snake case for simplicity |
| 181 | + $snakeCasedField = Str::asSnakeCase($valueName); |
| 182 | + |
| 183 | + if ('_id' === $suffix = substr($snakeCasedField, -3)) { |
| 184 | + $defaultType = 'Number'; |
| 185 | + } elseif (0 === strpos($snakeCasedField, 'is_')) { |
| 186 | + $defaultType = 'Boolean'; |
| 187 | + } elseif (0 === strpos($snakeCasedField, 'has_')) { |
| 188 | + $defaultType = 'Boolean'; |
| 189 | + } |
| 190 | + |
| 191 | + $type = null; |
| 192 | + $types = $this->getValuesTypes(); |
| 193 | + while (null === $type) { |
| 194 | + $question = new Question('Value type (enter <comment>?</comment> to see all types)', $defaultType); |
| 195 | + $question->setAutocompleterValues($types); |
| 196 | + $type = $io->askQuestion($question); |
| 197 | + |
| 198 | + if ('?' === $type) { |
| 199 | + $this->printAvailableTypes($io); |
| 200 | + $io->writeln(''); |
| 201 | + |
| 202 | + $type = null; |
| 203 | + } elseif (!\in_array($type, $types)) { |
| 204 | + $this->printAvailableTypes($io); |
| 205 | + $io->error(sprintf('Invalid type "%s".', $type)); |
| 206 | + $io->writeln(''); |
| 207 | + |
| 208 | + $type = null; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + return ['name' => $valueName, 'type' => $type]; |
| 213 | + } |
| 214 | + |
| 215 | + private function printAvailableTypes(ConsoleStyle $io) |
| 216 | + { |
| 217 | + $allTypes = $this->getValuesTypes(); |
| 218 | + foreach ($allTypes as $type) { |
| 219 | + $io->writeln(sprintf('<info>%s</info>', $type)); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + private function getValuesTypes(): array |
| 224 | + { |
| 225 | + return [ |
| 226 | + 'Array', |
| 227 | + 'Boolean', |
| 228 | + 'Number', |
| 229 | + 'Object', |
| 230 | + 'String', |
| 231 | + ]; |
| 232 | + } |
| 233 | + |
| 234 | + public function configureDependencies(DependencyBuilder $dependencies) |
| 235 | + { |
| 236 | + $dependencies->addClassDependency( |
| 237 | + WebpackEncoreBundle::class, |
| 238 | + 'webpack-encore-bundle' |
| 239 | + ); |
| 240 | + } |
| 241 | +} |
0 commit comments