Update error handler

This commit is contained in:
Maarten 2019-12-27 00:48:09 +01:00
parent 37e4278952
commit b2ce582152
10 changed files with 131 additions and 24 deletions

1
.env Normal file
View file

@ -0,0 +1 @@
DEBUG=true

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use Runtime\Http\Controllers\Controller;
use Runtime\Http\View\Factory;
class HomeController extends Controller {
@ -13,8 +14,7 @@ class HomeController extends Controller {
public function test()
{
a;
a
return view('index', ['test' => 'Dit is een test']);
}

View file

@ -10,6 +10,7 @@
"php": "^7.2",
"ext-json": "*",
"filp/whoops": "^2.6",
"kint-php/kint": "^3.3",
"php-di/php-di": "^6.0",
"twig/twig": "^3.0"
},

72
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "8dc6d12612bce2ab7a927687ee0aeca5",
"content-hash": "01196a6854719a6769583f7e66f1b40b",
"packages": [
{
"name": "filp/whoops",
@ -125,6 +125,76 @@
],
"time": "2018-03-21T22:21:57+00:00"
},
{
"name": "kint-php/kint",
"version": "3.3",
"source": {
"type": "git",
"url": "https://github.com/kint-php/kint.git",
"reference": "335ac1bcaf04d87df70d8aa51e8887ba2c6d203b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/kint-php/kint/zipball/335ac1bcaf04d87df70d8aa51e8887ba2c6d203b",
"reference": "335ac1bcaf04d87df70d8aa51e8887ba2c6d203b",
"shasum": ""
},
"require": {
"php": ">=5.3.6"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0",
"phpunit/phpunit": "^4.0",
"seld/phar-utils": "^1.0",
"symfony/finder": "^2.0 || ^3.0 || ^4.0",
"vimeo/psalm": "^3.0"
},
"suggest": {
"ext-ctype": "Simple data type tests",
"ext-iconv": "Provides fallback detection for ambiguous legacy string encodings such as the Windows and ISO 8859 code pages",
"ext-mbstring": "Provides string encoding detection",
"kint-php/kint-js": "Provides a simplified dump to console.log()",
"kint-php/kint-twig": "Provides d() and s() functions in twig templates",
"symfony/polyfill-ctype": "Replacement for ext-ctype if missing",
"symfony/polyfill-iconv": "Replacement for ext-iconv if missing",
"symfony/polyfill-mbstring": "Replacement for ext-mbstring if missing"
},
"type": "library",
"autoload": {
"files": [
"init.php"
],
"psr-4": {
"Kint\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jonathan Vollebregt",
"homepage": "https://github.com/jnvsor"
},
{
"name": "Rokas Šleinius",
"homepage": "https://github.com/raveren"
},
{
"name": "Contributors",
"homepage": "https://github.com/kint-php/kint/graphs/contributors"
}
],
"description": "Kint - debugging tool for PHP developers",
"homepage": "https://kint-php.github.io/kint/",
"keywords": [
"debug",
"kint",
"php"
],
"time": "2019-10-17T18:05:24+00:00"
},
{
"name": "nikic/php-parser",
"version": "v4.3.0",

View file

@ -0,0 +1 @@
An error has occured

View file

@ -43,9 +43,10 @@ class ExceptionHandler {
$debug = false; //TODO: Get from config
if($debug === false) {
self::$handler->pushHandler(function(\Exception $exception, $inspector, $run) {
echo 'An error has occured';
//TODO: Add proper 500 error page
self::$handler->pushHandler(function() {
if(app()->view()->exist('errors.500')) {
echo app()->view()->render('errors.500');
}
return Handler::DONE;
});
}

View file

@ -79,7 +79,7 @@ class BootstrapFactory {
}
/**
* @return Environment
* @return ViewEngine
*/
public function view()
{

View file

@ -2,7 +2,6 @@
namespace Runtime\Http\View;
use Runtime\Exceptions\ExceptionHandler;
use Runtime\Factory\AppFactory;
use Twig\Error\LoaderError;
@ -28,11 +27,11 @@ class Factory extends AppFactory implements \Runtime\Contracts\View\Factory {
*/
public function make($name, $arguments = [])
{
$this->name = $this->name($name);
$this->name = $name;
$this->arguments = $arguments;
try {
return $this->render();
return $this->render();
}
catch (\Exception $e) {
ExceptionHandler::make($e);
@ -47,15 +46,6 @@ class Factory extends AppFactory implements \Runtime\Contracts\View\Factory {
*/
private function render()
{
return app()->view()->render($this->name . '.html', $this->arguments);
}
/**
* @param $name
* @return string|string[]
*/
private function name($name)
{
return str_replace('.', '/', $name);
return app()->view()->render($this->name, $this->arguments);
}
}

View file

@ -4,25 +4,35 @@ namespace Runtime\Http\View;
use Runtime\Factory\AppFactory;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class ViewEngine extends AppFactory {
/**
* @var Environment
* @var self
*/
private static $instance;
/**
* @var Environment
*/
private static $twig;
public function __construct()
{
$loader = new FilesystemLoader(app()->resourcePath() . 'views');
self::$instance = new Environment($loader, [
self::$twig = new Environment($loader, [
//'cache' => app()->resourcePath() . 'cache'
]);
$this->loadHelperFunctions();
self::$instance = $this;
}
/**
@ -38,12 +48,45 @@ class ViewEngine extends AppFactory {
return call_user_func_array($function, $params);
});
self::$instance->addFunction($function);
self::$twig->addFunction($function);
}
}
/**
* @return Environment
* @param $name
* @param array $context
* @return string
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function render($name, array $context = [])
{
return self::$twig->render($this->name($name), $context);
}
/**
* @param $name
* @return bool
*/
public function exist($name)
{
$template = app()->resourcePath() . 'views/' . $this->name($name);
return file_exists($template);
}
/**
* @param $name
* @return string|string[]
*/
private function name($name)
{
return str_replace('.', '/', $name) . '.twig';
}
/**
* @return self
*/
public static function get()
{