-
-
Notifications
You must be signed in to change notification settings - Fork 424
[make:stimulus-controller] New Stimulus controller Maker command #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\ConsoleStyle; | ||
use Symfony\Bundle\MakerBundle\DependencyBuilder; | ||
use Symfony\Bundle\MakerBundle\Generator; | ||
use Symfony\Bundle\MakerBundle\InputConfiguration; | ||
use Symfony\Bundle\MakerBundle\Str; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Question\Question; | ||
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle; | ||
|
||
/** | ||
* @author Abdelilah Jabri <jbrabdelilah@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class MakeStimulusController extends AbstractMaker | ||
{ | ||
public static function getCommandName(): string | ||
{ | ||
return 'make:stimulus-controller'; | ||
} | ||
|
||
public static function getCommandDescription(): string | ||
{ | ||
return 'Creates a new Stimulus controller'; | ||
} | ||
|
||
public function configureCommand(Command $command, InputConfiguration $inputConfig): void | ||
{ | ||
$command | ||
->addArgument('name', InputArgument::REQUIRED, 'The name of the Stimulus controller (e.g. <fg=yellow>hello</>)') | ||
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeStimulusController.txt')); | ||
} | ||
|
||
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void | ||
{ | ||
$command->addArgument('extension', InputArgument::OPTIONAL); | ||
$command->addArgument('targets', InputArgument::OPTIONAL, '', []); | ||
$command->addArgument('values', InputArgument::OPTIONAL, '', []); | ||
|
||
$chosenExtension = $io->choice( | ||
'Language (<fg=yellow>JavaScript</> or <fg=yellow>TypeScript</>)', | ||
[ | ||
'js' => 'JavaScript', | ||
'ts' => 'TypeScript', | ||
] | ||
); | ||
|
||
$input->setArgument('extension', $chosenExtension); | ||
|
||
if ($io->confirm('Do you want to include targets?')) { | ||
$targets = []; | ||
$isFirstTarget = true; | ||
|
||
while (true) { | ||
$newTarget = $this->askForNextTarget($io, $targets, $isFirstTarget); | ||
$isFirstTarget = false; | ||
|
||
if (null === $newTarget) { | ||
break; | ||
} | ||
|
||
$targets[] = $newTarget; | ||
} | ||
|
||
$input->setArgument('targets', $targets); | ||
} | ||
|
||
if ($io->confirm('Do you want to include values?')) { | ||
$values = []; | ||
$isFirstValue = true; | ||
while (true) { | ||
$newValue = $this->askForNextValue($io, $values, $isFirstValue); | ||
$isFirstValue = false; | ||
|
||
if (null === $newValue) { | ||
break; | ||
} | ||
|
||
$values[$newValue['name']] = $newValue; | ||
} | ||
|
||
$input->setArgument('values', $values); | ||
} | ||
} | ||
|
||
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void | ||
{ | ||
$controllerName = Str::asSnakeCase($input->getArgument('name')); | ||
$chosenExtension = $input->getArgument('extension'); | ||
$targets = $input->getArgument('targets'); | ||
$values = $input->getArgument('values'); | ||
|
||
$targets = empty($targets) ? $targets : sprintf("['%s']", implode("', '", $targets)); | ||
|
||
$fileName = sprintf('%s_controller.%s', $controllerName, $chosenExtension); | ||
$filePath = sprintf('assets/controllers/%s', $fileName); | ||
|
||
$generator->generateFile( | ||
$filePath, | ||
'stimulus/Controller.tpl.php', | ||
[ | ||
'targets' => $targets, | ||
'values' => $values, | ||
] | ||
); | ||
|
||
$generator->writeChanges(); | ||
|
||
$this->writeSuccessMessage($io); | ||
|
||
$io->text([ | ||
'Next:', | ||
sprintf('- Open <info>%s</info> and add the code you need', $filePath), | ||
'Find the documentation at <fg=yellow>https://github.com/symfony/stimulus-bridge</>', | ||
]); | ||
} | ||
|
||
private function askForNextTarget(ConsoleStyle $io, array $targets, bool $isFirstTarget): ?string | ||
{ | ||
$questionText = 'New target name (press <return> to stop adding targets)'; | ||
|
||
if (!$isFirstTarget) { | ||
$questionText = 'Add another target? Enter the target name (or press <return> to stop adding targets)'; | ||
} | ||
|
||
$targetName = $io->ask($questionText, null, function (?string $name) use ($targets) { | ||
if (\in_array($name, $targets)) { | ||
throw new \InvalidArgumentException(sprintf('The "%s" target already exists.', $name)); | ||
} | ||
|
||
return $name; | ||
}); | ||
|
||
return !$targetName ? null : $targetName; | ||
} | ||
|
||
private function askForNextValue(ConsoleStyle $io, array $values, bool $isFirstValue): ?array | ||
{ | ||
$questionText = 'New value name (press <return> to stop adding values)'; | ||
|
||
if (!$isFirstValue) { | ||
$questionText = 'Add another value? Enter the value name (or press <return> to stop adding values)'; | ||
} | ||
|
||
$valueName = $io->ask($questionText, null, function ($name) use ($values) { | ||
if (\array_key_exists($name, $values)) { | ||
throw new \InvalidArgumentException(sprintf('The "%s" value already exists.', $name)); | ||
} | ||
|
||
return $name; | ||
}); | ||
|
||
if (!$valueName) { | ||
return null; | ||
} | ||
|
||
$defaultType = 'String'; | ||
// try to guess the type by the value name prefix/suffix | ||
// convert to snake case for simplicity | ||
$snakeCasedField = Str::asSnakeCase($valueName); | ||
|
||
if ('_id' === $suffix = substr($snakeCasedField, -3)) { | ||
$defaultType = 'Number'; | ||
} elseif (0 === strpos($snakeCasedField, 'is_')) { | ||
$defaultType = 'Boolean'; | ||
} elseif (0 === strpos($snakeCasedField, 'has_')) { | ||
$defaultType = 'Boolean'; | ||
} | ||
|
||
$type = null; | ||
$types = $this->getValuesTypes(); | ||
|
||
while (null === $type) { | ||
$question = new Question('Value type (enter <comment>?</comment> to see all types)', $defaultType); | ||
$question->setAutocompleterValues($types); | ||
$type = $io->askQuestion($question); | ||
|
||
if ('?' === $type) { | ||
$this->printAvailableTypes($io); | ||
$io->writeln(''); | ||
|
||
$type = null; | ||
} elseif (!\in_array($type, $types)) { | ||
$this->printAvailableTypes($io); | ||
$io->error(sprintf('Invalid type "%s".', $type)); | ||
$io->writeln(''); | ||
|
||
$type = null; | ||
} | ||
} | ||
|
||
return ['name' => $valueName, 'type' => $type]; | ||
} | ||
|
||
private function printAvailableTypes(ConsoleStyle $io): void | ||
{ | ||
foreach ($this->getValuesTypes() as $type) { | ||
$io->writeln(sprintf('<info>%s</info>', $type)); | ||
} | ||
} | ||
|
||
private function getValuesTypes(): array | ||
{ | ||
return [ | ||
'Array', | ||
'Boolean', | ||
'Number', | ||
'Object', | ||
'String', | ||
]; | ||
} | ||
|
||
public function configureDependencies(DependencyBuilder $dependencies): void | ||
{ | ||
$dependencies->addClassDependency( | ||
WebpackEncoreBundle::class, | ||
'webpack-encore-bundle' | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The <info>%command.name%</info> command generates new Stimulus Controller. | ||
|
||
<info>php %command.full_name% hello</info> | ||
|
||
If the argument is missing, the command will ask for the controller name interactively. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The extension also isnt it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I forget to update this file, thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @94noni in fact, the extension is interactive, no need to add it in here |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* | ||
* The following line makes this controller "lazy": it won't be downloaded until needed | ||
* See https://github.com/symfony/stimulus-bridge#lazy-controllers | ||
*/ | ||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
JabriAbdelilah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<?= $targets ? " static targets = $targets\n" : "" ?> | ||
<?php if ($values) { ?> | ||
static values = { | ||
<?php foreach ($values as $value): ?> | ||
<?= $value['name'] ?>: <?= $value['type'] ?>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just I think the last There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a trailing comma, it's a good practice in Javascript |
||
<?php endforeach; ?> | ||
} | ||
<?php } ?> | ||
// ... | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Tests\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\Maker\MakeStimulusController; | ||
use Symfony\Bundle\MakerBundle\Test\MakerTestCase; | ||
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner; | ||
|
||
class MakeStimulusControllerTest extends MakerTestCase | ||
{ | ||
protected function getMakerClass(): string | ||
{ | ||
return MakeStimulusController::class; | ||
} | ||
|
||
public function getTestDetails(): \Generator | ||
{ | ||
yield 'it_generates_stimulus_controller_with_targets' => [$this->createMakerTest() | ||
->run(function (MakerTestRunner $runner) { | ||
$runner->runMaker( | ||
[ | ||
'with_targets', // controller name | ||
'js', // controller language | ||
'yes', // add targets | ||
'results', // first target | ||
'messages', // second target | ||
'errors', // third target | ||
'', // empty input to stop adding targets | ||
]); | ||
|
||
$generatedFilePath = $runner->getPath('assets/controllers/with_targets_controller.js'); | ||
|
||
$this->assertFileExists($generatedFilePath); | ||
|
||
$generatedFileContents = file_get_contents($generatedFilePath); | ||
$expectedContents = file_get_contents(__DIR__.'/../fixtures/make-stimulus-controller/with_targets.js'); | ||
|
||
$this->assertSame( | ||
$expectedContents, | ||
$generatedFileContents | ||
); | ||
}), | ||
]; | ||
|
||
yield 'it_generates_stimulus_controller_without_targets' => [$this->createMakerTest() | ||
->run(function (MakerTestRunner $runner) { | ||
$runner->runMaker( | ||
[ | ||
'without_targets', // controller name | ||
'js', // controller language | ||
'no', // do not add targets | ||
]); | ||
|
||
$generatedFilePath = $runner->getPath('assets/controllers/without_targets_controller.js'); | ||
|
||
$this->assertFileExists($generatedFilePath); | ||
|
||
$generatedFileContents = file_get_contents($generatedFilePath); | ||
$expectedContents = file_get_contents(__DIR__.'/../fixtures/make-stimulus-controller/without_targets.js'); | ||
|
||
$this->assertSame( | ||
$expectedContents, | ||
$generatedFileContents | ||
); | ||
}), | ||
]; | ||
|
||
yield 'it_generates_typescript_stimulus_controller' => [$this->createMakerTest() | ||
->run(function (MakerTestRunner $runner) { | ||
$runner->runMaker( | ||
[ | ||
'typescript', // controller name | ||
'ts', // controller language | ||
'no', // do not add targets | ||
]); | ||
|
||
$this->assertFileExists($runner->getPath('assets/controllers/typescript_controller.ts')); | ||
}), | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* | ||
* The following line makes this controller "lazy": it won't be downloaded until needed | ||
* See https://github.com/symfony/stimulus-bridge#lazy-controllers | ||
*/ | ||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
static targets = ['results', 'messages', 'errors'] | ||
// ... | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* | ||
* The following line makes this controller "lazy": it won't be downloaded until needed | ||
* See https://github.com/symfony/stimulus-bridge#lazy-controllers | ||
*/ | ||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
// ... | ||
} |
Uh oh!
There was an error while loading. Please reload this page.