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 16, 2023
1 parent e20a693 commit 10eae04
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 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
33 changes: 33 additions & 0 deletions tests/system/HTTP/SiteURITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ 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());
$this->assertSame('foo=1&bar=2', $uri->getQuery());
}

public function testConstructorSubfolder()
{
$config = new App();
Expand All @@ -49,6 +70,18 @@ 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());
$this->assertSame('foo=1&bar=2', $uri->getQuery());
}

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

0 comments on commit 10eae04

Please sign in to comment.