2010-07-01 22:47:20 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Helpers collection is used as a registry for loaded helpers and handles loading
|
|
|
|
* and constructing helper class objects.
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 17:31:39 -04:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-01 22:47:20 -04:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 17:31:39 -04:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-01 22:47:20 -04:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 13:57:20 -05:00
|
|
|
* @package cake.libs.view
|
2010-07-04 13:14:04 -04:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2010-07-01 22:47:20 -04:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-02-13 23:10:19 -04:30
|
|
|
|
2010-12-11 01:17:55 -04:30
|
|
|
App::uses('ObjectCollection', 'Utility');
|
2010-07-01 22:47:20 -04:00
|
|
|
|
|
|
|
class HelperCollection extends ObjectCollection {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View object to use when making helpers.
|
|
|
|
*
|
|
|
|
* @var View
|
|
|
|
*/
|
|
|
|
protected $_View;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(View $view) {
|
|
|
|
$this->_View = $view;
|
|
|
|
}
|
2010-07-01 23:58:43 -04:00
|
|
|
|
2010-07-01 22:47:20 -04:00
|
|
|
/**
|
|
|
|
* Loads/constructs a helper. Will return the instance in the registry if it already exists.
|
2010-11-06 14:58:27 -04:00
|
|
|
* By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
|
2010-11-07 00:07:27 -04:00
|
|
|
* can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
|
2010-11-06 14:58:27 -04:00
|
|
|
* declaring $helpers arrays you can disable callbacks on helpers.
|
2011-01-09 18:27:53 -08:00
|
|
|
*
|
2011-01-13 18:06:32 -08:00
|
|
|
* You can alias your helper as an existing helper by setting the 'className' key, i.e.,
|
2011-01-09 18:27:53 -08:00
|
|
|
* {{{
|
2011-02-05 05:54:38 -05:00
|
|
|
* public $helpers = array(
|
2011-01-13 18:06:32 -08:00
|
|
|
* 'Html' => array(
|
|
|
|
* 'className' => 'AliasedHtml'
|
2011-01-09 18:27:53 -08:00
|
|
|
* );
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
* All calls to the `Html` helper would use `AliasedHtml` instead.
|
2010-07-01 22:47:20 -04:00
|
|
|
*
|
|
|
|
* @param string $helper Helper name to load
|
|
|
|
* @param array $settings Settings for the helper.
|
|
|
|
* @return Helper A helper object, Either the existing loaded helper or a new one.
|
|
|
|
* @throws MissingHelperFileException, MissingHelperClassException when the helper could not be found
|
|
|
|
*/
|
2010-11-06 23:48:27 -04:00
|
|
|
public function load($helper, $settings = array()) {
|
2011-01-18 19:33:05 -05:00
|
|
|
if (is_array($settings) && isset($settings['className'])) {
|
2011-01-14 17:44:33 -08:00
|
|
|
$alias = $helper;
|
|
|
|
$helper = $settings['className'];
|
|
|
|
}
|
2011-01-28 01:33:08 -04:30
|
|
|
list($plugin, $name) = pluginSplit($helper, true);
|
2011-01-14 17:44:33 -08:00
|
|
|
if (!isset($alias)) {
|
|
|
|
$alias = $name;
|
2011-01-09 18:27:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->_loaded[$alias])) {
|
|
|
|
return $this->_loaded[$alias];
|
2010-07-01 22:47:20 -04:00
|
|
|
}
|
|
|
|
$helperClass = $name . 'Helper';
|
2010-12-18 22:09:08 -04:30
|
|
|
App::uses($helperClass, $plugin . 'View/Helper');
|
2010-07-01 22:47:20 -04:00
|
|
|
if (!class_exists($helperClass)) {
|
2010-12-04 13:40:24 -04:30
|
|
|
throw new MissingHelperClassException(array(
|
|
|
|
'class' => $helperClass,
|
2010-12-18 22:09:08 -04:30
|
|
|
'file' => $helperClass . '.php'
|
2010-12-04 13:40:24 -04:30
|
|
|
));
|
2010-07-01 23:00:48 -04:00
|
|
|
}
|
2011-01-09 18:27:53 -08:00
|
|
|
$this->_loaded[$alias] = new $helperClass($this->_View, $settings);
|
2010-07-04 00:05:46 -04:00
|
|
|
|
2010-09-10 23:55:42 -04:00
|
|
|
$vars = array('request', 'theme', 'plugin');
|
2010-07-04 00:05:46 -04:00
|
|
|
foreach ($vars as $var) {
|
2011-01-09 18:27:53 -08:00
|
|
|
$this->_loaded[$alias]->{$var} = $this->_View->{$var};
|
2010-07-04 00:05:46 -04:00
|
|
|
}
|
2010-11-07 00:07:27 -04:00
|
|
|
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
|
2010-07-04 01:09:39 -04:00
|
|
|
if ($enable === true) {
|
2011-01-09 18:27:53 -08:00
|
|
|
$this->_enabled[] = $alias;
|
2010-07-02 23:15:48 -04:00
|
|
|
}
|
2011-01-09 18:27:53 -08:00
|
|
|
return $this->_loaded[$alias];
|
2010-07-01 22:47:20 -04:00
|
|
|
}
|
|
|
|
|
2010-08-29 21:37:25 -04:00
|
|
|
}
|