Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify App::url() code, convert string page to array early #2108

Merged
merged 10 commits into from
Sep 15, 2023
48 changes: 24 additions & 24 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,10 @@ protected function createRequestPathFromLocalPath(string $localPath): string
if (\PHP_SAPI === 'cli') { // for phpunit
$requestUrlPath = '/';
$requestLocalPath = \Closure::bind(static function () {
return dirname((new ExceptionRenderer\Html(new \Exception()))->getVendorDirectory());
return (new ExceptionRenderer\Html(new \Exception()))->getVendorDirectory();
}, null, ExceptionRenderer\Html::class)();
} else {
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$request = new \Symfony\Component\HttpFoundation\Request([], [], [], [], [], $_SERVER);
$requestUrlPath = $request->getBasePath();
$requestLocalPath = realpath($request->server->get('SCRIPT_FILENAME'));
}
Expand Down Expand Up @@ -657,37 +657,39 @@ public function stickyForget(string $name): void
*/
public function url($page = [], array $extraRequestUrlArgs = []): string
{
$request = $this->getRequest();

$pagePath = '';
if (is_string($page)) {
$pageExploded = explode('?', $page, 2);
$pagePath = $pageExploded[0];
parse_str($pageExploded[1] ?? '', $page);
$pagePath = $pageExploded[0] !== '' ? $pageExploded[0] : null;
} else {
if (isset($page[0])) {
$pagePath = $page[0];
} else {
// use current page by default
$requestUrl = $request->getUri()->getPath();
if (substr($requestUrl, -1, 1) === '/') {
$pagePath = $this->urlBuildingIndexPage;
} else {
$pagePath = basename($requestUrl, $this->urlBuildingExt);
}
}
$pagePath = $page[0] ?? null;
unset($page[0]);
if ($pagePath) {
$pagePath .= $this->urlBuildingExt;
}

$request = $this->getRequest();

if ($pagePath === null) {
$pagePath = $request->getUri()->getPath();
if ($pagePath === '') { // TODO path must always start with '/'
$pagePath = '/';
}
if (substr($pagePath, -1) === '/') {
$pagePath = $this->urlBuildingIndexPage;
} else {
$pagePath = basename($pagePath, $this->urlBuildingExt);
}
}
if (!str_contains(basename($pagePath), '.')) {
$pagePath .= $this->urlBuildingExt;
}

$args = $extraRequestUrlArgs;

// add sticky arguments
$requestQueryParams = $request->getQueryParams();
foreach ($this->stickyGetArguments as $k => $v) {
if ($v && isset($_GET[$k])) {
$args[$k] = $_GET[$k];
if ($v && isset($requestQueryParams[$k])) {
$args[$k] = $requestQueryParams[$k];
} else {
unset($args[$k]);
}
Expand All @@ -702,11 +704,9 @@ public function url($page = [], array $extraRequestUrlArgs = []): string
}
}

// put URL together
$pageQuery = http_build_query($args, '', '&', \PHP_QUERY_RFC3986);
$url = $pagePath . ($pageQuery ? '?' . $pageQuery : '');

return $url;
return $pagePath . ($pageQuery !== '' ? '?' . $pageQuery : '');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public function testViewUrlCallback(): void
$this->simulateCallbackTriggering($cbApp);
$this->simulateCallbackTriggering($cb);

$expectedUrlCbApp = '?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=callback&' . Callback::URL_QUERY_TARGET . '=aa';
$expectedUrlCb = '?' . /* Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=1&' . */ Callback::URL_QUERY_TRIGGER_PREFIX . 'bb=callback&' . Callback::URL_QUERY_TARGET . '=bb';
$expectedUrlCbApp = 'index.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=callback&' . Callback::URL_QUERY_TARGET . '=aa';
$expectedUrlCb = 'index.php?' . /* Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=1&' . */ Callback::URL_QUERY_TRIGGER_PREFIX . 'bb=callback&' . Callback::URL_QUERY_TARGET . '=bb';
self::assertSame($expectedUrlCbApp, $cbApp->getUrl());
self::assertSame($expectedUrlCb, $cb->getUrl());

Expand Down