HelperCollection.php 6.6 KB

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