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

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()
{