Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Improve Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 16, 2017
1 parent e349818 commit 2d7b6a7
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public function __invoke(string $uri): array
//simple URI which do not need any parsing
static $simple_uri = [
'' => [],
'#' => ['fragment' => ''],
'?' => ['query' => ''],
'?#' => ['query' => '', 'fragment' => ''],
'/' => ['path' => '/'],
'//' => ['host' => ''],
];
Expand All @@ -115,9 +118,7 @@ public function __invoke(string $uri): array
//The URI is made of the query and fragment
if ('?' == $first_char) {
$components = self::URI_COMPONENTS;
$parts = explode('#', substr($uri, 1), 2);
$components['query'] = array_shift($parts);
$components['fragment'] = array_shift($parts);
list($components['query'], $components['fragment']) = explode('#', substr($uri, 1), 2) + [null, null];

return $components;
}
Expand Down Expand Up @@ -166,16 +167,12 @@ protected function parseSchemeSpecificPart(string $uri): array
{
//We remove the authority delimiter
$remaining_uri = (string) substr($uri, 2);
$components = self::URI_COMPONENTS;

//Parsing is done from the right upmost part to the left
//1 - detect fragment, query and path part if any
$parts = explode('#', $remaining_uri, 2);
$remaining_uri = array_shift($parts);
$components = self::URI_COMPONENTS;
$components['fragment'] = array_shift($parts);
$parts = explode('?', $remaining_uri, 2);
$remaining_uri = array_shift($parts);
$components['query'] = array_shift($parts);
list($remaining_uri, $components['fragment']) = explode('#', $remaining_uri, 2) + [null, null];
list($remaining_uri, $components['query']) = explode('?', $remaining_uri, 2) + [null, null];
if (false !== ($pos = strpos($remaining_uri, '/'))) {
$components['path'] = substr($remaining_uri, $pos);
$remaining_uri = substr($remaining_uri, 0, $pos);
Expand All @@ -194,9 +191,7 @@ protected function parseSchemeSpecificPart(string $uri): array
$hostname = array_pop($parts);
$user_info = array_pop($parts);
if (null !== $user_info) {
$parts = explode(':', $user_info, 2);
$components['user'] = array_shift($parts);
$components['pass'] = array_shift($parts);
list($components['user'], $components['pass']) = explode(':', $user_info, 2) + [null, null];
}
list($components['host'], $components['port']) = $this->parseHostname($hostname);

Expand All @@ -213,12 +208,9 @@ protected function parseSchemeSpecificPart(string $uri): array
protected function parseHostname(string $hostname): array
{
if (false === strpos($hostname, '[')) {
$parts = explode(':', $hostname, 2);
list($host, $port)= explode(':', $hostname, 2) + [null, null];

return [
$this->filterHost(array_shift($parts)),
$this->filterPort(array_shift($parts)),
];
return [$this->filterHost($host), $this->filterPort($port)];
}

$delimiter_offset = strpos($hostname, ']') + 1;
Expand Down Expand Up @@ -444,17 +436,14 @@ protected function parsePathQueryAndFragment(string $uri): array
throw Exception::createFromInvalidPath($uri);
}

$components = self::URI_COMPONENTS;

//Parsing is done from the right upmost part to the left
//1 - detect the fragment part if any
$parts = explode('#', $uri, 2);
$remaining_uri = array_shift($parts);
$components = self::URI_COMPONENTS;
$components['fragment'] = array_shift($parts);
list($remaining_uri, $components['fragment']) = explode('#', $uri, 2) + [null, null];

//2 - detect the query and the path part
$parts = explode('?', $remaining_uri, 2);
$components['path'] = array_shift($parts);
$components['query'] = array_shift($parts);
list($components['path'], $components['query']) = explode('?', $remaining_uri, 2) + [null, null];

return $components;
}
Expand Down Expand Up @@ -536,14 +525,10 @@ protected function fallbackParser(string $uri): array

//2.5 - Parsing is done from the right upmost part to the left from the scheme specific part
//2.5.1 - detect the fragment part if any
$parts = explode('#', $remaining_uri, 2);
$remaining_uri = array_shift($parts);
$components['fragment'] = array_shift($parts);
list($remaining_uri, $components['fragment']) = explode('#', $remaining_uri, 2) + [null, null];

//2.5.2 - detect the part and query part if any
$parts = explode('?', $remaining_uri, 2);
$components['path'] = array_shift($parts);
$components['query'] = array_shift($parts);
list($components['path'], $components['query']) = explode('?', $remaining_uri, 2) + [null, null];

return $components;
}
Expand Down

0 comments on commit 2d7b6a7

Please sign in to comment.