diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2017-08-28 16:21:10 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2017-08-28 17:34:00 +0200 |
commit | 73175e2b64c85c7a8c528c76452cd82ffa99f925 (patch) | |
tree | 7464fe30c04fe245424646a98cfd6247d060e517 /src/Http | |
parent | e1762e7764d4ee4f37757ecd2630f62a440dbf0e (diff) |
#337: Added routing
Diffstat (limited to 'src/Http')
-rw-r--r-- | src/Http/Request.php | 108 |
1 files changed, 103 insertions, 5 deletions
diff --git a/src/Http/Request.php b/src/Http/Request.php index 2efd1e1d..ded1c95b 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -9,19 +9,73 @@ class Request /** @var self */ protected static $instance; + /** @var array of GET data */ + protected $query; + /** @var array of POST data */ protected $request; - /** @var array of GET data */ - protected $query; + /** @var array of SERVER data */ + protected $server; + + /** @var string */ + protected $scheme; + + /** @var string */ + protected $host; + + /** @var string */ + protected $baseUrl = ''; + + /** @var string */ + protected $path; /** * Initialize request + * + * @param array $query The GET data + * @param array $request the POST data + * @param array $server the SERVER data + * @param string $baseUrl base url to use for links */ - public function create() + public function create(array $query, array $request, array $server, $baseUrl = null) { - $this->request = $_POST; - $this->query = $_GET; + $this->query = $query; + $this->request = $request; + $this->server = array_merge([ + 'SERVER_NAME' => 'localhost', + 'HTTP_HOST' => 'localhost', + 'SERVER_PORT' => 80, + 'REQUEST_URI' => '/', + ], $server); + + if (isset($this->server['HTTPS']) && $this->server['HTTPS'] == 'off') { + unset($this->server['HTTPS']); + } + + $uri = $this->server['REQUEST_URI']; + $uri = '/' . ltrim($uri, '/'); + $uri = explode('?', $uri); + $this->path = array_shift($uri); + + $components = parse_url($baseUrl); + if (!$components) { + $components = []; + } + + $this->scheme = (isset($components['scheme']) ? $components['scheme'] : ($this->isSecure() ? 'https' : 'http')); + $this->host = (isset($components['host']) ? $components['host'] : $this->server['SERVER_NAME']); + + if (isset($components['path'])) { + $this->baseUrl = '/' . ltrim($components['path'], '/'); + $this->path = preg_replace('~^' . preg_quote($this->baseUrl, '~') . '~i', '', $this->path); + $this->path = '/' . ltrim($this->path, '/'); + } + } + + public function isSecure() + { + return isset($this->server['HTTPS']); } /** @@ -88,6 +142,50 @@ class Request } /** + * Get the requested path + * + * @return string + */ + public function path() + { + // @TODO: base uri? + return $this->path; + } + + public function url() + { + return $this->getSchemeAndHttpHost() . $this->getBaseUrl() . '/' . $this->path(); + } + + /** + * @return string + */ + public function root() + { + return $this->baseUrl; + } + + public function getSchemeAndHttpHost() + { + return $this->getScheme() . '://' . $this->getHttpHost(); + } + + public function getScheme() + { + return $this->scheme; + } + + public function getHttpHost() + { + return $this->host; + } + + public function getBaseUrl() + { + return $this->baseUrl; + } + + /** * @return self * @throws ErrorException */ |