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.
|
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);
|
|
|
|
|
2010-07-04 04:29:28 +00:00
|
|
|
if (isset($this->_loaded[$name])) {
|
|
|
|
return $this->_loaded[$name];
|
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
|
|
|
}
|
2010-07-04 04:29:28 +00:00
|
|
|
$this->_loaded[$name] = 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) {
|
2010-07-04 04:29:28 +00:00
|
|
|
$this->_loaded[$name]->{$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) {
|
|
|
|
$this->_enabled[] = $name;
|
2010-07-03 03:15:48 +00:00
|
|
|
}
|
2010-07-04 04:29:28 +00:00
|
|
|
return $this->_loaded[$name];
|
2010-07-02 02:47:20 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 01:37:25 +00:00
|
|
|
}
|