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

Reintroduce App::$page as App::$urlBuildingIndexPage #2096

Merged
merged 21 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/init-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function get_class(\Closure $createAnonymousClassFx): string
throw new Exception('Database error: ' . $e->getMessage());
}

[$rootUrl, $relUrl] = preg_split('~(?<=/)(?=demos(/|\?|$))|\?~s', $_SERVER['REQUEST_URI'], 3);
[$rootUrl, $relUrl] = preg_split('~(?<=/)(?=demos(?:/|$))~s', $app->getRequest()->getUri()->getPath(), 3);
$demosUrl = $rootUrl . 'demos/';

// allow custom layout override
Expand Down
6 changes: 0 additions & 6 deletions docs/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,6 @@ $app->requireCss('https://example.com/file.min.css');

Initializes all includes required by Agile UI. You may extend this class to add more includes.

:::{php:method} getRequestUrl()
:::

Decodes current request without any arguments. If you are changing URL generation pattern, you
probably need to change this method to properly identify the current page. See {php:meth}`App::url()`

## Loading Templates for Views

:::{php:method} loadTemplate($name)
Expand Down
32 changes: 20 additions & 12 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,19 @@ class App
private $portals = [];

/**
* @var string used in method App::url to build the URL
* If filename path part is missing during building of URL, this page will be used.
* Set to empty string when when your webserver supports index.php autoindex or you use mod_rewrite with routing.
*
* Used only in method App::url
* Remove and re-add the extension of the file during parsing requests and building urls
* @internal only for self::url() method
*/
protected $urlBuildingExt = '.php';
protected string $urlBuildingIndexPage = 'index';

/**
* Remove and re-add the extension of the file during parsing requests and building URL.
*
* @internal only for self::url() method
*/
protected string $urlBuildingExt = '.php';

/** @var bool Call exit in place of throw Exception when Application need to exit. */
public $callExit = true;
Expand Down Expand Up @@ -629,11 +636,6 @@ public function loadTemplate(string $filename)
->addMoreInfo('templateDir', $this->templateDir);
}

protected function getRequestUrl(): string
{
return $this->request->getUri()->getPath();
}

protected function createRequestPathFromLocalPath(string $localPath): string
{
// $localPath does not need realpath() as the path is expected to be built using __DIR__
Expand Down Expand Up @@ -691,8 +693,14 @@ public function stickyForget(string $name): void
*/
public function url($page = [], $useRequestUrl = false, $extraRequestUrlArgs = []): string
{
$request = $this->getRequest();

if ($useRequestUrl) {
$page = $_SERVER['REQUEST_URI'];
$page = $request->getUri()->getPath();
$query = $request->getUri()->getQuery();
if ($query !== '') {
$page .= '?' . $query;
}
}

$pagePath = '';
Expand All @@ -705,9 +713,9 @@ public function url($page = [], $useRequestUrl = false, $extraRequestUrlArgs = [
$pagePath = $page[0];
} else {
// use current page by default
$requestUrl = $this->getRequestUrl();
$requestUrl = $request->getUri()->getPath();
if (substr($requestUrl, -1, 1) === '/') {
$pagePath = 'index';
$pagePath = $this->urlBuildingIndexPage;
} else {
$pagePath = basename($requestUrl, $this->urlBuildingExt);
}
Expand Down
9 changes: 6 additions & 3 deletions tests/CreateAppTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

trait CreateAppTrait
{
protected function createApp(): App
/**
* @param array<string, mixed> $defaults
*/
protected function createApp(array $defaults = []): App
{
return new App([
return new App(array_merge([
'catchExceptions' => false,
'alwaysRun' => false,
]);
], $defaults));
}
}
10 changes: 6 additions & 4 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,19 @@ protected function setSuperglobalsFromRequest(RequestInterface $request): void

$rootDirRealpath = realpath(static::ROOT_DIR);

$requestPath = $request->getUri()->getPath();
$requestQuery = $request->getUri()->getQuery();
$_SERVER = [
'REQUEST_METHOD' => $request->getMethod(),
'HTTP_HOST' => $request->getUri()->getHost(),
'REQUEST_URI' => (string) $request->getUri(),
'QUERY_STRING' => $request->getUri()->getQuery(),
'REQUEST_URI' => $requestPath . ($requestQuery !== '' ? '?' . $requestQuery : ''),
'QUERY_STRING' => $requestQuery,
'DOCUMENT_ROOT' => $rootDirRealpath,
'SCRIPT_FILENAME' => $rootDirRealpath . $request->getUri()->getPath(),
'SCRIPT_FILENAME' => $rootDirRealpath . $requestPath,
];

$_GET = [];
parse_str($request->getUri()->getQuery(), $queryArr);
parse_str($requestQuery, $queryArr);
foreach ($queryArr as $k => $v) {
$_GET[$k] = $v;
}
Expand Down