2010-07-02 02:47:20 +00: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)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.view
|
2010-07-04 17:14:04 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2010-07-02 02:47:20 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
App::import('Core', 'ObjectCollection');
|
|
|
|
|
|
|
|
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-02 03:58:43 +00:00
|
|
|
|
2010-07-02 02:47:20 +00:00
|
|
|
/**
|
|
|
|
* Loads/constructs a helper. Will return the instance in the registry if it already exists.
|
2010-11-06 18:58:27 +00:00
|
|
|
* By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
|
2010-11-07 04:07:27 +00:00
|
|
|
* can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
|
2010-11-06 18:58:27 +00:00
|
|
|
* declaring $helpers arrays you can disable callbacks on helpers.
|
2011-01-10 02:27:53 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2010-07-02 02:47:20 +00: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-07 03:48:27 +00:00
|
|
|
public function load($helper, $settings = array()) {
|
2010-07-02 02:47:20 +00:00
|
|
|
list($plugin, $name) = pluginSplit($helper, true);
|
|
|
|
|
2011-01-10 02:27:53 +00:00
|
|
|
$alias = $name;
|
2011-01-14 02:04:06 +00:00
|
|
|
if (isset($settings['className'])) {
|
|
|
|
$name = $settings['className'];
|
2011-01-10 02:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->_loaded[$alias])) {
|
|
|
|
return $this->_loaded[$alias];
|
2010-07-02 02:47:20 +00:00
|
|
|
}
|
|
|
|
$helperClass = $name . 'Helper';
|
|
|
|
if (!class_exists($helperClass)) {
|
|
|
|
if (!App::import('Helper', $helper)) {
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingHelperFileException(array(
|
|
|
|
'class' => $helperClass,
|
|
|
|
'file' => Inflector::underscore($name) . '.php'
|
|
|
|
));
|
2010-07-02 02:47:20 +00:00
|
|
|
}
|
|
|
|
if (!class_exists($helperClass)) {
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingHelperClassException(array(
|
|
|
|
'class' => $helperClass,
|
|
|
|
'file' => Inflector::underscore($name) . '.php'
|
|
|
|
));
|
2010-07-02 02:47:20 +00:00
|
|
|
}
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
2011-01-10 02:27:53 +00:00
|
|
|
$this->_loaded[$alias] = new $helperClass($this->_View, $settings);
|
2010-07-04 04:05:46 +00:00
|
|
|
|
2010-09-11 03:55:42 +00:00
|
|
|
$vars = array('request', 'theme', 'plugin');
|
2010-07-04 04:05:46 +00:00
|
|
|
foreach ($vars as $var) {
|
2011-01-10 02:27:53 +00:00
|
|
|
$this->_loaded[$alias]->{$var} = $this->_View->{$var};
|
2010-07-04 04:05:46 +00:00
|
|
|
}
|
2010-11-07 04:07:27 +00:00
|
|
|
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
|
2010-07-04 05:09:39 +00:00
|
|
|
if ($enable === true) {
|
2011-01-10 02:27:53 +00:00
|
|
|
$this->_enabled[] = $alias;
|
2010-07-03 03:15:48 +00:00
|
|
|
}
|
2011-01-10 02:27:53 +00:00
|
|
|
return $this->_loaded[$alias];
|
2010-07-02 02:47:20 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 01:37:25 +00:00
|
|
|
}
|