1: <?php
2: /**
3: * Helpers collection is used as a registry for loaded helpers and handles loading
4: * and constructing helper class objects.
5: *
6: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
7: * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
8: *
9: * Licensed under The MIT License
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @package Cake.View
15: * @since CakePHP(tm) v 2.0
16: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
17: */
18:
19: App::uses('ObjectCollection', 'Utility');
20:
21: /**
22: * Helpers collection is used as a registry for loaded helpers and handles loading
23: * and constructing helper class objects.
24: *
25: * @package Cake.View
26: */
27: class HelperCollection extends ObjectCollection {
28:
29: /**
30: * View object to use when making helpers.
31: *
32: * @var View
33: */
34: protected $_View;
35:
36: /**
37: * Constructor
38: *
39: * @param View $view
40: */
41: public function __construct(View $view) {
42: $this->_View = $view;
43: }
44:
45: /**
46: * Loads/constructs a helper. Will return the instance in the registry if it already exists.
47: * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
48: * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
49: * declaring $helpers arrays you can disable callbacks on helpers.
50: *
51: * You can alias your helper as an existing helper by setting the 'className' key, i.e.,
52: * {{{
53: * public $helpers = array(
54: * 'Html' => array(
55: * 'className' => 'AliasedHtml'
56: * );
57: * );
58: * }}}
59: * All calls to the `Html` helper would use `AliasedHtml` instead.
60: *
61: * @param string $helper Helper name to load
62: * @param array $settings Settings for the helper.
63: * @return Helper A helper object, Either the existing loaded helper or a new one.
64: * @throws MissingHelperException when the helper could not be found
65: */
66: public function load($helper, $settings = array()) {
67: if (is_array($settings) && isset($settings['className'])) {
68: $alias = $helper;
69: $helper = $settings['className'];
70: }
71: list($plugin, $name) = pluginSplit($helper, true);
72: if (!isset($alias)) {
73: $alias = $name;
74: }
75:
76: if (isset($this->_loaded[$alias])) {
77: return $this->_loaded[$alias];
78: }
79: $helperClass = $name . 'Helper';
80: App::uses($helperClass, $plugin . 'View/Helper');
81: if (!class_exists($helperClass)) {
82: throw new MissingHelperException(array(
83: 'class' => $helperClass,
84: 'plugin' => substr($plugin, 0, -1)
85: ));
86: }
87: $this->_loaded[$alias] = new $helperClass($this->_View, $settings);
88:
89: $vars = array('request', 'theme', 'plugin');
90: foreach ($vars as $var) {
91: $this->_loaded[$alias]->{$var} = $this->_View->{$var};
92: }
93: $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
94: if ($enable === true) {
95: $this->_enabled[] = $alias;
96: }
97: return $this->_loaded[$alias];
98: }
99:
100: }