From 685f0db2dbcfda86cfed2a1e10e15f9fcb79421e Mon Sep 17 00:00:00 2001 From: dos-format-d Date: Mon, 8 Sep 2025 14:07:55 +0200 Subject: [PATCH] FEAT: dynamic parameter templating --- Classes/RedirectService.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Classes/RedirectService.php b/Classes/RedirectService.php index e951a36..e638213 100644 --- a/Classes/RedirectService.php +++ b/Classes/RedirectService.php @@ -80,6 +80,30 @@ public function buildResponseIfApplicable(ServerRequestInterface $httpRequest): } } + /** + * Replaces template variables in the given query string with corresponding values from the provided array. + * + * This method searches for placeholders in the format `${variable}` within the input query string, + * and replaces them with the values from the `$variables` array that match the variable name. + * + * @param string $queryString The query string containing template variables to be replaced. + * @param array $variables An associative array of variable names and their replacement values. + * @return string The query string with all template variables replaced by their corresponding values. + */ + private function templateQueryVariables(string $queryString, array $variables): string + { + $pattern = '/\$\{([^}]+)\}/'; + + $output = preg_replace_callback($pattern, function ($m) use ($variables) { + $key = $m[1]; + if(isset($key) && !empty($key) && isset($variables[$key])) { + return $variables[$key]; + } + }, $queryString); + + return $output; + } + /** * @param ServerRequestInterface $httpRequest * @param RedirectInterface $redirect @@ -111,7 +135,9 @@ protected function buildResponse(ServerRequestInterface $httpRequest, RedirectIn } if (isset($targetUriParts['query'])) { - $absoluteTargetUri = $absoluteTargetUri->withQuery($targetUriParts['query']); + $value = $this->templateQueryVariables($targetUriParts['query'], $httpRequest->getQueryParams()); + + $absoluteTargetUri = $absoluteTargetUri->withQuery($value); } if (isset($targetUriParts['fragment'])) {