From 76919902b90be98c05d7aaf49ab233912333edda Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 4 Nov 2010 22:29:41 -0400 Subject: [PATCH] Removing direct tests of protected method. Refactoring View with the intention of eventually removing coupling with CacheHelper. Making Helpers load after the first rendering method is called. Moving callbacks out of _render() to make logic simpler, and with the idea that elements will get a callback too. --- cake/libs/view/view.php | 45 +++++++++++++----------- cake/tests/cases/libs/view/view.test.php | 4 +-- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index 92c917f1d8f..04f7ef028fe 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -267,6 +267,13 @@ class View extends Object { */ private $__paths = array(); +/** + * boolean to indicate that helpers have been loaded. + * + * @var boolean + */ + protected $_helpersLoaded = false; + /** * Constructor * @@ -345,6 +352,9 @@ public function element($name, $params = array(), $loadHelpers = false) { } if (is_file($file)) { + if (!$this->_helpersLoaded) { + $this->loadHelpers(); + } $element = $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers); if (isset($params['cache']) && isset($cacheFile) && isset($expires)) { cache('views' . DS . $cacheFile, $element, $expires); @@ -371,6 +381,9 @@ public function render($action = null, $layout = null, $file = null) { if ($this->hasRendered) { return true; } + if (!$this->_helpersLoaded) { + $this->loadHelpers(); + } $out = null; if ($file != null) { @@ -378,7 +391,9 @@ public function render($action = null, $layout = null, $file = null) { } if ($action !== false && $viewFileName = $this->_getViewFileName($action)) { + $this->Helpers->trigger('beforeRender', array($this, $viewFileName)); $out = $this->_render($viewFileName); + $this->Helpers->trigger('afterRender', array($this, $viewFileName, $out)); } if ($layout === null) { @@ -422,7 +437,10 @@ public function renderLayout($content_for_layout, $layout = null) { if (empty($layoutFileName)) { return $this->output; } - $this->Helpers->trigger('beforeLayout', array(&$this)); + if (!$this->_helpersLoaded) { + $this->loadHelpers(); + } + $this->Helpers->trigger('beforeLayout', array(&$this, $layoutFileName)); $this->viewVars = array_merge($this->viewVars, array( 'content_for_layout' => $content_for_layout, @@ -432,23 +450,16 @@ public function renderLayout($content_for_layout, $layout = null) { if (!isset($this->viewVars['title_for_layout'])) { $this->viewVars['title_for_layout'] = Inflector::humanize($this->viewPath); } - - $attached = $this->Helpers->attached(); - if (empty($attached) && !empty($this->helpers)) { - $loadHelpers = true; - } else { - $loadHelpers = false; - } - $this->output = $this->_render($layoutFileName, array(), $loadHelpers, true); + $this->output = $this->_render($layoutFileName); if ($this->output === false) { $this->output = $this->_render($layoutFileName, $data_for_layout); trigger_error(sprintf(__("Error in layout %s, got:
%s
"), $layoutFileName, $this->output), E_USER_ERROR); return false; } - - $this->Helpers->trigger('afterLayout', array(&$this)); + + $this->Helpers->trigger('afterLayout', array(&$this, $layoutFileName, $this->output)); return $this->output; } @@ -633,6 +644,7 @@ public function loadHelpers() { foreach ($helpers as $name => $properties) { $this->Helpers->load($properties['class'], $properties['settings'], true); } + $this->_helpersLoaded = true; } /** @@ -646,12 +658,6 @@ public function loadHelpers() { * @return string Rendered output */ protected function _render($___viewFn, $___dataForView = array(), $loadHelpers = true, $cached = false) { - $attached = $this->Helpers->attached(); - if (count($attached) === 0 && $loadHelpers === true) { - $this->loadHelpers(); - $this->Helpers->trigger('beforeRender', array(&$this)); - unset($attached); - } if (empty($___dataForView)) { $___dataForView = $this->viewVars; } @@ -661,11 +667,8 @@ protected function _render($___viewFn, $___dataForView = array(), $loadHelpers = include $___viewFn; - if ($loadHelpers === true) { - $this->Helpers->trigger('afterRender', array(&$this)); - } - $out = ob_get_clean(); + $caching = ( isset($this->Helpers->Cache) && (($this->cacheAction != false)) && (Configure::read('Cache.check') === true) diff --git a/cake/tests/cases/libs/view/view.test.php b/cake/tests/cases/libs/view/view.test.php index c31e7ef8292..5c303cf747f 100644 --- a/cake/tests/cases/libs/view/view.test.php +++ b/cake/tests/cases/libs/view/view.test.php @@ -635,7 +635,7 @@ function testRenderLoadHelper() { $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number'); $View = new TestView($this->PostsController); - $result = $View->render_($View->getViewFileName('index'), array()); + $result = $View->render('index', false); $this->assertEqual($result, 'posts index'); $attached = $View->Helpers->attached(); @@ -644,7 +644,7 @@ function testRenderLoadHelper() { $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'); $View = new TestView($this->PostsController); - $result = $View->render_($View->getViewFileName('index'), array()); + $result = $View->render('index', false); $this->assertEqual($result, 'posts index'); $attached = $View->Helpers->attached();