Moving methods around. ObjectCollection now normalizes helpers arrays. Plugin helpers now lazy load.

Tests updated.
This commit is contained in:
mark_story 2010-07-03 11:36:53 -04:00
parent 98982a6f7a
commit fc3379767d
8 changed files with 89 additions and 22 deletions

View file

@ -151,4 +151,23 @@ abstract class ObjectCollection {
return $this->_attached; 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;
}
} }

View file

@ -38,7 +38,14 @@ class Helper extends Object {
* *
* @var array * @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 * Base URL
@ -143,11 +150,11 @@ class Helper extends Object {
private $__cleaned = null; private $__cleaned = null;
/** /**
* undocumented class variable * The View instance this helper is attached to
* *
* @var string * @var View
*/ */
public $View; protected $_View;
/** /**
* Default Constructor * Default Constructor
@ -156,8 +163,11 @@ class Helper extends Object {
* @param array $settings Configuration settings for the helper. * @param array $settings Configuration settings for the helper.
*/ */
public function __construct(View $View, $settings = array()) { public function __construct(View $View, $settings = array()) {
$this->View = $View; $this->_View = $View;
// Nothing to see here. $this->params = $View->params;
if (!empty($this->helpers)) {
$this->_helperMap = ObjectCollection::normalizeObjectArray($this->helpers);
}
} }
/** /**
@ -174,8 +184,12 @@ class Helper extends Object {
* @return void * @return void
*/ */
public function __get($name) { public function __get($name) {
if (!empty($this->helpers) && in_array($name, $this->helpers)) { if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
$this->{$name} = $this->View->Helpers->load($name, array(), false); $this->{$name} = $this->_View->loadHelper(
$this->_helperMap[$name]['class'], $this->_helperMap[$name]['settings'], false
);
}
if (isset($this->{$name})) {
return $this->{$name}; return $this->{$name};
} }
} }

View file

@ -98,7 +98,7 @@ class JsHelper extends AppHelper {
$this->__engineName = $className . 'Engine'; $this->__engineName = $className . 'Engine';
$engineClass = $engineName . 'Engine'; $engineClass = $engineName . 'Engine';
$this->helpers[] = $engineClass; $this->helpers[] = $engineClass;
parent::__construct(); parent::__construct($View, $settings);
} }
/** /**

View file

@ -90,6 +90,7 @@ class PaginatorHelper extends AppHelper {
* @return void * @return void
*/ */
function __construct(View $View, $settings = array()) { function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
$ajaxProvider = isset($settings['ajax']) ? $settings['ajax'] : 'Js'; $ajaxProvider = isset($settings['ajax']) ? $settings['ajax'] : 'Js';
$this->helpers[] = $ajaxProvider; $this->helpers[] = $ajaxProvider;
$this->_ajaxHelperClass = $ajaxProvider; $this->_ajaxHelperClass = $ajaxProvider;

View file

@ -44,7 +44,7 @@ class XmlHelper extends AppHelper {
* @return void * @return void
*/ */
function __construct(View $View, $settings = array()) { function __construct(View $View, $settings = array()) {
parent::__construct(); parent::__construct($View, $settings);
$this->Xml =& new Xml(); $this->Xml =& new Xml();
$this->Xml->options(array('verifyNs' => false)); $this->Xml->options(array('verifyNs' => false));
} }

View file

@ -736,8 +736,8 @@ class View extends Object {
* @param array $settings Settings for the helper * @param array $settings Settings for the helper
* @return Helper a constructed helper object. * @return Helper a constructed helper object.
*/ */
public function loadHelper($helperName, $settings = array()) { public function loadHelper($helperName, $settings = array(), $attach = true) {
return $this->Helpers->load($helperName, $settings, true); return $this->Helpers->load($helperName, $settings, $attach);
} }
/** /**

View file

@ -163,6 +163,13 @@ class HelperTestPostsTag extends Model {
} }
class TestHelper extends Helper { class TestHelper extends Helper {
/**
* helpers for this helper.
*
* @var string
*/
var $helpers = array('Html', 'TestPlugin.OtherHelper');
/** /**
* expose a method as public * expose a method as public
* *
@ -790,8 +797,12 @@ class HelperTest extends CakeTestCase {
* @return void * @return void
*/ */
function testLazyLoadingHelpers() { function testLazyLoadingHelpers() {
$this->Helper->helpers = array('Test', 'Html'); App::build(array(
$result = $this->Helper->Test; 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
$this->assertType('TestHelper', $result); ));
$Helper = new TestHelper($this->View);
$this->assertType('OtherHelperHelper', $Helper->OtherHelper);
$this->assertType('HtmlHelper', $Helper->Html);
App::build();
} }
} }

View file

@ -28,8 +28,8 @@ class HelperCollectionTest extends CakeTestCase {
* @return void * @return void
*/ */
function setup() { function setup() {
$View = $this->getMock('View', array(), array(null)); $this->View = $this->getMock('View', array(), array(null));
$this->Helpers = new HelperCollection($View); $this->Helpers = new HelperCollection($this->View);
} }
/** /**
@ -38,7 +38,7 @@ class HelperCollectionTest extends CakeTestCase {
* @return void * @return void
*/ */
function teardown() { function teardown() {
unset($this->Helpers); unset($this->Helpers, $this->View);
} }
/** /**
@ -124,8 +124,8 @@ class HelperCollectionTest extends CakeTestCase {
$this->Helpers->load('Form'); $this->Helpers->load('Form');
$this->Helpers->load('Html'); $this->Helpers->load('Html');
$this->Helpers->Html = $this->getMock('HtmlHelper'); $this->Helpers->Html = $this->getMock('HtmlHelper', array(), array($this->View));
$this->Helpers->Form = $this->getMock('FormHelper'); $this->Helpers->Form = $this->getMock('FormHelper', array(), array($this->View));
$this->Helpers->Html->expects($this->once())->method('beforeRender') $this->Helpers->Html->expects($this->once())->method('beforeRender')
->with('one', 'two'); ->with('one', 'two');
@ -144,8 +144,8 @@ class HelperCollectionTest extends CakeTestCase {
$this->Helpers->load('Form'); $this->Helpers->load('Form');
$this->Helpers->load('Html'); $this->Helpers->load('Html');
$this->Helpers->Html = $this->getMock('HtmlHelper'); $this->Helpers->Html = $this->getMock('HtmlHelper', array(), array($this->View));
$this->Helpers->Form = $this->getMock('FormHelper'); $this->Helpers->Form = $this->getMock('FormHelper', array(), array($this->View));
$this->Helpers->Html->expects($this->once())->method('beforeRender') $this->Helpers->Html->expects($this->once())->method('beforeRender')
->with('one', 'two'); ->with('one', 'two');
@ -155,4 +155,26 @@ class HelperCollectionTest extends CakeTestCase {
$this->Helpers->trigger('beforeRender', array('one', 'two')); $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);
}
} }