diff --git a/jumpapp/classes/Language.php b/jumpapp/classes/Language.php index 4c70ed2..99d6963 100644 --- a/jumpapp/classes/Language.php +++ b/jumpapp/classes/Language.php @@ -14,6 +14,7 @@ namespace Jump; use \Jump\Exceptions\ConfigException; +use \Jump\Pages\ErrorPage; /** * Defines a class for loading language strings form available translations files, caching @@ -64,7 +65,7 @@ class Language { try { $locale = new \Utopia\Locale\Locale($this->config->get('language')); } catch (\Exception) { - (new Pages\ErrorPage($this->cache, $this->config, 500, 'Provided language code has no corresponding translation file.'))->init(); + ErrorPage::display($this->config, 500, 'Provided language code has no corresponding translation file.'); } $this->locale = $locale; diff --git a/jumpapp/classes/Main.php b/jumpapp/classes/Main.php index 1e77267..9634f34 100644 --- a/jumpapp/classes/Main.php +++ b/jumpapp/classes/Main.php @@ -13,6 +13,9 @@ namespace Jump; +use \Jump\Pages\ErrorPage; +use \Tracy\Debugger; + class Main { private Cache $cache; @@ -23,7 +26,18 @@ class Main { private \Nette\Http\Session $session; public function __construct() { + // We can't do anything without the config object. $this->config = new Config(); + + // Set something to either display detailed debugging info or handle exceptions + // as early as possible during initialisation. + if ($this->config->get('debug')) { + Debugger::enable(Debugger::Development); + } else { + set_exception_handler([$this, 'exception_handler']); + } + + // Carry on setting things up. $this->cache = new Cache($this->config); $this->router = new \Nette\Routing\RouteList; $this->language = new Language($this->config, $this->cache); @@ -49,7 +63,7 @@ class Main { ]); } - function init() { + public function init() { // Create a request object based on globals so we can utilise url rewriting etc. $this->request = (new \Nette\Http\RequestFactory)->fromGlobals(); @@ -70,4 +84,15 @@ class Main { return $page->get_output(); } + /** + * Global exception handler, display friendly message if something goes wrong. + * + * @param $exception + * @return void + */ + public function exception_handler($exception): void { + error_log($exception->getMessage()); + ErrorPage::display($this->config, 500, 'Something went wrong, please use debug option to see details.'); + } + } diff --git a/jumpapp/classes/Pages/ErrorPage.php b/jumpapp/classes/Pages/ErrorPage.php index 86f35af..9015597 100644 --- a/jumpapp/classes/Pages/ErrorPage.php +++ b/jumpapp/classes/Pages/ErrorPage.php @@ -14,26 +14,17 @@ namespace Jump\Pages; class ErrorPage { - - private string $content; - - public function __construct(private \Jump\Cache $cache, private \Jump\Config $config, private int $httpcode, public string $message) { - $this->mustache = new \Mustache_Engine([ - 'loader' => new \Mustache_Loader_FilesystemLoader($this->config->get('templatedir')) + public static function display(\Jump\Config $config, int $httpcode, string $message) { + $mustache = new \Mustache_Engine([ + 'loader' => new \Mustache_Loader_FilesystemLoader($config->get('templatedir')) ]); - $this->content = $cache->load(cachename: 'templates/errorpage', key: $httpcode.md5($message), callback: function() use ($httpcode, $message) { - $template = $this->mustache->loadTemplate('errorpage'); - return $template->render([ - 'code' => $httpcode, - 'message' => $message, - 'wwwurl' => $this->config->get_wwwurl(), - ]); - }); + $template = $mustache->loadTemplate('errorpage'); + $content = $template->render([ + 'code' => $httpcode, + 'message' => $message, + 'wwwurl' => $config->get_wwwurl(), + ]); + http_response_code($httpcode); + die($content); } - - public function init() { - http_response_code($this->httpcode); - die($this->content); - } - } diff --git a/jumpapp/classes/Pages/TagPage.php b/jumpapp/classes/Pages/TagPage.php index 9962f35..9fd4c56 100644 --- a/jumpapp/classes/Pages/TagPage.php +++ b/jumpapp/classes/Pages/TagPage.php @@ -63,7 +63,7 @@ class TagPage extends AbstractPage { $taggedsites = $sites->get_sites_by_tag($this->tagname); } catch (TagNotFoundException) { - (new ErrorPage($this->cache, $this->config, 404, 'There are no sites with this tag.'))->init(); + ErrorPage::display($this->config, 404, 'There are no sites with this tag.'); } $template = $this->mustache->loadTemplate('sites'); return $template->render([ diff --git a/jumpapp/composer.json b/jumpapp/composer.json index e2be85e..e8f54a5 100644 --- a/jumpapp/composer.json +++ b/jumpapp/composer.json @@ -20,6 +20,7 @@ "guzzlehttp/guzzle": "^7.0", "unsplash/unsplash": "3.2.1", "divineomega/array_undot": "^4.1", - "utopia-php/locale": "^0.6.0" + "utopia-php/locale": "^0.6.0", + "tracy/tracy": "^2.10" } } diff --git a/jumpapp/composer.lock b/jumpapp/composer.lock index 1f24898..9d35ef0 100644 --- a/jumpapp/composer.lock +++ b/jumpapp/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": "5fcb97502e7722b9364b85cdd3a4f851", + "content-hash": "124fb3f5325190e7a3c8284b42e9e304", "packages": [ { "name": "arthurhoaro/favicon", @@ -1502,6 +1502,80 @@ ], "time": "2023-02-21T19:46:44+00:00" }, + { + "name": "tracy/tracy", + "version": "v2.10.2", + "source": { + "type": "git", + "url": "https://github.com/nette/tracy.git", + "reference": "882fee7cf4258a602ad4a37461e837ed2ca1406b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nette/tracy/zipball/882fee7cf4258a602ad4a37461e837ed2ca1406b", + "reference": "882fee7cf4258a602ad4a37461e837ed2ca1406b", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-session": "*", + "php": ">=8.0 <8.3" + }, + "conflict": { + "nette/di": "<3.0" + }, + "require-dev": { + "latte/latte": "^2.5", + "nette/di": "^3.0", + "nette/mail": "^3.0", + "nette/tester": "^2.2", + "nette/utils": "^3.0", + "phpstan/phpstan": "^1.0", + "psr/log": "^1.0 || ^2.0 || ^3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.10-dev" + } + }, + "autoload": { + "files": [ + "src/Tracy/functions.php" + ], + "classmap": [ + "src" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "David Grudl", + "homepage": "https://davidgrudl.com" + }, + { + "name": "Nette Community", + "homepage": "https://nette.org/contributors" + } + ], + "description": "😎 Tracy: the addictive tool to ease debugging PHP code for cool developers. Friendly design, logging, profiler, advanced features like debugging AJAX calls or CLI support. You will love it.", + "homepage": "https://tracy.nette.org", + "keywords": [ + "Xdebug", + "debug", + "debugger", + "nette", + "profiler" + ], + "support": { + "issues": "https://github.com/nette/tracy/issues", + "source": "https://github.com/nette/tracy/tree/v2.10.2" + }, + "time": "2023-03-29T12:34:53+00:00" + }, { "name": "unsplash/unsplash", "version": "3.2.1", diff --git a/jumpapp/config.php b/jumpapp/config.php index a49e277..de156fa 100644 --- a/jumpapp/config.php +++ b/jumpapp/config.php @@ -29,6 +29,10 @@ return [ // Where is the cache storage directory, should not be public. 'cachedir' => getenv('CACHEDIR') ?: '/var/www/cache', + // Soemthing not working? Set this to "true" to display detailed + // debugging information. + 'debug' => getenv('DEBUG') ?: false, + // Display alternative layout of sites list. 'altlayout' => getenv('ALTLAYOUT') ?: false, // Should the clock be displayed?