diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2017-08-29 16:21:25 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2017-08-29 21:52:07 +0200 |
commit | cc01c906ba63b3797bf2b9ef92a6854fe2ddbefb (patch) | |
tree | ea819678100f6a50d67f1f8516e82fdef8a9196b /src/Http/Request.php | |
parent | 73175e2b64c85c7a8c528c76452cd82ffa99f925 (diff) |
#336: Integration of symfony/http-foundation request
Diffstat (limited to 'src/Http/Request.php')
-rw-r--r-- | src/Http/Request.php | 138 |
1 files changed, 11 insertions, 127 deletions
diff --git a/src/Http/Request.php b/src/Http/Request.php index ded1c95b..f0235d45 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -3,97 +3,13 @@ namespace Engelsystem\Http; use ErrorException; +use Symfony\Component\HttpFoundation\Request as SymfonyRequest; -class Request +class Request extends SymfonyRequest { /** @var self */ protected static $instance; - /** @var array of GET data */ - protected $query; - - /** @var array of POST data */ - protected $request; - - /** @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(array $query, array $request, array $server, $baseUrl = null) - { - $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']); - } - - /** - * Get GET input - * - * @param string $key - * @param mixed $default - * @return mixed - */ - public function get($key, $default = null) - { - if (!empty($this->query[$key])) { - return $this->query[$key]; - } - - return $default; - } - /** * Get POST input * @@ -101,13 +17,9 @@ class Request * @param mixed $default * @return mixed */ - public function post($key, $default = null) + public function postData($key, $default = null) { - if (!empty($this->request[$key])) { - return $this->request[$key]; - } - - return $default; + return $this->request->get($key, $default); } /** @@ -119,13 +31,7 @@ class Request */ public function input($key, $default = null) { - $data = $this->request + $this->query; - - if (!empty($data[$key])) { - return $data[$key]; - } - - return $default; + return $this->get($key, $default); } /** @@ -148,41 +54,19 @@ class Request */ public function path() { - // @TODO: base uri? - return $this->path; - } + $pattern = trim($this->getPathInfo(), '/'); - public function url() - { - return $this->getSchemeAndHttpHost() . $this->getBaseUrl() . '/' . $this->path(); + return $pattern == '' ? '/' : $pattern; } /** + * Return the current URL + * * @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() + public function url() { - return $this->baseUrl; + return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); } /** |