Skip to content

Commit

Permalink
feat: add param $relativePath to constructor
Browse files Browse the repository at this point in the history
Change the condition to add / after index.php.
  • Loading branch information
kenjis committed Feb 15, 2023
1 parent 5af1578 commit 77d43a8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
62 changes: 44 additions & 18 deletions system/HTTP/SiteURI.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,31 @@ class SiteURI extends URI
*/
private string $routePath;

public function __construct(App $configApp)
/**
* @param string $relativePath URI path relative to baseURL. May include
* queries or fragments.
*/
public function __construct(App $configApp, string $relativePath = '')
{
// It's possible the user forgot a trailing slash on their
// baseURL, so let's help them out.
$baseURL = rtrim($configApp->baseURL, '/ ') . '/';

// Validate baseURL
if (filter_var($baseURL, FILTER_VALIDATE_URL) === false) {
throw new ConfigException(
'Config\App::$baseURL is invalid.'
);
}

$this->baseURL = $baseURL;
$this->baseURL = $this->normalizeBaseURL($configApp);
$this->indexPage = $configApp->indexPage;

$this->setBaseSegments();

// Check for an index page
$indexPage = '';
if ($configApp->indexPage !== '') {
$indexPage = $configApp->indexPage . '/';
$indexPage = $configApp->indexPage;

// Check if we need a separator
if ($relativePath !== '' && $relativePath[0] !== '/' && $relativePath[0] !== '?') {
$indexPage .= '/';
}
}

$tempUri = $this->baseURL . $indexPage;
$relativePath = URI::removeDotSegments($relativePath);

$tempUri = $this->baseURL . $indexPage . $relativePath;
$uri = new URI($tempUri);

if ($configApp->forceGlobalSecureRequests) {
Expand All @@ -107,7 +107,25 @@ public function __construct(App $configApp)
}
$this->applyParts($parts);

$this->setPath('/');
$parts = explode('?', $relativePath);
$routePath = $parts[0];
$this->setRoutePath($routePath);
}

private function normalizeBaseURL(App $configApp): string
{
// It's possible the user forgot a trailing slash on their
// baseURL, so let's help them out.
$baseURL = rtrim($configApp->baseURL, '/ ') . '/';

// Validate baseURL
if (filter_var($baseURL, FILTER_VALIDATE_URL) === false) {
throw new ConfigException(
'Config\App::$baseURL is invalid.'
);
}

return $baseURL;
}

/**
Expand Down Expand Up @@ -247,14 +265,22 @@ public function __toString(): string
* @return $this
*/
public function setPath(string $path)
{
$this->setRoutePath($path);

return $this;
}

/**
* Sets the route path (and segments).
*/
private function setRoutePath(string $path): void
{
$this->routePath = $this->filterPath($path);

$this->segments = $this->convertToSegments($this->routePath);

$this->refreshPath();

return $this;
}

/**
Expand Down
11 changes: 5 additions & 6 deletions system/HTTP/SiteURIFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,17 @@ private function parseQueryString(): string
*/
private function createURIFromRoutePath(string $routePath): SiteURI
{
$uri = new SiteURI($this->appConfig);
$query = $this->server['QUERY_STRING'] ?? '';

$relativePath = $query !== '' ? $routePath . '?' . $query : $routePath;

$uri = new SiteURI($this->appConfig, $relativePath);

// Based on our baseURL and allowedHostnames provided by the developer
// and HTTP_HOST, set our current hostname.
$host = $this->determineHost($uri->getBaseURL());
$uri->setHost($host);

$uri->setPath($routePath);

// Ensure we have any query vars
$uri->setQuery($this->server['QUERY_STRING'] ?? '');

return $uri;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/system/HTTP/SiteURITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ public function testConstructor()
$this->assertSame('/index.php/', $uri->getPath());
}

public function testConstructorRelativePath()
{
$config = new App();

$uri = new SiteURI($config, 'one/two');

$this->assertSame('http://example.com/index.php/one/two', (string) $uri);
$this->assertSame('/index.php/one/two', $uri->getPath());
}

public function testConstructorRelativePathWithQuery()
{
$config = new App();

$uri = new SiteURI($config, 'one/two?foo=1&bar=2');

$this->assertSame('http://example.com/index.php/one/two?foo=1&bar=2', (string) $uri);
$this->assertSame('/index.php/one/two', $uri->getPath());
}

public function testConstructorSubfolder()
{
$config = new App();
Expand All @@ -49,6 +69,17 @@ public function testConstructorSubfolder()
$this->assertSame('/ci4/index.php/', $uri->getPath());
}

public function testConstructorSubfolderRelativePathWithQuery()
{
$config = new App();
$config->baseURL = 'http://example.com/ci4/';

$uri = new SiteURI($config, 'one/two?foo=1&bar=2');

$this->assertSame('http://example.com/ci4/index.php/one/two?foo=1&bar=2', (string) $uri);
$this->assertSame('/ci4/index.php/one/two', $uri->getPath());
}

public function testConstructorForceGlobalSecureRequests()
{
$config = new App();
Expand Down

0 comments on commit 77d43a8

Please sign in to comment.