Skip to content

Commit

Permalink
auto gen use statements for security controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Apr 23, 2022
1 parent cc21c44 commit 5694cc6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Bundle\MakerBundle;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
Expand Down Expand Up @@ -229,7 +228,6 @@ public function generateController(string $controllerClassName, string $controll
$parameters +
[
'generator' => $this->templateComponentGenerator,
'parent_class_name' => static::getControllerBaseClass()->getShortName(),
]
);
}
Expand All @@ -246,8 +244,14 @@ public function generateTemplate(string $targetPath, string $templateName, array
);
}

/**
* @deprecated
* @legacy
*/
public static function getControllerBaseClass(): ClassNameDetails
{
trigger_deprecation('symfony/maker-bundle', 'v1.41.0', 'MakerBundle only supports AbstractController::class');

return new ClassNameDetails(AbstractController::class, '\\');
}
}
12 changes: 11 additions & 1 deletion src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\MakerBundle\Maker;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
Expand Down Expand Up @@ -38,10 +39,12 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
Expand Down Expand Up @@ -320,9 +323,16 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
);

if (!class_exists($controllerClassNameDetails->getFullName())) {
$useStatements = [
AbstractController::class,
Route::class,
AuthenticationUtils::class,
];

$controllerPath = $this->generator->generateController(
$controllerClassNameDetails->getFullName(),
'authenticator/EmptySecurityController.tpl.php'
'authenticator/EmptySecurityController.tpl.php',
['use_statements' => TemplateComponentGenerator::generateUseStatements($useStatements)]
);

$controllerSourceCode = $this->generator->getFileContentsForPendingOperation($controllerPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace <?= $namespace ?>;

use Symfony\Bundle\FrameworkBundle\Controller\<?= $parent_class_name; ?>;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
<?= $use_statements; ?>

class <?= $class_name; ?> extends <?= $parent_class_name; ?><?= "\n" ?>
class <?= $class_name; ?> extends AbstractController
{
}

0 comments on commit 5694cc6

Please sign in to comment.