From 8ce3b0c224ed42811f9cd410a7b14862a8071ca7 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Thu, 28 Mar 2019 20:12:46 +0100 Subject: [PATCH] Add debug mode You can enable Pico's debug mode by setting the PICO_DEBUG environment variable. At the moment this just enables Twig's debug mode. --- config/config.yml.template | 1 + lib/Pico.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/config/config.yml.template b/config/config.yml.template index 4d25dcf..638c6d8 100644 --- a/config/config.yml.template +++ b/config/config.yml.template @@ -5,6 +5,7 @@ site_title: Pico # The title of your website base_url: ~ # Pico will try to guess its base URL, if this fails, override it here; # Example: http://example.com/pico/ rewrite_url: ~ # A boolean (true or false) indicating whether URL rewriting is forced +debug: ~ # Set this to true to enable Pico's debug mode timezone: UTC # Your PHP installation might require you to manually specify a timezone ## diff --git a/lib/Pico.php b/lib/Pico.php index a48cccb..50ea42c 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -906,6 +906,7 @@ class Pico 'site_title' => 'Pico', 'base_url' => '', 'rewrite_url' => null, + 'debug' => null, 'timezone' => null, 'theme' => 'default', 'theme_url' => null, @@ -928,6 +929,10 @@ class Pico $this->config['rewrite_url'] = $this->isUrlRewritingEnabled(); } + if ($this->config['debug'] === null) { + $this->config['debug'] = $this->isDebugModeEnabled(); + } + if (!$this->config['timezone']) { // explicitly set a default timezone to prevent a E_NOTICE when no timezone is set; // the `date_default_timezone_get()` function always returns a timezone, at least UTC @@ -959,6 +964,9 @@ class Pico if ($this->config['twig_config']['cache']) { $this->config['twig_config']['cache'] = $this->getAbsolutePath($this->config['twig_config']['cache']); } + if ($this->config['twig_config']['debug'] === null) { + $this->config['twig_config']['debug'] = $this->isDebugModeEnabled(); + } } if (!$this->config['content_dir']) { @@ -2120,6 +2128,29 @@ class Pico return $this->config['rewrite_url']; } + /** + * Returns TRUE if Pico's debug mode is enabled + * + * @return bool TRUE if Pico's debug mode is enabled, FALSE otherwise + */ + public function isDebugModeEnabled() + { + $debugModeEnabled = $this->getConfig('debug'); + if ($debugModeEnabled !== null) { + return $debugModeEnabled; + } + + if (isset($_SERVER['PICO_DEBUG'])) { + $this->config['debug'] = (bool) $_SERVER['PICO_DEBUG']; + } elseif (isset($_SERVER['REDIRECT_PICO_DEBUG'])) { + $this->config['debug'] = (bool) $_SERVER['REDIRECT_PICO_DEBUG']; + } else { + $this->config['debug'] = false; + } + + return $this->config['debug']; + } + /** * Returns the URL to a given page *