-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SecurityBundle][Routing] Add
LogoutRouteLoader
- Loading branch information
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Symfony/Bundle/SecurityBundle/Routing/LogoutRouteLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony 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\SecurityBundle\Routing; | ||
|
||
use Symfony\Component\DependencyInjection\Config\ContainerParametersResource; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
class LogoutRouteLoader | ||
{ | ||
/** | ||
* @param iterable<string, string> $logoutPaths Logout paths indexed by the corresponding firewall name | ||
* @param string $parameterName Name of the container parameter containing {@see $logoutPaths}' value | ||
*/ | ||
public function __construct( | ||
Check failure on line 24 in src/Symfony/Bundle/SecurityBundle/Routing/LogoutRouteLoader.php GitHub Actions / PsalmPossiblyUnusedMethod
Check failure on line 24 in src/Symfony/Bundle/SecurityBundle/Routing/LogoutRouteLoader.php GitHub Actions / PsalmPossiblyUnusedMethod
|
||
private readonly iterable $logoutPaths, | ||
private readonly string $parameterName, | ||
) { | ||
} | ||
|
||
public function __invoke(): RouteCollection | ||
{ | ||
$collection = new RouteCollection(); | ||
$collection->addResource(new ContainerParametersResource([$this->parameterName => $this->logoutPaths])); | ||
|
||
$routeNames = []; | ||
foreach ($this->logoutPaths as $firewallName => $logoutPath) { | ||
$routeName = '_logout_'.$firewallName; | ||
|
||
if (isset($routeNames[$logoutPath])) { | ||
$collection->addAlias($routeNames[$logoutPath], $routeName); | ||
} else { | ||
$routeNames[$logoutPath] = $routeName; | ||
} | ||
|
||
$collection->add($routeName, new Route($logoutPath)); | ||
} | ||
|
||
return $collection; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Symfony/Bundle/SecurityBundle/Tests/Routing/LogoutRouteLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony 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\SecurityBundle\Tests\Routing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\SecurityBundle\Routing\LogoutRouteLoader; | ||
use Symfony\Component\DependencyInjection\Config\ContainerParametersResource; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
class LogoutRouteLoaderTest extends TestCase | ||
{ | ||
public function testLoad() | ||
{ | ||
$logoutPaths = [ | ||
'main' => '/logout', | ||
'admin' => '/logout', | ||
]; | ||
|
||
$loader = new LogoutRouteLoader($logoutPaths, 'parameterName'); | ||
$collection = $loader(); | ||
|
||
self::assertInstanceOf(RouteCollection::class, $collection); | ||
self::assertCount(1, $collection); | ||
self::assertEquals(new Route('/logout'), $collection->get('_logout_main')); | ||
self::assertCount(1, $collection->getAliases()); | ||
self::assertEquals('_logout_admin', $collection->getAlias('_logout_main')->getId()); | ||
|
||
$resources = $collection->getResources(); | ||
self::assertCount(1, $resources); | ||
|
||
$resource = reset($resources); | ||
self::assertInstanceOf(ContainerParametersResource::class, $resource); | ||
self::assertSame(['parameterName' => $logoutPaths], $resource->getParameters()); | ||
} | ||
} |