Skip to content

Commit

Permalink
Moving methods around. ObjectCollection now normalizes helpers arrays…
Browse files Browse the repository at this point in the history
…. Plugin helpers now lazy load.

Tests updated.
  • Loading branch information
markstory committed Aug 11, 2010
1 parent 98982a6 commit fc33797
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 22 deletions.
19 changes: 19 additions & 0 deletions cake/libs/object_collection.php
Expand Up @@ -151,4 +151,23 @@ public function attached($name = null) {
return $this->_attached;
}

/**
* Normalizes an object array, creates an array that makes lazy loading
* easier
*
* @param array $objects Array of child objects to normalize.
* @return array Array of normalized objects.
*/
public static function normalizeObjectArray($objects) {
$normal = array();
foreach ($objects as $i => $objectName) {
$options = array();
if (!is_int($i)) {
list($options, $objectName) = array($objectName, $i);
}
list($plugin, $name) = pluginSplit($objectName);
$normal[$name] = array('class' => $objectName, 'settings' => $options);
}
return $normal;
}
}
30 changes: 22 additions & 8 deletions cake/libs/view/helper.php
Expand Up @@ -38,7 +38,14 @@ class Helper extends Object {
*
* @var array
*/
public $helpers = null;
public $helpers = array();

/**
* A helper lookup table used to lazy load helper objects.
*
* @var array
*/
protected $_helperMap = array();

/**
* Base URL
Expand Down Expand Up @@ -143,11 +150,11 @@ class Helper extends Object {
private $__cleaned = null;

/**
* undocumented class variable
* The View instance this helper is attached to
*
* @var string
* @var View
*/
public $View;
protected $_View;

/**
* Default Constructor
Expand All @@ -156,8 +163,11 @@ class Helper extends Object {
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
$this->View = $View;
// Nothing to see here.
$this->_View = $View;
$this->params = $View->params;
if (!empty($this->helpers)) {
$this->_helperMap = ObjectCollection::normalizeObjectArray($this->helpers);
}
}

/**
Expand All @@ -174,8 +184,12 @@ public function __call($method, $params) {
* @return void
*/
public function __get($name) {
if (!empty($this->helpers) && in_array($name, $this->helpers)) {
$this->{$name} = $this->View->Helpers->load($name, array(), false);
if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
$this->{$name} = $this->_View->loadHelper(
$this->_helperMap[$name]['class'], $this->_helperMap[$name]['settings'], false
);
}
if (isset($this->{$name})) {
return $this->{$name};
}
}
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/view/helpers/js.php
Expand Up @@ -98,7 +98,7 @@ public function __construct(View $View, $settings = array()) {
$this->__engineName = $className . 'Engine';
$engineClass = $engineName . 'Engine';
$this->helpers[] = $engineClass;
parent::__construct();
parent::__construct($View, $settings);
}

/**
Expand Down
1 change: 1 addition & 0 deletions cake/libs/view/helpers/paginator.php
Expand Up @@ -90,6 +90,7 @@ class PaginatorHelper extends AppHelper {
* @return void
*/
function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
$ajaxProvider = isset($settings['ajax']) ? $settings['ajax'] : 'Js';
$this->helpers[] = $ajaxProvider;
$this->_ajaxHelperClass = $ajaxProvider;
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/view/helpers/xml.php
Expand Up @@ -44,7 +44,7 @@ class XmlHelper extends AppHelper {
* @return void
*/
function __construct(View $View, $settings = array()) {
parent::__construct();
parent::__construct($View, $settings);
$this->Xml =& new Xml();
$this->Xml->options(array('verifyNs' => false));
}
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/view/view.php
Expand Up @@ -736,8 +736,8 @@ protected function _render($___viewFn, $___dataForView, $loadHelpers = true, $ca
* @param array $settings Settings for the helper
* @return Helper a constructed helper object.
*/
public function loadHelper($helperName, $settings = array()) {
return $this->Helpers->load($helperName, $settings, true);
public function loadHelper($helperName, $settings = array(), $attach = true) {
return $this->Helpers->load($helperName, $settings, $attach);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions cake/tests/cases/libs/view/helper.test.php
Expand Up @@ -163,6 +163,13 @@ function schema() {
}

class TestHelper extends Helper {
/**
* helpers for this helper.
*
* @var string
*/
var $helpers = array('Html', 'TestPlugin.OtherHelper');

/**
* expose a method as public
*
Expand Down Expand Up @@ -790,8 +797,12 @@ function testParseAttributeCompact() {
* @return void
*/
function testLazyLoadingHelpers() {
$this->Helper->helpers = array('Test', 'Html');
$result = $this->Helper->Test;
$this->assertType('TestHelper', $result);
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
));
$Helper = new TestHelper($this->View);
$this->assertType('OtherHelperHelper', $Helper->OtherHelper);
$this->assertType('HtmlHelper', $Helper->Html);
App::build();
}
}
36 changes: 29 additions & 7 deletions cake/tests/cases/libs/view/helper_collection.test.php
Expand Up @@ -28,8 +28,8 @@ class HelperCollectionTest extends CakeTestCase {
* @return void
*/
function setup() {
$View = $this->getMock('View', array(), array(null));
$this->Helpers = new HelperCollection($View);
$this->View = $this->getMock('View', array(), array(null));
$this->Helpers = new HelperCollection($this->View);
}

/**
Expand All @@ -38,7 +38,7 @@ function setup() {
* @return void
*/
function teardown() {
unset($this->Helpers);
unset($this->Helpers, $this->View);
}

/**
Expand Down Expand Up @@ -124,8 +124,8 @@ function testTrigger() {
$this->Helpers->load('Form');
$this->Helpers->load('Html');

$this->Helpers->Html = $this->getMock('HtmlHelper');
$this->Helpers->Form = $this->getMock('FormHelper');
$this->Helpers->Html = $this->getMock('HtmlHelper', array(), array($this->View));
$this->Helpers->Form = $this->getMock('FormHelper', array(), array($this->View));

$this->Helpers->Html->expects($this->once())->method('beforeRender')
->with('one', 'two');
Expand All @@ -144,8 +144,8 @@ function testTriggerWithDisabledHelpers() {
$this->Helpers->load('Form');
$this->Helpers->load('Html');

$this->Helpers->Html = $this->getMock('HtmlHelper');
$this->Helpers->Form = $this->getMock('FormHelper');
$this->Helpers->Html = $this->getMock('HtmlHelper', array(), array($this->View));
$this->Helpers->Form = $this->getMock('FormHelper', array(), array($this->View));

$this->Helpers->Html->expects($this->once())->method('beforeRender')
->with('one', 'two');
Expand All @@ -155,4 +155,26 @@ function testTriggerWithDisabledHelpers() {

$this->Helpers->trigger('beforeRender', array('one', 'two'));
}

/**
* test normalizeObjectArray
*
* @return void
*/
function testnormalizeObjectArray() {
$helpers = array(
'Html',
'Foo.Bar' => array('one', 'two'),
'Something',
'Banana.Apple' => array('foo' => 'bar')
);
$result = ObjectCollection::normalizeObjectArray($helpers);
$expected = array(
'Html' => array('class' => 'Html', 'settings' => array()),
'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),
'Something' => array('class' => 'Something', 'settings' => array()),
'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')),
);
$this->assertEquals($expected, $result);
}
}

0 comments on commit fc33797

Please sign in to comment.