Skip to content

Commit

Permalink
update with review
Browse files Browse the repository at this point in the history
  • Loading branch information
eltharin committed Sep 27, 2024
1 parent 46c1f55 commit 496beed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 46 deletions.
9 changes: 2 additions & 7 deletions src/Maker/Common/CanGenerateTestsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;

Expand All @@ -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
Expand Down
59 changes: 20 additions & 39 deletions src/Maker/Security/MakeFormLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> */
private ?array $securityData = null;
private bool $willLogout;

public function __construct(
private FileManager $fileManager,
Expand All @@ -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. <fg=yellow>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);
Expand Down Expand Up @@ -116,44 +116,38 @@ 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. <fg=yellow>SecurityController</>)',
'SecurityController',
Validator::validateClassName(...)
));
}
$this->controllerName = $input->getArgument('controllerName') ?? $io->ask(
'Choose a name for the controller class (e.g. <fg=yellow>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,
Route::class,
AuthenticationUtils::class,
]);

$controllerNameDetails = $generator->createClassNameDetails($input->getArgument('controllerName'), 'Controller\\', 'Controller');
$controllerNameDetails = $generator->createClassNameDetails($this->controllerName, 'Controller\\', 'Controller');
$templatePath = strtolower($controllerNameDetails->getRelativeNameWithoutSuffix());

$controllerPath = $generator->generateController(
Expand All @@ -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);
Expand All @@ -178,19 +172,19 @@ 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'),
]
);

$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\\'
Expand Down Expand Up @@ -234,17 +228,4 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
\sprintf('Next: Review and adapt the login template: <info>%s/login.html.twig</info> to suit your needs.', $templatePath),
]);
}

/**
* @return array<string, mixed> $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;
}
}

0 comments on commit 496beed

Please sign in to comment.