cakephp2-php8/lib/Cake/View/HelperCollection.php

94 lines
2.8 KiB
PHP
Raw Normal View History

<?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)
*
* 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)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
2011-08-16 03:55:08 +00:00
App::uses('ObjectCollection', 'Utility');
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
*/
public function __construct(View $view) {
$this->_View = $view;
}
/**
* 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
* can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
* declaring $helpers arrays you can disable callbacks on helpers.
*
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-02-05 10:54:38 +00:00
* public $helpers = array(
2011-01-14 02:06:32 +00:00
* 'Html' => array(
* 'className' => 'AliasedHtml'
* );
* );
* }}}
* All calls to the `Html` helper would use `AliasedHtml` instead.
2011-08-16 03:55:08 +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
*/
public function load($helper, $settings = array()) {
if (is_array($settings) && isset($settings['className'])) {
$alias = $helper;
$helper = $settings['className'];
}
list($plugin, $name) = pluginSplit($helper, true);
if (!isset($alias)) {
$alias = $name;
}
2011-08-16 03:55:08 +00:00
if (isset($this->_loaded[$alias])) {
return $this->_loaded[$alias];
}
$helperClass = $name . 'Helper';
App::uses($helperClass, $plugin . 'View/Helper');
if (!class_exists($helperClass)) {
2010-12-04 18:10:24 +00:00
throw new MissingHelperClassException(array(
'class' => $helperClass,
'file' => $helperClass . '.php'
2010-12-04 18:10:24 +00:00
));
}
$this->_loaded[$alias] = new $helperClass($this->_View, $settings);
$vars = array('request', 'theme', 'plugin');
foreach ($vars as $var) {
$this->_loaded[$alias]->{$var} = $this->_View->{$var};
}
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
if ($enable === true) {
$this->_enabled[] = $alias;
}
return $this->_loaded[$alias];
}
}