diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Database/Db.php | 9 | ||||
-rw-r--r-- | src/Exceptions/Handler.php | 56 | ||||
-rw-r--r-- | src/Http/Request.php | 70 | ||||
-rw-r--r-- | src/Routing/UrlGenerator.php | 27 | ||||
-rw-r--r-- | src/helpers.php | 28 |
5 files changed, 136 insertions, 54 deletions
diff --git a/src/Database/Db.php b/src/Database/Db.php index 777b16a2..7042998f 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -79,9 +79,10 @@ class Db return self::$stm->fetchAll(PDO::FETCH_ASSOC); } - + /** * Run a select query and return only the first result or null if no result is found. + * * @param string $query * @param array $bindings * @return array|null @@ -89,11 +90,11 @@ class Db public static function selectOne($query, array $bindings = []) { $result = self::select($query, $bindings); - - if(empty($result)) { + + if (empty($result)) { return null; } - + return array_shift($result); } diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index e52549e7..c4fb639c 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 '<pre style="background-color:#333;color:#ccc;z-index:1000;position:absolute;top:1em;padding:1em;width:97%;overflow-y:auto;">'; + echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;max-height: 90%;overflow-y:auto;">'; 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 '</pre>'; die(); @@ -88,6 +94,44 @@ class Handler } /** + * @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 */ public function setEnvironment($environment) diff --git a/src/Http/Request.php b/src/Http/Request.php index fcfc2600..f0235d45 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -3,88 +3,70 @@ 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 POST data */ - protected $request; - - /** @var array of GET data */ - protected $query; - /** - * Initialize request + * Get POST input + * + * @param string $key + * @param mixed $default + * @return mixed */ - public function create() + public function postData($key, $default = null) { - $this->request = $_POST; - $this->query = $_GET; + return $this->request->get($key, $default); } /** - * Get GET input + * Get input data * * @param string $key * @param mixed $default * @return mixed */ - public function get($key, $default = null) + public function input($key, $default = null) { - if (!empty($this->query[$key])) { - return $this->query[$key]; - } - - return $default; + return $this->get($key, $default); } /** - * Get POST input + * Checks if the input exists * * @param string $key - * @param mixed $default - * @return mixed + * @return bool */ - public function post($key, $default = null) + public function has($key) { - if (!empty($this->request[$key])) { - return $this->request[$key]; - } + $value = $this->input($key); - return $default; + return !empty($value); } /** - * Get input data + * Get the requested path * - * @param string $key - * @param mixed $default - * @return mixed + * @return string */ - public function input($key, $default = null) + public function path() { - $data = $this->request + $this->query; + $pattern = trim($this->getPathInfo(), '/'); - if (isset($data[$key])) { - return $data[$key]; - } - - return $default; + return $pattern == '' ? '/' : $pattern; } /** - * Checks if the input exists + * Return the current URL * - * @param string $key - * @return bool + * @return string */ - public function has($key) + public function url() { - $data = $this->request + $this->query; - - return isset($data[$key]); + 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 @@ +<?php + +namespace Engelsystem\Routing; + +use Engelsystem\Http\Request; + +class UrlGenerator +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + public static function to($path, $parameters = []) + { + $path = '/' . ltrim($path, '/'); + $request = Request::getInstance(); + $uri = $request->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..24f93f2c 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,6 +4,8 @@ use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; +use Engelsystem\Routing\UrlGenerator; +use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Get or set config values @@ -42,6 +44,22 @@ function request($key = null, $default = null) } /** + * @param string $key + * @param mixed $default + * @return SessionInterface|mixed + */ +function session($key = null, $default = null) +{ + $session = request()->getSession(); + + if (is_null($key)) { + return $session; + } + + return $session->get($key, $default); +} + +/** * @param string $template * @param mixed[] $data * @return Renderer|string @@ -56,3 +74,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); +} |