HelperCollection.php 6.3 KB

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