Skip to content

Commit

Permalink
Merge pull request #13 from PGSSoft/issue/9-2-add-support-for-interna…
Browse files Browse the repository at this point in the history
…tionalized-routes

Use locale while searching for route
  • Loading branch information
kjonski authored Jun 25, 2020
2 parents 715fc96 + ec8c166 commit 37ee499
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
23 changes: 23 additions & 0 deletions src/Controller/DemoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ public function decodeMore(Request $request, int $id, int $other): Response
return new Response($this->getDecodeResponse($request, $id, $other));
}

/**
* @Hash("id")
*
* @param int $id
*/
public function encodeLocalized($id): Response
{
$url1 = $this->generateUrl('pgs_hash_id_demo_encode_localized', ['id' => $id, '_locale' => 'pl']);
$url2 = $this->generateUrl('pgs_hash_id_demo_encode_localized', ['id' => $id]);

$response = <<<EOT
<html>
<body>
Provided id: $id<br />
Localized url with encoded parameter and locale provided: <a href="$url1">$url1</a><br />
Localized url with encoded parameter: <a href="$url2">$url2</a><br />
</body>
</html>
EOT;

return new Response($response);
}

private function getDecodeResponse(Request $request, int $id, int $other): string
{
$providedId = $this->getRouteParam($request, 'id');
Expand Down
28 changes: 19 additions & 9 deletions src/Decorator/RouterDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@ public function getRouter(): RouterInterface
return $this->object;
}

/**
* @param string $name
* @param array $parameters
* @param int $referenceType
*/
public function generate($name, $parameters = [], $referenceType = RouterInterface::ABSOLUTE_PATH): string
{
$route = $this->getRouter()->getRouteCollection()->get($name);
$this->processParameters($route, $parameters);
public function generate(
$name,
$parameters = [],
$referenceType = RouterInterface::ABSOLUTE_PATH
): string {
$this->processParameters($this->getRoute($name, $parameters), $parameters);

return $this->getRouter()->generate($name, $parameters, $referenceType);
}

private function getRoute(string $name, array $parameters): ?Route
{
$routeCollection = $this->getRouter()->getRouteCollection();
$route = $routeCollection->get($name);

if (null === $route) {
$locale = $parameters['_locale'] ?? $this->getRouter()->getContext()->getParameter('_locale');
$route = $routeCollection->get(sprintf('%s.%s', $name, $locale));
}

return $route;
}

private function processParameters(?Route $route, array &$parameters): void
{
if (null !== $route) {
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ pgs_hash_id_demo_decode:
pgs_hash_id_demo_decode_more:
path: /hash-id/demo/decode_more/{id}/{other}
controller: Pgs\HashIdBundle\Controller\DemoController::decodeMore

pgs_hash_id_demo_encode_localized:
path:
en: /hash-id/demo/encode-en/{id}
pl: /hash-id/demo/encode-pl/{id}
controller: Pgs\HashIdBundle\Controller\DemoController::encodeLocalized
18 changes: 16 additions & 2 deletions tests/Decorator/RouterDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,29 @@ public function testGenerateUrl(): void
$id = 10;
$other = 20;
$alphabet = self::$container->getParameter('pgs_hash_id.converter.hashids.alphabet');
$hashLength = self::$container->getParameter('pgs_hash_id.converter.hashids.min_hash_length');

$routeArgs = ['pgs_hash_id_demo_decode', ['id' => $id, 'other' => $other]];
$generatedPath = $this->router->generate(...$routeArgs);
$this->assertNotSame(sprintf('/hash-id/demo/decode/%d/%d', $id, $other), $generatedPath);
$pattern = sprintf('/\/hash-id\/demo\/decode\/[%s]+\/\d+/', $alphabet);
$pattern = sprintf('/\/hash-id\/demo\/decode\/[%s]{%d}\/\d+/', $alphabet, $hashLength);
$this->assertRegExp($pattern, $generatedPath);

$routeArgs = ['pgs_hash_id_demo_encode_localized', ['id' => $id, '_locale' => 'pl']];
$generatedPath = $this->router->generate(...$routeArgs);
$this->assertNotSame(sprintf('/hash-id/demo/encode-pl/%d', $id), $generatedPath);
$pattern = sprintf('/\/hash-id\/demo\/encode-pl\/[%s]{%d}/', $alphabet, $hashLength);
$this->assertRegExp($pattern, $generatedPath);

$routeArgs = ['pgs_hash_id_demo_decode_more', ['id' => $id, 'other' => $other]];
$generatedPath = $this->router->generate(...$routeArgs);
$pattern = sprintf('/\/hash-id\/demo\/decode_more\/[%s]+\/[%s]+/', $alphabet, $alphabet);
$pattern = sprintf(
'/\/hash-id\/demo\/decode_more\/[%s]{%d}\/[%s]{%d}/',
$alphabet,
$hashLength,
$alphabet,
$hashLength
);
$this->assertRegExp($pattern, $generatedPath);
}
}

0 comments on commit 37ee499

Please sign in to comment.