Base for a static organization website

HelperCollection.php 6.5KB

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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.View
  16. * @since CakePHP(tm) v 2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. App::uses('ObjectCollection', 'Utility');
  20. App::uses('CakeEventListener', 'Event');
  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 implements CakeEventListener {
  28. /**
  29. * View object to use when making helpers.
  30. *
  31. * @var View
  32. */
  33. protected $_View;
  34. /**
  35. * Constructor
  36. *
  37. * @param View $view View instance.
  38. */
  39. public function __construct(View $view) {
  40. $this->_View = $view;
  41. }
  42. /**
  43. * Tries to lazy load a helper based on its name, if it cannot be found
  44. * in the application folder, then it tries looking under the current plugin
  45. * if any
  46. *
  47. * @param string $helper The helper name to be loaded
  48. * @return bool whether the helper could be loaded or not
  49. * @throws MissingHelperException When a helper could not be found.
  50. * App helpers are searched, and then plugin helpers.
  51. */
  52. public function __isset($helper) {
  53. if (parent::__isset($helper)) {
  54. return true;
  55. }
  56. try {
  57. $this->load($helper);
  58. } catch (MissingHelperException $exception) {
  59. if ($this->_View->plugin) {
  60. $this->load($this->_View->plugin . '.' . $helper);
  61. return true;
  62. }
  63. }
  64. if (!empty($exception)) {
  65. throw $exception;
  66. }
  67. return true;
  68. }
  69. /**
  70. * Provide public read access to the loaded objects
  71. *
  72. * @param string $name Name of property to read
  73. * @return mixed
  74. */
  75. public function __get($name) {
  76. if ($result = parent::__get($name)) {
  77. return $result;
  78. }
  79. if ($this->__isset($name)) {
  80. return $this->_loaded[$name];
  81. }
  82. return null;
  83. }
  84. /**
  85. * Loads/constructs a helper. Will return the instance in the registry if it already exists.
  86. * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
  87. * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
  88. * declaring $helpers arrays you can disable callbacks on helpers.
  89. *
  90. * You can alias your helper as an existing helper by setting the 'className' key, i.e.,
  91. * ```
  92. * public $helpers = array(
  93. * 'Html' => array(
  94. * 'className' => 'AliasedHtml'
  95. * );
  96. * );
  97. * ```
  98. * All calls to the `Html` helper would use `AliasedHtml` instead.
  99. *
  100. * @param string $helper Helper name to load
  101. * @param array $settings Settings for the helper.
  102. * @return Helper A helper object, Either the existing loaded helper or a new one.
  103. * @throws MissingHelperException when the helper could not be found
  104. */
  105. public function load($helper, $settings = array()) {
  106. if (isset($settings['className'])) {
  107. $alias = $helper;
  108. $helper = $settings['className'];
  109. }
  110. list($plugin, $name) = pluginSplit($helper, true);
  111. if (!isset($alias)) {
  112. $alias = $name;
  113. }
  114. if (isset($this->_loaded[$alias])) {
  115. return $this->_loaded[$alias];
  116. }
  117. $helperClass = $name . 'Helper';
  118. App::uses($helperClass, $plugin . 'View/Helper');
  119. if (!class_exists($helperClass)) {
  120. throw new MissingHelperException(array(
  121. 'class' => $helperClass,
  122. 'plugin' => substr($plugin, 0, -1)
  123. ));
  124. }
  125. $this->_loaded[$alias] = new $helperClass($this->_View, $settings);
  126. $vars = array('request', 'theme', 'plugin');
  127. foreach ($vars as $var) {
  128. $this->_loaded[$alias]->{$var} = $this->_View->{$var};
  129. }
  130. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  131. if ($enable) {
  132. $this->enable($alias);
  133. }
  134. return $this->_loaded[$alias];
  135. }
  136. /**
  137. * Returns a list of all events that will fire in the View during it's lifecycle.
  138. *
  139. * @return array
  140. */
  141. public function implementedEvents() {
  142. return array(
  143. 'View.beforeRenderFile' => 'trigger',
  144. 'View.afterRenderFile' => 'trigger',
  145. 'View.beforeRender' => 'trigger',
  146. 'View.afterRender' => 'trigger',
  147. 'View.beforeLayout' => 'trigger',
  148. 'View.afterLayout' => 'trigger'
  149. );
  150. }
  151. /**
  152. * Trigger a callback method on every object in the collection.
  153. * Used to trigger methods on objects in the collection. Will fire the methods in the
  154. * order they were attached.
  155. *
  156. * ### Options
  157. *
  158. * - `breakOn` Set to the value or values you want the callback propagation to stop on.
  159. * Can either be a scalar value, or an array of values to break on. Defaults to `false`.
  160. *
  161. * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
  162. * will be returned. If used in combination with `collectReturn` the collected results will be returned.
  163. * Defaults to `false`.
  164. *
  165. * - `collectReturn` Set to true to collect the return of each object into an array.
  166. * This array of return values will be returned from the trigger() call. Defaults to `false`.
  167. *
  168. * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
  169. * Setting modParams to an integer value will allow you to modify the parameter with that index.
  170. * Any non-null value will modify the parameter index indicated.
  171. * Defaults to false.
  172. *
  173. * @param string|CakeEvent $callback 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. }