Helper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 0.2.9
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\View;
  17. use Cake\Core\InstanceConfigTrait;
  18. use Cake\Event\EventListenerInterface;
  19. /**
  20. * Abstract base class for all other Helpers in CakePHP.
  21. * Provides common methods and features.
  22. *
  23. * ### Callback methods
  24. *
  25. * Helpers support a number of callback methods. These callbacks allow you to hook into
  26. * the various view lifecycle events and either modify existing view content or perform
  27. * other application specific logic. The events are not implemented by this base class, as
  28. * implementing a callback method subscribes a helper to the related event. The callback methods
  29. * are as follows:
  30. *
  31. * - `beforeRender(EventInterface $event, $viewFile)` - beforeRender is called before the view file is rendered.
  32. * - `afterRender(EventInterface $event, $viewFile)` - afterRender is called after the view file is rendered
  33. * but before the layout has been rendered.
  34. * - beforeLayout(EventInterface $event, $layoutFile)` - beforeLayout is called before the layout is rendered.
  35. * - `afterLayout(EventInterface $event, $layoutFile)` - afterLayout is called after the layout has rendered.
  36. * - `beforeRenderFile(EventInterface $event, $viewFile)` - Called before any view fragment is rendered.
  37. * - `afterRenderFile(EventInterface $event, $viewFile, $content)` - Called after any view fragment is rendered.
  38. * If a listener returns a non-null value, the output of the rendered file will be set to that.
  39. */
  40. class Helper implements EventListenerInterface
  41. {
  42. use InstanceConfigTrait;
  43. /**
  44. * List of helpers used by this helper
  45. *
  46. * @var array
  47. */
  48. protected $helpers = [];
  49. /**
  50. * Default config for this helper.
  51. *
  52. * @var array<string, mixed>
  53. */
  54. protected $_defaultConfig = [];
  55. /**
  56. * A helper lookup table used to lazy load helper objects.
  57. *
  58. * @var array
  59. */
  60. protected $_helperMap = [];
  61. /**
  62. * The View instance this helper is attached to
  63. *
  64. * @var \Cake\View\View
  65. */
  66. protected $_View;
  67. /**
  68. * Default Constructor
  69. *
  70. * @param \Cake\View\View $view The View this helper is being attached to.
  71. * @param array<string, mixed> $config Configuration settings for the helper.
  72. */
  73. public function __construct(View $view, array $config = [])
  74. {
  75. $this->_View = $view;
  76. $this->setConfig($config);
  77. if (!empty($this->helpers)) {
  78. $this->_helperMap = $view->helpers()->normalizeArray($this->helpers);
  79. }
  80. $this->initialize($config);
  81. }
  82. /**
  83. * Provide non fatal errors on missing method calls.
  84. *
  85. * @param string $method Method to invoke
  86. * @param array $params Array of params for the method.
  87. * @return mixed|void
  88. */
  89. public function __call(string $method, array $params)
  90. {
  91. trigger_error(sprintf('Method %1$s::%2$s does not exist', static::class, $method), E_USER_WARNING);
  92. }
  93. /**
  94. * Lazy loads helpers.
  95. *
  96. * @param string $name Name of the property being accessed.
  97. * @return \Cake\View\Helper|null|void Helper instance if helper with provided name exists
  98. */
  99. public function __get(string $name)
  100. {
  101. if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
  102. $config = ['enabled' => false] + (array)$this->_helperMap[$name]['config'];
  103. $this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $config);
  104. return $this->{$name};
  105. }
  106. }
  107. /**
  108. * Get the view instance this helper is bound to.
  109. *
  110. * @return \Cake\View\View The bound view instance.
  111. */
  112. public function getView(): View
  113. {
  114. return $this->_View;
  115. }
  116. /**
  117. * Returns a string to be used as onclick handler for confirm dialogs.
  118. *
  119. * @param string $okCode Code to be executed after user chose 'OK'
  120. * @param string $cancelCode Code to be executed after user chose 'Cancel'
  121. * @return string "onclick" JS code
  122. */
  123. protected function _confirm(string $okCode, string $cancelCode): string
  124. {
  125. return "if (confirm(this.dataset.confirmMessage)) { {$okCode} } {$cancelCode}";
  126. }
  127. /**
  128. * Adds the given class to the element options
  129. *
  130. * @param array<string, mixed> $options Array options/attributes to add a class to
  131. * @param string $class The class name being added.
  132. * @param string $key the key to use for class. Defaults to `'class'`.
  133. * @return array<string, mixed> Array of options with $key set.
  134. */
  135. public function addClass(array $options, string $class, string $key = 'class'): array
  136. {
  137. if (isset($options[$key]) && is_array($options[$key])) {
  138. $options[$key][] = $class;
  139. } elseif (isset($options[$key]) && trim($options[$key])) {
  140. $options[$key] .= ' ' . $class;
  141. } else {
  142. $options[$key] = $class;
  143. }
  144. return $options;
  145. }
  146. /**
  147. * Get the View callbacks this helper is interested in.
  148. *
  149. * By defining one of the callback methods a helper is assumed
  150. * to be interested in the related event.
  151. *
  152. * Override this method if you need to add non-conventional event listeners.
  153. * Or if you want helpers to listen to non-standard events.
  154. *
  155. * @return array<string, mixed>
  156. */
  157. public function implementedEvents(): array
  158. {
  159. $eventMap = [
  160. 'View.beforeRenderFile' => 'beforeRenderFile',
  161. 'View.afterRenderFile' => 'afterRenderFile',
  162. 'View.beforeRender' => 'beforeRender',
  163. 'View.afterRender' => 'afterRender',
  164. 'View.beforeLayout' => 'beforeLayout',
  165. 'View.afterLayout' => 'afterLayout',
  166. ];
  167. $events = [];
  168. foreach ($eventMap as $event => $method) {
  169. if (method_exists($this, $method)) {
  170. $events[$event] = $method;
  171. }
  172. }
  173. return $events;
  174. }
  175. /**
  176. * Constructor hook method.
  177. *
  178. * Implement this method to avoid having to overwrite the constructor and call parent.
  179. *
  180. * @param array<string, mixed> $config The configuration settings provided to this helper.
  181. * @return void
  182. */
  183. public function initialize(array $config): void
  184. {
  185. }
  186. /**
  187. * Returns an array that can be used to describe the internal state of this
  188. * object.
  189. *
  190. * @return array<string, mixed>
  191. */
  192. public function __debugInfo(): array
  193. {
  194. return [
  195. 'helpers' => $this->helpers,
  196. 'implementedEvents' => $this->implementedEvents(),
  197. '_config' => $this->getConfig(),
  198. ];
  199. }
  200. }