From cc01c906ba63b3797bf2b9ef92a6854fe2ddbefb Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 29 Aug 2017 16:21:25 +0200 Subject: #336: Integration of symfony/http-foundation request --- src/Exceptions/Handler.php | 56 ++++++++++++++++-- src/Http/Request.php | 138 ++++--------------------------------------- src/Routing/UrlGenerator.php | 27 +++++++++ src/helpers.php | 11 ++++ 4 files changed, 99 insertions(+), 133 deletions(-) create mode 100644 src/Routing/UrlGenerator.php (limited to 'src') diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index 0532a7d8..6e5b4749 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -34,7 +34,9 @@ class Handler */ public function errorHandler($number, $string, $file, $line, $context) { - $this->handle('error', $number, $string, $file, $line, $context); + $trace = array_reverse(debug_backtrace()); + + $this->handle('error', $number, $string, $file, $line, $context, $trace); } /** @@ -59,8 +61,9 @@ class Handler * @param string $file * @param int $line * @param array $context + * @param array $trace */ - protected function handle($type, $number, $string, $file, $line, $context = []) + protected function handle($type, $number, $string, $file, $line, $context = [], $trace = []) { error_log(sprintf('%s: Number: %s, String: %s, File: %s:%u, Context: %s', $type, @@ -71,13 +74,16 @@ class Handler json_encode($context) )); + $file = $this->stripBasePath($file); + if ($this->environment == self::ENV_DEVELOPMENT) { - echo '
';
+            echo '
';
             echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
             var_export([
-                'string'  => $string,
-                'file'    => $file . ':' . $line,
-                'context' => ($this->environment == self::ENV_DEVELOPMENT ? $context : null),
+                'string'     => $string,
+                'file'       => $file . ':' . $line,
+                'context'    => $context,
+                'stacktrace' => $this->formatStackTrace($trace),
             ]);
             echo '
'; die(); @@ -87,6 +93,44 @@ class Handler die(); } + /** + * @param array $stackTrace + * @return array + */ + protected function formatStackTrace($stackTrace) + { + $return = []; + + foreach ($stackTrace as $trace) { + $path = ''; + $line = ''; + + if (isset($trace['file']) && isset($trace['line'])) { + $path = $this->stripBasePath($trace['file']); + $line = $trace['line']; + } + + $functionName = $trace['function']; + + $return[] = [ + 'file' => $path . ':' . $line, + $functionName => $trace['args'], + ]; + } + + return $return; + } + + /** + * @param string $path + * @return string + */ + protected function stripBasePath($path) + { + $basePath = realpath(__DIR__ . '/../..') . '/'; + return str_replace($basePath, '', $path); + } + /** * @param string $environment */ 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()), '/'); } /** diff --git a/src/Routing/UrlGenerator.php b/src/Routing/UrlGenerator.php new file mode 100644 index 00000000..8dc464c6 --- /dev/null +++ b/src/Routing/UrlGenerator.php @@ -0,0 +1,27 @@ +getUriForPath($path); + + if (!empty($parameters) && is_array($parameters)) { + $parameters = http_build_query($parameters); + $uri .= '?' . $parameters; + } + + return $uri; + } +} diff --git a/src/helpers.php b/src/helpers.php index d44d1d21..af0e802b 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,6 +4,7 @@ use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; +use Engelsystem\Routing\UrlGenerator; /** * Get or set config values @@ -56,3 +57,13 @@ function view($template = null, $data = null) return $renderer->render($template, $data); } + +/** + * @param string $path + * @param array $parameters + * @return string + */ +function url($path, $parameters = []) +{ + return UrlGenerator::to($path, $parameters); +} -- cgit v1.2.3-54-g00ecf