-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add SiteURIFactory #7256
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
Merged
Merged
feat: add SiteURIFactory #7256
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6318c23
feat: add SiteURIFactory
kenjis 09a96ab
fix: createFromString() returns URI with invalid hostname
kenjis 37772e9
docs: add version to @deprecated
kenjis 9937a03
refactor: extract class Superglobals and use it
kenjis 8522e99
test: extract createSiteURIFactory()
kenjis be7ca15
refactor: change parameter order
kenjis 1a95c89
feat: allow Superglobals state changes from the constructor
kenjis 78b0ac6
refactore: remove `@` that does not make sense, and cast to string
kenjis fde3216
refactor: remove string array access
kenjis 6b3dd7a
refactor: make SiteURIFactory final
kenjis 84b4a31
feat: add Superglobals::setGet()
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\HTTP; | ||
|
|
||
| use CodeIgniter\HTTP\Exceptions\HTTPException; | ||
| use CodeIgniter\Superglobals; | ||
| use Config\App; | ||
|
|
||
| final class SiteURIFactory | ||
| { | ||
| private App $appConfig; | ||
| private Superglobals $superglobals; | ||
|
|
||
| public function __construct(App $appConfig, Superglobals $superglobals) | ||
| { | ||
| $this->appConfig = $appConfig; | ||
| $this->superglobals = $superglobals; | ||
| } | ||
|
|
||
| /** | ||
| * Create the current URI object from superglobals. | ||
| * | ||
| * This method updates superglobal $_SERVER and $_GET. | ||
| */ | ||
| public function createFromGlobals(): SiteURI | ||
| { | ||
| $routePath = $this->detectRoutePath(); | ||
|
|
||
| return $this->createURIFromRoutePath($routePath); | ||
| } | ||
|
|
||
| /** | ||
| * Create the SiteURI object from URI string. | ||
| * | ||
| * @internal Used for testing purposes only. | ||
| */ | ||
| public function createFromString(string $uri): SiteURI | ||
| { | ||
| // Validate URI | ||
| if (filter_var($uri, FILTER_VALIDATE_URL) === false) { | ||
| throw HTTPException::forUnableToParseURI($uri); | ||
| } | ||
|
|
||
| $parts = parse_url($uri); | ||
|
|
||
| if ($parts === false) { | ||
| throw HTTPException::forUnableToParseURI($uri); | ||
| } | ||
|
|
||
| $query = $fragment = ''; | ||
| if (isset($parts['query'])) { | ||
| $query = '?' . $parts['query']; | ||
| } | ||
| if (isset($parts['fragment'])) { | ||
| $fragment = '#' . $parts['fragment']; | ||
| } | ||
|
|
||
| $relativePath = $parts['path'] . $query . $fragment; | ||
| $host = $this->getValidHost($parts['host']); | ||
|
|
||
| return new SiteURI($this->appConfig, $relativePath, $host, $parts['scheme']); | ||
| } | ||
|
|
||
| /** | ||
| * Detects the current URI path relative to baseURL based on the URIProtocol | ||
| * Config setting. | ||
| * | ||
| * @param string $protocol URIProtocol | ||
| * | ||
| * @return string The route path | ||
| * | ||
| * @internal Used for testing purposes only. | ||
| */ | ||
| public function detectRoutePath(string $protocol = ''): string | ||
| { | ||
| if ($protocol === '') { | ||
| $protocol = $this->appConfig->uriProtocol; | ||
| } | ||
|
|
||
| switch ($protocol) { | ||
| case 'REQUEST_URI': | ||
| $routePath = $this->parseRequestURI(); | ||
| break; | ||
|
|
||
| case 'QUERY_STRING': | ||
| $routePath = $this->parseQueryString(); | ||
| break; | ||
|
|
||
| case 'PATH_INFO': | ||
| default: | ||
| $routePath = $this->superglobals->server($protocol) ?? $this->parseRequestURI(); | ||
| break; | ||
| } | ||
|
|
||
| return ($routePath === '/' || $routePath === '') ? '/' : ltrim($routePath, '/'); | ||
| } | ||
|
|
||
| /** | ||
| * Will parse the REQUEST_URI and automatically detect the URI from it, | ||
| * fixing the query string if necessary. | ||
| * | ||
| * This method updates superglobal $_SERVER and $_GET. | ||
| * | ||
| * @return string The route path (before normalization). | ||
| */ | ||
| private function parseRequestURI(): string | ||
| { | ||
| if ( | ||
| $this->superglobals->server('REQUEST_URI') === null | ||
| || $this->superglobals->server('SCRIPT_NAME') === null | ||
| ) { | ||
| return ''; | ||
| } | ||
|
|
||
| // parse_url() returns false if no host is present, but the path or query | ||
| // string contains a colon followed by a number. So we attach a dummy | ||
| // host since REQUEST_URI does not include the host. This allows us to | ||
| // parse out the query string and path. | ||
| $parts = parse_url('http://dummy' . $this->superglobals->server('REQUEST_URI')); | ||
| $query = $parts['query'] ?? ''; | ||
| $path = $parts['path'] ?? ''; | ||
|
|
||
| // Strip the SCRIPT_NAME path from the URI | ||
| if ( | ||
| $path !== '' && $this->superglobals->server('SCRIPT_NAME') !== '' | ||
| && pathinfo($this->superglobals->server('SCRIPT_NAME'), PATHINFO_EXTENSION) === 'php' | ||
| ) { | ||
| // Compare each segment, dropping them until there is no match | ||
| $segments = $keep = explode('/', $path); | ||
|
|
||
| foreach (explode('/', $this->superglobals->server('SCRIPT_NAME')) as $i => $segment) { | ||
| // If these segments are not the same then we're done | ||
| if (! isset($segments[$i]) || $segment !== $segments[$i]) { | ||
| break; | ||
| } | ||
|
|
||
| array_shift($keep); | ||
| } | ||
|
|
||
| $path = implode('/', $keep); | ||
| } | ||
|
|
||
| // This section ensures that even on servers that require the URI to | ||
| // contain the query string (Nginx) a correct URI is found, and also | ||
| // fixes the QUERY_STRING Server var and $_GET array. | ||
| if (trim($path, '/') === '' && strncmp($query, '/', 1) === 0) { | ||
| $parts = explode('?', $query, 2); | ||
| $path = $parts[0]; | ||
| $newQuery = $query[1] ?? ''; | ||
|
|
||
| $this->superglobals->setServer('QUERY_STRING', $newQuery); | ||
| } else { | ||
| $this->superglobals->setServer('QUERY_STRING', $query); | ||
| } | ||
|
|
||
| // Update our global GET for values likely to have been changed | ||
| parse_str($this->superglobals->server('QUERY_STRING'), $get); | ||
| $this->superglobals->setGetArray($get); | ||
|
|
||
| return URI::removeDotSegments($path); | ||
| } | ||
|
|
||
| /** | ||
| * Will parse QUERY_STRING and automatically detect the URI from it. | ||
| * | ||
| * This method updates superglobal $_SERVER and $_GET. | ||
| * | ||
| * @return string The route path (before normalization). | ||
| */ | ||
| private function parseQueryString(): string | ||
| { | ||
| $query = $this->superglobals->server('QUERY_STRING') ?? (string) getenv('QUERY_STRING'); | ||
|
|
||
| if (trim($query, '/') === '') { | ||
| return '/'; | ||
| } | ||
|
|
||
| if (strncmp($query, '/', 1) === 0) { | ||
| $parts = explode('?', $query, 2); | ||
| $path = $parts[0]; | ||
| $newQuery = $parts[1] ?? ''; | ||
|
|
||
| $this->superglobals->setServer('QUERY_STRING', $newQuery); | ||
| } else { | ||
| $path = $query; | ||
| } | ||
|
|
||
| // Update our global GET for values likely to have been changed | ||
| parse_str($this->superglobals->server('QUERY_STRING'), $get); | ||
| $this->superglobals->setGetArray($get); | ||
|
|
||
| return URI::removeDotSegments($path); | ||
| } | ||
|
|
||
| /** | ||
| * Create current URI object. | ||
| * | ||
| * @param string $routePath URI path relative to baseURL | ||
| */ | ||
| private function createURIFromRoutePath(string $routePath): SiteURI | ||
| { | ||
| $query = $this->superglobals->server('QUERY_STRING') ?? ''; | ||
|
|
||
| $relativePath = $query !== '' ? $routePath . '?' . $query : $routePath; | ||
|
|
||
| return new SiteURI($this->appConfig, $relativePath, $this->getHost()); | ||
| } | ||
|
|
||
| /** | ||
| * @return string|null The current hostname. Returns null if no valid host. | ||
| */ | ||
| private function getHost(): ?string | ||
| { | ||
| $httpHostPort = $this->superglobals->server('HTTP_HOST') ?? null; | ||
|
|
||
| if ($httpHostPort !== null) { | ||
| [$httpHost] = explode(':', $httpHostPort, 2); | ||
|
|
||
| return $this->getValidHost($httpHost); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @return string|null The valid hostname. Returns null if not valid. | ||
| */ | ||
| private function getValidHost(string $host): ?string | ||
| { | ||
| if (in_array($host, $this->appConfig->allowedHostnames, true)) { | ||
| return $host; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter; | ||
|
|
||
| /** | ||
| * Superglobals manipulation. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class Superglobals | ||
| { | ||
| private array $server; | ||
| private array $get; | ||
|
|
||
| public function __construct(?array $server = null, ?array $get = null) | ||
| { | ||
| $this->server = $server ?? $_SERVER; | ||
| $this->get = $get ?? $_GET; | ||
| } | ||
|
|
||
| public function server(string $key): ?string | ||
| { | ||
| return $this->server[$key] ?? null; | ||
| } | ||
|
|
||
| public function setServer(string $key, string $value): void | ||
| { | ||
| $this->server[$key] = $value; | ||
| $_SERVER[$key] = $value; | ||
| } | ||
|
|
||
| /** | ||
| * @return array|string|null | ||
| */ | ||
| public function get(string $key) | ||
| { | ||
| return $this->get[$key] ?? null; | ||
| } | ||
|
|
||
| public function setGet(string $key, string $value): void | ||
| { | ||
| $this->get[$key] = $value; | ||
| $_GET[$key] = $value; | ||
| } | ||
|
|
||
| public function setGetArray(array $array): void | ||
| { | ||
| $this->get = $array; | ||
| $_GET = $array; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.