From 496beedd85c445925dcc2d7dce27778ef5ddf797 Mon Sep 17 00:00:00 2001 From: eltharin Date: Fri, 27 Sep 2024 11:43:10 +0200 Subject: [PATCH] update with review --- src/Maker/Common/CanGenerateTestsTrait.php | 9 +--- src/Maker/Security/MakeFormLogin.php | 59 ++++++++-------------- 2 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/Maker/Common/CanGenerateTestsTrait.php b/src/Maker/Common/CanGenerateTestsTrait.php index 3ea57d014..1b101fb59 100644 --- a/src/Maker/Common/CanGenerateTestsTrait.php +++ b/src/Maker/Common/CanGenerateTestsTrait.php @@ -32,7 +32,7 @@ public function configureCommandWithTestsOption(Command $command): Command $help = $command->getHelp()."\n".$testsHelp; $command - ->addOption(name: 'with-tests', mode: InputOption::VALUE_NONE, description: 'Generate PHPUnit Tests') + ->addOption(name: 'with-tests', mode: InputOption::VALUE_NEGATABLE, description: 'Generate PHPUnit Tests') ->setHelp($help) ; @@ -46,12 +46,7 @@ public function interactSetGenerateTests(InputInterface $input, ConsoleStyle $io throw new RuntimeCommandException('Whoops! "--with-tests" option does not exist. Call "addWithTestsOptions()" in the makers "configureCommand().'); } - $this->generateTests = $input->getOption('with-tests'); - - if (!$this->generateTests) { - $this->generateTests = $io->confirm('Do you want to generate PHPUnit tests? [Experimental]', false); - $input->setOption('with-tests', $this->generateTests); - } + $this->generateTests = $input->getOption('with-tests') ?? $io->confirm('Do you want to generate PHPUnit tests? [Experimental]', false); } public function shouldGenerateTests(): bool diff --git a/src/Maker/Security/MakeFormLogin.php b/src/Maker/Security/MakeFormLogin.php index b46ca70be..d76f64342 100644 --- a/src/Maker/Security/MakeFormLogin.php +++ b/src/Maker/Security/MakeFormLogin.php @@ -59,11 +59,11 @@ final class MakeFormLogin extends AbstractMaker private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml'; private YamlSourceManipulator $ysm; + private string $controllerName; private string $firewallToUpdate; private string $userClass; private string $userNameField; - /** @var ?array */ - private ?array $securityData = null; + private bool $willLogout; public function __construct( private FileManager $fileManager, @@ -80,7 +80,7 @@ public static function getCommandName(): string public function configureCommand(Command $command, InputConfiguration $inputConfig): void { $command->addArgument('controllerName', InputArgument::OPTIONAL, 'The class name of the Controller (e.g. SecurityController)') - ->addOption('will-logout', null, InputOption::VALUE_NONE, 'Will generate a \'/logout\' URL? ') + ->addOption('will-logout', null, InputOption::VALUE_NEGATABLE, 'Will generate a \'/logout\' URL? ') ->setHelp(file_get_contents(\dirname(__DIR__, 2).'/Resources/help/security/MakeFormLogin.txt')); $this->configureCommandWithTestsOption($command); @@ -116,36 +116,30 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma throw new RuntimeCommandException(\sprintf('The file "%s" does not exist. PHP & XML configuration formats are currently not supported.', self::SECURITY_CONFIG_PATH)); } - $securityData = $this->getSecurityData(); + $this->ysm = new YamlSourceManipulator($this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH)); + $securityData = $this->ysm->getData(); if (!isset($securityData['security']['providers']) || !$securityData['security']['providers']) { throw new RuntimeCommandException('To generate a form login authentication, you must configure at least one entry under "providers" in "security.yaml".'); } - if (null === $input->getArgument('controllerName')) { - $input->setArgument( - 'controllerName', $io->ask( - 'Choose a name for the controller class (e.g. SecurityController)', - 'SecurityController', - Validator::validateClassName(...) - )); - } + $this->controllerName = $input->getArgument('controllerName') ?? $io->ask( + 'Choose a name for the controller class (e.g. SecurityController)', + 'SecurityController', + Validator::validateClassName(...) + ); - if (false === $input->getOption('will-logout')) { - $input->setOption('will-logout', $io->confirm('Do you want to generate a \'/logout\' URL?')); - } + $securityHelper = new InteractiveSecurityHelper(); + $this->firewallToUpdate = $securityHelper->guessFirewallName($io, $securityData); + $this->userClass = $securityHelper->guessUserClass($io, $securityData['security']['providers']); + $this->userNameField = $securityHelper->guessUserNameField($io, $this->userClass, $securityData['security']['providers']); + $this->willLogout = $input->getOption('will-logout') ?? $io->confirm('Do you want to generate a \'/logout\' URL?'); $this->interactSetGenerateTests($input, $io); } public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void { - $securityData = $this->getSecurityData(); - $securityHelper = new InteractiveSecurityHelper(); - $this->firewallToUpdate = $securityHelper->guessFirewallName($io, $securityData); - $this->userClass = $securityHelper->guessUserClass($io, $securityData['security']['providers']); - $this->userNameField = $securityHelper->guessUserNameField($io, $this->userClass, $securityData['security']['providers']); - $useStatements = new UseStatementGenerator([ AbstractController::class, Response::class, @@ -153,7 +147,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen AuthenticationUtils::class, ]); - $controllerNameDetails = $generator->createClassNameDetails($input->getArgument('controllerName'), 'Controller\\', 'Controller'); + $controllerNameDetails = $generator->createClassNameDetails($this->controllerName, 'Controller\\', 'Controller'); $templatePath = strtolower($controllerNameDetails->getRelativeNameWithoutSuffix()); $controllerPath = $generator->generateController( @@ -166,7 +160,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen ] ); - if ($input->getOption('will-logout')) { + if ($this->willLogout) { $manipulator = new ClassSourceManipulator($generator->getFileContentsForPendingOperation($controllerPath)); $this->securityControllerBuilder->addLogoutMethod($manipulator); @@ -178,7 +172,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen \sprintf('%s/login.html.twig', $templatePath), 'security/formLogin/login_form.tpl.php', [ - 'logout_setup' => $input->getOption('will-logout'), + 'logout_setup' => $this->willLogout, 'username_label' => Str::asHumanWords($this->userNameField), 'username_is_email' => false !== stripos($this->userNameField, 'email'), ] @@ -186,11 +180,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $securityData = $this->securityConfigUpdater->updateForFormLogin($this->ysm->getContents(), $this->firewallToUpdate, 'app_login', 'app_login'); - if ($input->getOption('will-logout')) { + if ($this->willLogout) { $securityData = $this->securityConfigUpdater->updateForLogout($securityData, $this->firewallToUpdate); } - if ($input->getOption('with-tests')) { + if ($this->shouldGenerateTests()) { $userClassNameDetails = $generator->createClassNameDetails( '\\'.$this->userClass, 'Entity\\' @@ -234,17 +228,4 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen \sprintf('Next: Review and adapt the login template: %s/login.html.twig to suit your needs.', $templatePath), ]); } - - /** - * @return array $items - */ - private function getSecurityData(): array - { - if (null === $this->securityData) { - $this->ysm = new YamlSourceManipulator($this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH)); - $this->securityData = $this->ysm->getData(); - } - - return $this->securityData; - } }