From ec8c166e68d87b7c9ff10081133a63f5cb6f1ca1 Mon Sep 17 00:00:00 2001 From: Karol Jonski Date: Tue, 26 May 2020 22:37:16 +0200 Subject: [PATCH] Use locale while searching for route --- src/Controller/DemoController.php | 23 ++++++++++++++++++++ src/Decorator/RouterDecorator.php | 28 +++++++++++++++++-------- src/Resources/routes.yaml | 6 ++++++ tests/Decorator/RouterDecoratorTest.php | 18 ++++++++++++++-- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/Controller/DemoController.php b/src/Controller/DemoController.php index 71fea74..118289a 100644 --- a/src/Controller/DemoController.php +++ b/src/Controller/DemoController.php @@ -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 = << + + Provided id: $id
+ Localized url with encoded parameter and locale provided: $url1
+ Localized url with encoded parameter: $url2
+ + +EOT; + + return new Response($response); + } + private function getDecodeResponse(Request $request, int $id, int $other): string { $providedId = $this->getRouteParam($request, 'id'); diff --git a/src/Decorator/RouterDecorator.php b/src/Decorator/RouterDecorator.php index e8ac2fb..8c55129 100644 --- a/src/Decorator/RouterDecorator.php +++ b/src/Decorator/RouterDecorator.php @@ -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) { diff --git a/src/Resources/routes.yaml b/src/Resources/routes.yaml index cedfc89..f130808 100644 --- a/src/Resources/routes.yaml +++ b/src/Resources/routes.yaml @@ -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 \ No newline at end of file diff --git a/tests/Decorator/RouterDecoratorTest.php b/tests/Decorator/RouterDecoratorTest.php index 514a361..429fd5b 100644 --- a/tests/Decorator/RouterDecoratorTest.php +++ b/tests/Decorator/RouterDecoratorTest.php @@ -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); } }