Skip to content

Commit

Permalink
Removing direct tests of protected method.
Browse files Browse the repository at this point in the history
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 7, 2010
1 parent 540f142 commit 7691990
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
45 changes: 24 additions & 21 deletions cake/libs/view/view.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -633,6 +644,7 @@ public function loadHelpers() {
foreach ($helpers as $name => $properties) {
$this->Helpers->load($properties['class'], $properties['settings'], true);
}
$this->_helpersLoaded = true;
}

/**
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/libs/view/view.test.php
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 7691990

Please sign in to comment.