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)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-02 02:47:20 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-02 02:47:20 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.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)
|
|
|
|
*/
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('ObjectCollection', 'Utility');
|
2010-07-02 02:47:20 +00:00
|
|
|
|
|
|
|
class HelperCollection extends ObjectCollection {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View object to use when making helpers.
|
|
|
|
*
|
|
|
|
* @var View
|
|
|
|
*/
|
|
|
|
protected $_View;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param View $view
|
2010-07-02 02:47:20 +00:00
|
|
|
*/
|
|
|
|
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.
|
2011-08-16 03:55:08 +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
|
|
|
*
|
2011-01-14 02:06:32 +00:00
|
|
|
* You can alias your helper as an existing helper by setting the 'className' key, i.e.,
|
2011-01-10 02:27:53 +00:00
|
|
|
* {{{
|
2011-02-05 10:54:38 +00:00
|
|
|
* public $helpers = array(
|
2011-01-14 02:06:32 +00:00
|
|
|
* 'Html' => array(
|
|
|
|
* 'className' => 'AliasedHtml'
|
2011-01-10 02:27:53 +00:00
|
|
|
* );
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
* All calls to the `Html` helper would use `AliasedHtml` instead.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
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.
|
2011-10-15 18:06:31 +00:00
|
|
|
* @throws MissingHelperException when the helper could not be found
|
2010-07-02 02:47:20 +00:00
|
|
|
*/
|
2010-11-07 03:48:27 +00:00
|
|
|
public function load($helper, $settings = array()) {
|
2011-01-19 00:33:05 +00:00
|
|
|
if (is_array($settings) && isset($settings['className'])) {
|
2011-01-15 01:44:33 +00:00
|
|
|
$alias = $helper;
|
|
|
|
$helper = $settings['className'];
|
|
|
|
}
|
2011-01-28 06:03:08 +00:00
|
|
|
list($plugin, $name) = pluginSplit($helper, true);
|
2011-01-15 01:44:33 +00:00
|
|
|
if (!isset($alias)) {
|
|
|
|
$alias = $name;
|
2011-01-10 02:27:53 +00:00
|
|
|
}
|
2011-08-16 03:55:08 +00:00
|
|
|
|
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';
|
2010-12-19 02:39:08 +00:00
|
|
|
App::uses($helperClass, $plugin . 'View/Helper');
|
2010-07-02 02:47:20 +00:00
|
|
|
if (!class_exists($helperClass)) {
|
2011-10-15 18:06:31 +00:00
|
|
|
throw new MissingHelperException(array(
|
2011-10-16 11:40:36 +00:00
|
|
|
'class' => $helperClass,
|
|
|
|
'plugin' => substr($plugin, 0, -1)
|
2010-12-04 18:10:24 +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
|
|
|
}
|