FlashComponent.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Controller\Component;
  17. use Cake\Controller\Component;
  18. use Cake\Controller\ComponentRegistry;
  19. use Cake\Http\Exception\InternalErrorException;
  20. use Cake\Utility\Inflector;
  21. use Exception;
  22. /**
  23. * The CakePHP FlashComponent provides a way for you to write a flash variable
  24. * to the session from your controllers, to be rendered in a view with the
  25. * FlashHelper.
  26. *
  27. * @method void success(string $message, array $options = []) Set a message using "success" element
  28. * @method void error(string $message, array $options = []) Set a message using "error" element
  29. */
  30. class FlashComponent extends Component
  31. {
  32. /**
  33. * The Session object instance
  34. *
  35. * @var \Cake\Http\Session
  36. */
  37. protected $_session;
  38. /**
  39. * Default configuration
  40. *
  41. * @var array
  42. */
  43. protected $_defaultConfig = [
  44. 'key' => 'flash',
  45. 'element' => 'default',
  46. 'params' => [],
  47. 'clear' => false,
  48. 'duplicate' => true
  49. ];
  50. /**
  51. * Constructor
  52. *
  53. * @param \Cake\Controller\ComponentRegistry $registry A ComponentRegistry for this component
  54. * @param array $config Array of config.
  55. */
  56. public function __construct(ComponentRegistry $registry, array $config = [])
  57. {
  58. parent::__construct($registry, $config);
  59. $this->_session = $registry->getController()->getRequest()->getSession();
  60. }
  61. /**
  62. * Used to set a session variable that can be used to output messages in the view.
  63. * If you make consecutive calls to this method, the messages will stack (if they are
  64. * set with the same flash key)
  65. *
  66. * In your controller: $this->Flash->set('This has been saved');
  67. *
  68. * ### Options:
  69. *
  70. * - `key` The key to set under the session's Flash key
  71. * - `element` The element used to render the flash message. Default to 'default'.
  72. * - `params` An array of variables to make available when using an element
  73. * - `clear` A bool stating if the current stack should be cleared to start a new one
  74. * - `escape` Set to false to allow templates to print out HTML content
  75. *
  76. * @param string|\Exception $message Message to be flashed. If an instance
  77. * of \Exception the exception message will be used and code will be set
  78. * in params.
  79. * @param array $options An array of options
  80. * @return void
  81. */
  82. public function set($message, array $options = []): void
  83. {
  84. $options += $this->getConfig();
  85. if ($message instanceof Exception) {
  86. if (!isset($options['params']['code'])) {
  87. $options['params']['code'] = $message->getCode();
  88. }
  89. $message = $message->getMessage();
  90. }
  91. if (isset($options['escape']) && !isset($options['params']['escape'])) {
  92. $options['params']['escape'] = $options['escape'];
  93. }
  94. list($plugin, $element) = pluginSplit($options['element']);
  95. if ($plugin) {
  96. $options['element'] = $plugin . '.Flash/' . $element;
  97. } else {
  98. $options['element'] = 'Flash/' . $element;
  99. }
  100. $messages = [];
  101. if (!$options['clear']) {
  102. $messages = (array)$this->_session->read('Flash.' . $options['key']);
  103. }
  104. if (!$options['duplicate']) {
  105. foreach ($messages as $existingMessage) {
  106. if ($existingMessage['message'] === $message) {
  107. return;
  108. }
  109. }
  110. }
  111. $messages[] = [
  112. 'message' => $message,
  113. 'key' => $options['key'],
  114. 'element' => $options['element'],
  115. 'params' => $options['params']
  116. ];
  117. $this->_session->write('Flash.' . $options['key'], $messages);
  118. }
  119. /**
  120. * Magic method for verbose flash methods based on element names.
  121. *
  122. * For example: $this->Flash->success('My message') would use the
  123. * success.ctp element under `src/Template/Element/Flash` for rendering the
  124. * flash message.
  125. *
  126. * If you make consecutive calls to this method, the messages will stack (if they are
  127. * set with the same flash key)
  128. *
  129. * Note that the parameter `element` will be always overridden. In order to call a
  130. * specific element from a plugin, you should set the `plugin` option in $args.
  131. *
  132. * For example: `$this->Flash->warning('My message', ['plugin' => 'PluginName'])` would
  133. * use the warning.ctp element under `plugins/PluginName/src/Template/Element/Flash` for
  134. * rendering the flash message.
  135. *
  136. * @param string $name Element name to use.
  137. * @param array $args Parameters to pass when calling `FlashComponent::set()`.
  138. * @return void
  139. * @throws \Cake\Http\Exception\InternalErrorException If missing the flash message.
  140. */
  141. public function __call(string $name, array $args): void
  142. {
  143. $element = Inflector::underscore($name);
  144. if (count($args) < 1) {
  145. throw new InternalErrorException('Flash message missing.');
  146. }
  147. $options = ['element' => $element];
  148. if (!empty($args[1])) {
  149. if (!empty($args[1]['plugin'])) {
  150. $options = ['element' => $args[1]['plugin'] . '.' . $element];
  151. unset($args[1]['plugin']);
  152. }
  153. $options += (array)$args[1];
  154. }
  155. $this->set($args[0], $options);
  156. }
  157. }