summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Exceptions/Handler.php56
-rw-r--r--src/Http/Request.php70
-rw-r--r--src/Routing/UrlGenerator.php27
-rw-r--r--src/helpers.php11
4 files changed, 114 insertions, 50 deletions
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..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);
+}