diff --git a/.env b/.env new file mode 100644 index 0000000..352bc6c --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DEBUG=true \ No newline at end of file diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 2d908e7..4f6c5a6 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -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']); } diff --git a/composer.json b/composer.json index 99982d3..40292c9 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/composer.lock b/composer.lock index d32a9e1..19b37a1 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/resources/views/errors/500.twig b/resources/views/errors/500.twig new file mode 100644 index 0000000..77168b2 --- /dev/null +++ b/resources/views/errors/500.twig @@ -0,0 +1 @@ +An error has occured \ No newline at end of file diff --git a/resources/views/index.html b/resources/views/index.twig similarity index 100% rename from resources/views/index.html rename to resources/views/index.twig diff --git a/src/Runtime/Exceptions/ExceptionHandler.php b/src/Runtime/Exceptions/ExceptionHandler.php index 8b03c75..9cca784 100644 --- a/src/Runtime/Exceptions/ExceptionHandler.php +++ b/src/Runtime/Exceptions/ExceptionHandler.php @@ -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; }); } diff --git a/src/Runtime/Factory/BootstrapFactory.php b/src/Runtime/Factory/BootstrapFactory.php index c591747..6ba5775 100644 --- a/src/Runtime/Factory/BootstrapFactory.php +++ b/src/Runtime/Factory/BootstrapFactory.php @@ -79,7 +79,7 @@ class BootstrapFactory { } /** - * @return Environment + * @return ViewEngine */ public function view() { diff --git a/src/Runtime/Http/View/Factory.php b/src/Runtime/Http/View/Factory.php index 49b37fb..7b34348 100644 --- a/src/Runtime/Http/View/Factory.php +++ b/src/Runtime/Http/View/Factory.php @@ -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); } } \ No newline at end of file diff --git a/src/Runtime/Http/View/ViewEngine.php b/src/Runtime/Http/View/ViewEngine.php index 516907f..cecf9a6 100644 --- a/src/Runtime/Http/View/ViewEngine.php +++ b/src/Runtime/Http/View/ViewEngine.php @@ -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() {