HelperCollection.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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-2012, 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-2012, 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. App::uses('ObjectCollection', 'Utility');
  19. App::uses('CakeEventListener', 'Event');
  20. /**
  21. * Helpers collection is used as a registry for loaded helpers and handles loading
  22. * and constructing helper class objects.
  23. *
  24. * @package Cake.View
  25. */
  26. class HelperCollection extends ObjectCollection implements CakeEventListener {
  27. /**
  28. * View object to use when making helpers.
  29. *
  30. * @var View
  31. */
  32. protected $_View;
  33. /**
  34. * Constructor
  35. *
  36. * @param View $view
  37. */
  38. public function __construct(View $view) {
  39. $this->_View = $view;
  40. }
  41. /**
  42. * Tries to lazy load a helper based on its name, if it cannot be found
  43. * in the application folder, then it tries looking under the current plugin
  44. * if any
  45. *
  46. * @param string $helper The helper name to be loaded
  47. * @return boolean wheter the helper could be loaded or not
  48. * @throws MissingHelperException When a helper could not be found.
  49. * App helpers are searched, and then plugin helpers.
  50. */
  51. public function __isset($helper) {
  52. if (parent::__isset($helper)) {
  53. return true;
  54. }
  55. try {
  56. $this->load($helper);
  57. } catch (MissingHelperException $exception) {
  58. if ($this->_View->plugin) {
  59. $this->load($this->_View->plugin . '.' . $helper);
  60. return true;
  61. }
  62. }
  63. if (!empty($exception)) {
  64. throw $exception;
  65. }
  66. return true;
  67. }
  68. /**
  69. * Provide public read access to the loaded objects
  70. *
  71. * @param string $name Name of property to read
  72. * @return mixed
  73. */
  74. public function __get($name) {
  75. if ($result = parent::__get($name)) {
  76. return $result;
  77. }
  78. if ($this->__isset($name)) {
  79. return $this->_loaded[$name];
  80. }
  81. return null;
  82. }
  83. /**
  84. * Loads/constructs a helper. Will return the instance in the registry if it already exists.
  85. * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
  86. * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
  87. * declaring $helpers arrays you can disable callbacks on helpers.
  88. *
  89. * You can alias your helper as an existing helper by setting the 'className' key, i.e.,
  90. * {{{
  91. * public $helpers = array(
  92. * 'Html' => array(
  93. * 'className' => 'AliasedHtml'
  94. * );
  95. * );
  96. * }}}
  97. * All calls to the `Html` helper would use `AliasedHtml` instead.
  98. *
  99. * @param string $helper Helper name to load
  100. * @param array $settings Settings for the helper.
  101. * @return Helper A helper object, Either the existing loaded helper or a new one.
  102. * @throws MissingHelperException when the helper could not be found
  103. */
  104. public function load($helper, $settings = array()) {
  105. if (is_array($settings) && isset($settings['className'])) {
  106. $alias = $helper;
  107. $helper = $settings['className'];
  108. }
  109. list($plugin, $name) = pluginSplit($helper, true);
  110. if (!isset($alias)) {
  111. $alias = $name;
  112. }
  113. if (isset($this->_loaded[$alias])) {
  114. return $this->_loaded[$alias];
  115. }
  116. $helperClass = $name . 'Helper';
  117. App::uses($helperClass, $plugin . 'View/Helper');
  118. if (!class_exists($helperClass)) {
  119. throw new MissingHelperException(array(
  120. 'class' => $helperClass,
  121. 'plugin' => substr($plugin, 0, -1)
  122. ));
  123. }
  124. $this->_loaded[$alias] = new $helperClass($this->_View, $settings);
  125. $vars = array('request', 'theme', 'plugin');
  126. foreach ($vars as $var) {
  127. $this->_loaded[$alias]->{$var} = $this->_View->{$var};
  128. }
  129. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  130. if ($enable) {
  131. $this->enable($alias);
  132. }
  133. return $this->_loaded[$alias];
  134. }
  135. /**
  136. * Returns a list of all events that will fire in the View during it's lifecycle.
  137. *
  138. * @return array
  139. */
  140. public function implementedEvents() {
  141. return array(
  142. 'View.beforeRenderFile' => 'trigger',
  143. 'View.afterRenderFile' => 'trigger',
  144. 'View.beforeRender' => 'trigger',
  145. 'View.afterRender' => 'trigger',
  146. 'View.beforeLayout' => 'trigger',
  147. 'View.afterLayout' => 'trigger'
  148. );
  149. }
  150. /**
  151. * Trigger a callback method on every object in the collection.
  152. * Used to trigger methods on objects in the collection. Will fire the methods in the
  153. * order they were attached.
  154. *
  155. * ### Options
  156. *
  157. * - `breakOn` Set to the value or values you want the callback propagation to stop on.
  158. * Can either be a scalar value, or an array of values to break on. Defaults to `false`.
  159. *
  160. * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
  161. * will be returned. If used in combination with `collectReturn` the collected results will be returned.
  162. * Defaults to `false`.
  163. *
  164. * - `collectReturn` Set to true to collect the return of each object into an array.
  165. * This array of return values will be returned from the trigger() call. Defaults to `false`.
  166. *
  167. * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
  168. * Setting modParams to an integer value will allow you to modify the parameter with that index.
  169. * Any non-null value will modify the parameter index indicated.
  170. * Defaults to false.
  171. *
  172. *
  173. * @param string $callback|CakeEvent Method to fire on all the objects. Its assumed all the objects implement
  174. * the method you are calling. If an instance of CakeEvent is provided, then then Event name will parsed to
  175. * get the callback name. This is done by getting the last word after any dot in the event name
  176. * (eg. `Model.afterSave` event will trigger the `afterSave` callback)
  177. * @param array $params Array of parameters for the triggered callback.
  178. * @param array $options Array of options.
  179. * @return mixed Either the last result or all results if collectReturn is on.
  180. * @throws CakeException when modParams is used with an index that does not exist.
  181. */
  182. public function trigger($callback, $params = array(), $options = array()) {
  183. if ($callback instanceof CakeEvent) {
  184. $callback->omitSubject = true;
  185. }
  186. return parent::trigger($callback, $params, $options);
  187. }
  188. }