From b5982ea965d3fdb9cec35c1146d0e56bdfe0bc6e Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 6 Aug 2014 12:51:49 +0300 Subject: [PATCH] Add new event onFatalException and listen to it in problems plugin --- bin/grav | 8 ++--- index.php | 48 ++++++++++++++---------------- system/autoload.php | 12 ++++---- system/src/Grav/Common/Config.php | 4 ++- system/src/Grav/Common/Grav.php | 2 +- user/plugins/problems/problems.php | 35 ++++++++++++++++++++++ 6 files changed, 71 insertions(+), 38 deletions(-) diff --git a/bin/grav b/bin/grav index 1bc34ca15..dd922442e 100755 --- a/bin/grav +++ b/bin/grav @@ -1,13 +1,13 @@ #!/usr/bin/env php store('Grav', new Grav); -$registry->store('Uri', new Uri); -$registry->store('Config', Config::instance(CACHE_DIR . 'config.php')); -$registry->store('Cache', new Cache); -$registry->store('Twig', new Twig); -$registry->store('Pages', new Page\Pages); -$registry->store('Taxonomy', new Taxonomy); +$grav = new Grav; -/** @var Grav $grav */ -$grav = $registry->retrieve('Grav'); -$grav->process(); +try { + // Register all the Grav bits into registry. + $registry = Registry::instance(); + $registry->store('autoloader', $loader); + $registry->store('Grav', $grav); + $registry->store('Uri', new Uri); + $registry->store('Config', Config::instance(CACHE_DIR . 'config.php')); + $registry->store('Cache', new Cache); + $registry->store('Twig', new Twig); + $registry->store('Pages', new Page\Pages); + $registry->store('Taxonomy', new Taxonomy); + + $grav->process(); + +} catch (\Exception $e) { + $grav->fireEvent('onFatalException', $e); + throw $e; +} ob_end_flush(); diff --git a/system/autoload.php b/system/autoload.php index 1e3911cef..7419fb852 100644 --- a/system/autoload.php +++ b/system/autoload.php @@ -1,10 +1,8 @@ addPsr4('Grav\\', LIB_DIR . 'Grav'); - if (strpos($class, 'Grav\\Common') === 0 || strpos($class, 'Grav\\Console') === 0) { - $filename = str_replace('\\', '/', LIB_DIR.$class.'.php'); - include($filename); - } -}); +return $loader; diff --git a/system/src/Grav/Common/Config.php b/system/src/Grav/Common/Config.php index 0228c5db9..68865b6df 100644 --- a/system/src/Grav/Common/Config.php +++ b/system/src/Grav/Common/Config.php @@ -33,6 +33,7 @@ class Config extends Data * @var bool Flag to tell if configuration needs to be saved. */ public $updated = false; + public $issues = array(); /** * Constructor. @@ -92,7 +93,8 @@ class Config extends Data } $this->updated = false; } catch (\Exception $e) { - throw new \RuntimeException('Writing to cache folder failed (configuration).', 500, $e); + $this->issues[] = 'Writing configuration into cache failed.'; + //throw new \RuntimeException('Writing configuration into cache failed.', 500, $e); } return $this; diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 49c42f070..5fe0946c7 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -207,7 +207,7 @@ class Grav extends Getters } } - if ($this->config->get('system.debugger.log.timing') && !in_array($hook_id, $no_timing_hooks)) { + if ($this->config && $this->config->get('system.debugger.log.timing') && !in_array($hook_id, $no_timing_hooks)) { static::log($hook_id.': %f ms'); } } diff --git a/user/plugins/problems/problems.php b/user/plugins/problems/problems.php index aff1a15d2..5dc6b8018 100644 --- a/user/plugins/problems/problems.php +++ b/user/plugins/problems/problems.php @@ -71,6 +71,41 @@ class ProblemsPlugin extends Plugin Registry::get('Twig')->twig_paths[] = __DIR__ . '/templates'; } + /** + * + */ + public function onFatalException($e) + { + // Run through potential issues + if ($this->problemChecker()) { + foreach ($this->results as $key => $result) { + if ($key == 'files') { + foreach ($result as $filename => $status) { + if (key($status)) { + continue; + } + $text = reset($status); + echo "
{$filename} {$text}
"; + } + } else { + if (key($status)) { + continue; + } + $text = reset($status); + echo "
{$text}
"; + } + } + + // Create Required Folders if they don't exist + if (!is_dir(LOG_DIR)) mkdir(LOG_DIR); + if (!is_dir(CACHE_DIR)) mkdir(CACHE_DIR); + if (!is_dir(IMAGES_DIR)) mkdir(IMAGES_DIR); + if (!is_dir(DATA_DIR)) mkdir(DATA_DIR); + + exit(); + } + } + /** * Set needed variables to display the problems. */