diff --git a/src/System/Http/RedirectResponse.php b/src/System/Http/RedirectResponse.php index a82572d4..dca28770 100644 --- a/src/System/Http/RedirectResponse.php +++ b/src/System/Http/RedirectResponse.php @@ -12,7 +12,7 @@ public function __construct(string $url, int $response_code = 302, array $header $this->setTarget($url); } - public function setTarget(string $url) + public function setTarget(string $url): void { $this->setContent(sprintf('Redirecting to %1$sRedirecting to %1$s.', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8'))); $this->setHeaders([ diff --git a/src/System/Integrate/helper.php b/src/System/Integrate/helper.php index da8d1ad1..273b35fb 100644 --- a/src/System/Integrate/helper.php +++ b/src/System/Integrate/helper.php @@ -4,7 +4,9 @@ // path aplication +use System\Http\RedirectResponse; use System\Integrate\Exceptions\ApplicationNotAvailable; +use System\Router\Router; if (!function_exists('app_path')) { /** @@ -310,3 +312,36 @@ function vite(...$entry_ponits) return $vite(...$entry_ponits); } } + +if (!function_exists('redirect_route')) { + /** + * Redirect to another route. + * + * @param string[] $parameter Dinamic parameter to fill with url exprestion + */ + function redirect_route(string $route_name, array $parameter = []): RedirectResponse + { + $route = Router::redirect($route_name); + $valueIndex = 0; + $url = preg_replace_callback( + "/\(:\w+\)/", + function ($matches) use ($parameter, &$valueIndex) { + if (!array_key_exists($matches[0], Router::$patterns)) { + throw new \Exception('parameter not matches with any pattern.'); + } + + if ($valueIndex < count($parameter)) { + $value = $parameter[$valueIndex]; + $valueIndex++; + + return $value; + } + + return ''; + }, + $route['uri'] + ); + + return new RedirectResponse($url); + } +} diff --git a/src/System/Router/Router.php b/src/System/Router/Router.php index 1e88043a..1c89f35f 100644 --- a/src/System/Router/Router.php +++ b/src/System/Router/Router.php @@ -268,7 +268,7 @@ public static function redirect(string $to): Route { foreach (self::$routes as $name => $route) { if ($route['name'] === $to) { - return static::$routes[$name]; + return self::$routes[$name]; } } @@ -324,6 +324,7 @@ public static function match($method, string $uri, $callback): Route return self::$routes[] = new Route([ 'method' => $method, + 'uri' => $uri, 'expression' => self::mapPatterns($uri), 'function' => $callback, 'middleware' => $middleware, diff --git a/tests/Integrate/Helper/RedirectResponseTest.php b/tests/Integrate/Helper/RedirectResponseTest.php new file mode 100644 index 00000000..6eee0b22 --- /dev/null +++ b/tests/Integrate/Helper/RedirectResponseTest.php @@ -0,0 +1,55 @@ + $test)->name('test'); + $redirect = redirect_route('test', ['ok']); + $res = new TestResponse($redirect); + $res->assertStatusCode(302); + + Router::reset(); + } + + /** + * @test + */ + public function itRiderectToCorrectUrlWithPlanUrl() + { + Router::get('/test', fn ($test) => $test)->name('test'); + $redirect = redirect_route('test'); + $res = new TestResponse($redirect); + $res->assertStatusCode(302); + + Router::reset(); + } + + /** + * @test + */ + public function itThrowErrorWhenPatternNotExist() + { + Router::get('/test/(:test)', fn ($test) => $test)->name('test'); + $message = ''; + try { + redirect_route('test', ['test']); + } catch (\Throwable $th) { + $message = $th->getMessage(); + } + $this->assertEquals('parameter not matches with any pattern.', $message); + + Router::reset(); + } +}