From 3038ed7a49a640e08b0ce1fb3ea7134898199a7f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 16 Jun 2011 21:55:02 -0500 Subject: [PATCH] fix view handling to properly catch exceptions when rendering. --- system/view.php | 98 +++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 43 deletions(-) diff --git a/system/view.php b/system/view.php index fee9a1a5..1cc2eae0 100644 --- a/system/view.php +++ b/system/view.php @@ -48,47 +48,6 @@ class View { return new self($view, $data); } - /** - * Load the content of a view. - * - * @param string $view - * @return string - */ - private function load($view) - { - // ----------------------------------------------------- - // Does the view exist in the application directory? - // ----------------------------------------------------- - if (file_exists($path = APP_PATH.'views/'.$view.EXT)) - { - return file_get_contents($path); - } - // ----------------------------------------------------- - // Does the view exist in the system directory? - // ----------------------------------------------------- - elseif (file_exists($path = SYS_PATH.'views/'.$view.EXT)) - { - return file_get_contents($path); - } - else - { - throw new \Exception("View [$view] doesn't exist."); - } - } - - /** - * Add a key / value pair to the view data. - * - * @param string $key - * @param mixed $value - * @return View - */ - public function bind($key, $value) - { - $this->data[$key] = $value; - return $this; - } - /** * Get the parsed content of the view. * @@ -118,15 +77,68 @@ class View { extract($this->data, EXTR_SKIP); // ----------------------------------------------------- - // Get the string content of the view. + // Start the output buffer so nothing escapes to the + // browser. The response will be sent later. // ----------------------------------------------------- ob_start(); - echo eval('?>'.$this->load($this->view)); + $path = $this->find(); + + // ----------------------------------------------------- + // We include the view into the local scope within a + // try / catch block to catch any exceptions that may + // occur while the view is rendering. + // ----------------------------------------------------- + try + { + include $path; + } + catch (\Exception $e) + { + Error::handle($e); + } return ob_get_clean(); } + /** + * Get the full path to the view. + * + * Views are cascaded, so the application directory views + * will take precedence over the system directory's views + * of the same name. + * + * @return string + */ + private function find() + { + if (file_exists($path = APP_PATH.'views/'.$this->view.EXT)) + { + return $path; + } + elseif (file_exists($path = SYS_PATH.'views/'.$this->view.EXT)) + { + return $path; + } + else + { + throw new \Exception("View [".$this->view."] doesn't exist."); + } + } + + /** + * Add a key / value pair to the view data. + * + * @param string $key + * @param mixed $value + * @return View + */ + public function bind($key, $value) + { + $this->data[$key] = $value; + return $this; + } + /** * Magic Method for getting items from the view data. */