Skip to content

Commit

Permalink
Allowed helpers to be aliased by setting the 'alias' key
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Jan 10, 2011
1 parent 1f1d920 commit 9749dc8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
27 changes: 21 additions & 6 deletions cake/libs/view/helper_collection.php
Expand Up @@ -40,6 +40,16 @@ public function __construct(View $view) {
* By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
* can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
* declaring $helpers arrays you can disable callbacks on helpers.
*
* You can alias your helper as an existing helper by setting the 'alias' key, i.e.,
* {{{
* public $components = array(
* 'AliasedHtml' => array(
* 'alias' => 'Html'
* );
* );
* }}}
* All calls to the `Html` helper would use `AliasedHtml` instead.
*
* @param string $helper Helper name to load
* @param array $settings Settings for the helper.
Expand All @@ -49,8 +59,13 @@ public function __construct(View $view) {
public function load($helper, $settings = array()) {
list($plugin, $name) = pluginSplit($helper, true);

if (isset($this->_loaded[$name])) {
return $this->_loaded[$name];
$alias = $name;
if (isset($settings['alias'])) {
$alias = $settings['alias'];
}

if (isset($this->_loaded[$alias])) {
return $this->_loaded[$alias];
}
$helperClass = $name . 'Helper';
if (!class_exists($helperClass)) {
Expand All @@ -67,17 +82,17 @@ public function load($helper, $settings = array()) {
));
}
}
$this->_loaded[$name] = new $helperClass($this->_View, $settings);
$this->_loaded[$alias] = new $helperClass($this->_View, $settings);

$vars = array('request', 'theme', 'plugin');
foreach ($vars as $var) {
$this->_loaded[$name]->{$var} = $this->_View->{$var};
$this->_loaded[$alias]->{$var} = $this->_View->{$var};
}
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
if ($enable === true) {
$this->_enabled[] = $name;
$this->_enabled[] = $alias;
}
return $this->_loaded[$name];
return $this->_loaded[$alias];
}

}
26 changes: 26 additions & 0 deletions cake/tests/cases/libs/view/helper_collection.test.php
Expand Up @@ -20,6 +20,13 @@
App::import('Core', 'HelperCollection');
App::import('View', 'View');

/**
* Extended HtmlHelper
*/
App::import('Helper', 'Html');
class HtmlAliasHelper extends HtmlHelper {
}

class HelperCollectionTest extends CakeTestCase {
/**
* setup
Expand Down Expand Up @@ -56,6 +63,25 @@ function testLoad() {
$this->assertTrue($this->Helpers->enabled('Html'));
}

/**
* Tests loading as an alias
*
* @return void
*/
function testLoadWithAlias() {
$result = $this->Helpers->load('HtmlAlias', array('alias' => 'Html'));
$this->assertInstanceOf('HtmlAliasHelper', $result);
$this->assertInstanceOf('HtmlAliasHelper', $this->Helpers->Html);

$result = $this->Helpers->attached();
$this->assertEquals(array('Html'), $result, 'attached() results are wrong.');

$this->assertTrue($this->Helpers->enabled('Html'));

$result = $this->Helpers->load('Html');
$this->assertInstanceOf('HtmlAliasHelper', $result);
}

/**
* test that the enabled setting disables the helper.
*
Expand Down

0 comments on commit 9749dc8

Please sign in to comment.