/lib/Cake/View/HelperCollection.php

https://github.com/nshahzad/CakePHP-Base · PHP · 155 lines · 56 code · 9 blank · 90 comment · 7 complexity · 9fb6a0bf7d7537fd19a791d1bcf53548 MD5 · raw file

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