HelperCollection.php 6.5 KB

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