Skip to content
Permalink
Browse files

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.
  • Loading branch information...
markstory committed Nov 5, 2010
1 parent 540f142 commit 76919902b90be98c05d7aaf49ab233912333edda
Showing with 26 additions and 23 deletions.
  1. +24 −21 cake/libs/view/view.php
  2. +2 −2 cake/tests/cases/libs/view/view.test.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,14 +381,19 @@ public function render($action = null, $layout = null, $file = null) {
if ($this->hasRendered) {
return true;
}
if (!$this->_helpersLoaded) {
$this->loadHelpers();
}
$out = null;
if ($file != null) {
$action = $file;
}
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: <blockquote>%s</blockquote>"), $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)
@@ -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();

0 comments on commit 7691990

Please sign in to comment.
You can’t perform that action at this time.