Skip to content

[auth] Inject UrlGeneratorInterface instead of RouterInterface #349

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

Merged
merged 2 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('maker');
if (\method_exists($treeBuilder, 'getRootNode')) {
if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
Expand Down
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function generateController(string $controllerClassName, string $controll
$controllerTemplatePath,
$parameters +
[
'parent_class_name' => \method_exists(AbstractController::class, 'getParameter') ? 'AbstractController' : 'Controller',
'parent_class_name' => method_exists(AbstractController::class, 'getParameter') ? 'AbstractController' : 'Controller',
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<?= $user_is_entity ? "use Doctrine\\ORM\\EntityManagerInterface;\n" : null ?>
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
<?= $user_needs_encoder ? "use Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface;\n" : null ?>
Expand All @@ -24,14 +24,14 @@ class <?= $class_name; ?> extends AbstractFormLoginAuthenticator
use TargetPathTrait;

<?= $user_is_entity ? " private \$entityManager;\n" : null ?>
private $router;
private $urlGenerator;
private $csrfTokenManager;
<?= $user_needs_encoder ? " private \$passwordEncoder;\n" : null ?>

public function __construct(<?= $user_is_entity ? 'EntityManagerInterface $entityManager, ' : null ?>RouterInterface $router, CsrfTokenManagerInterface $csrfTokenManager<?= $user_needs_encoder ? ', UserPasswordEncoderInterface $passwordEncoder' : null ?>)
public function __construct(<?= $user_is_entity ? 'EntityManagerInterface $entityManager, ' : null ?>UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager<?= $user_needs_encoder ? ', UserPasswordEncoderInterface $passwordEncoder' : null ?>)
{
<?= $user_is_entity ? " \$this->entityManager = \$entityManager;\n" : null ?>
$this->router = $router;
$this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager;
<?= $user_needs_encoder ? " \$this->passwordEncoder = \$passwordEncoder;\n" : null ?>
}
Expand Down Expand Up @@ -91,12 +91,12 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
return new RedirectResponse($targetPath);
}

// For example : return new RedirectResponse($this->router->generate('some_route'));
// For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
}

protected function getLoginUrl()
{
return $this->router->generate('app_login');
return $this->urlGenerator->generate('app_login');
}
}
2 changes: 1 addition & 1 deletion src/Util/AutoloaderUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(ComposerAutoloaderFinder $autoloaderFinder)
*
* @param string $className
*
* @return null|string
* @return string|null
*
* @throws \Exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testCommand()
{
$authenticatorReflection = new \ReflectionClass(AppCustomAuthenticator::class);
$constructorParameters = $authenticatorReflection->getConstructor()->getParameters();
$this->assertSame('router', $constructorParameters[0]->getName());
$this->assertSame('urlGenerator', $constructorParameters[0]->getName());

// assert authenticator is injected
$this->assertEquals(3, \count($constructorParameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testCommand()
{
$authenticatorReflection = new \ReflectionClass(AppCustomAuthenticator::class);
$constructorParameters = $authenticatorReflection->getConstructor()->getParameters();
$this->assertSame('router', $constructorParameters[0]->getName());
$this->assertSame('urlGenerator', $constructorParameters[0]->getName());

// assert authenticator is injected
$this->assertEquals(3, \count($constructorParameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testCommand()
{
$authenticatorReflection = new \ReflectionClass(AppCustomAuthenticator::class);
$constructorParameters = $authenticatorReflection->getConstructor()->getParameters();
$this->assertSame('router', $constructorParameters[0]->getName());
$this->assertSame('urlGenerator', $constructorParameters[0]->getName());

// assert authenticator is *not* injected
$this->assertEquals(2, \count($constructorParameters));
Expand Down