FlashComponent.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Controller\Component;
  16. use Cake\Controller\Component;
  17. use Cake\Controller\ComponentRegistry;
  18. use Cake\Network\Exception\InternalErrorException;
  19. use Cake\Utility\Inflector;
  20. /**
  21. * The CakePHP FlashComponent provides a way for you to write a flash variable
  22. * to the session from your controllers, to be rendered in a view with the
  23. * FlashHelper.
  24. */
  25. class FlashComponent extends Component
  26. {
  27. /**
  28. * The Session object instance
  29. *
  30. * @var \Cake\Network\Session
  31. */
  32. protected $_session;
  33. /**
  34. * Default configuration
  35. *
  36. * @var array
  37. */
  38. protected $_defaultConfig = [
  39. 'key' => 'flash',
  40. 'element' => 'default',
  41. 'params' => []
  42. ];
  43. /**
  44. * Constructor
  45. *
  46. * @param ComponentRegistry $registry A ComponentRegistry for this component
  47. * @param array $config Array of config.
  48. */
  49. public function __construct(ComponentRegistry $registry, array $config = [])
  50. {
  51. parent::__construct($registry, $config);
  52. $this->_session = $registry->getController()->request->session();
  53. }
  54. /**
  55. * Used to set a session variable that can be used to output messages in the view.
  56. *
  57. * In your controller: $this->Flash->set('This has been saved');
  58. *
  59. * ### Options:
  60. *
  61. * - `key` The key to set under the session's Flash key
  62. * - `element` The element used to render the flash message. Default to 'default'.
  63. * - `params` An array of variables to make available when using an element
  64. *
  65. * @param string|\Exception $message Message to be flashed. If an instance
  66. * of \Exception the exception message will be used and code will be set
  67. * in params.
  68. * @param array $options An array of options
  69. * @return void
  70. */
  71. public function set($message, array $options = [])
  72. {
  73. $options += $this->config();
  74. if ($message instanceof \Exception) {
  75. $options['params'] += ['code' => $message->getCode()];
  76. $message = $message->getMessage();
  77. }
  78. list($plugin, $element) = pluginSplit($options['element']);
  79. if ($plugin) {
  80. $options['element'] = $plugin . '.Flash/' . $element;
  81. } else {
  82. $options['element'] = 'Flash/' . $element;
  83. }
  84. $this->_session->write('Flash.' . $options['key'], [
  85. 'message' => $message,
  86. 'key' => $options['key'],
  87. 'element' => $options['element'],
  88. 'params' => $options['params']
  89. ]);
  90. }
  91. /**
  92. * Magic method for verbose flash methods based on element names.
  93. *
  94. * For example: $this->Flash->success('My message') would use the
  95. * success.ctp element under `src/Template/Element/Flash` for rendering the
  96. * flash message.
  97. *
  98. * Note that the parameter `element` will be always overridden. In order to call a
  99. * specific element from a plugin, you should set the `plugin` option in $args.
  100. *
  101. * For example: $this->Flash->warning('My message', ['plugin' => 'PluginName']) would
  102. * use the warning.ctp element under `plugins/PluginName/src/Template/Element/Flash` for
  103. * rendering the flash message.
  104. *
  105. * @param string $name Element name to use.
  106. * @param array $args Parameters to pass when calling `FlashComponent::set()`.
  107. * @return void
  108. * @throws \Cake\Network\Exception\InternalErrorException If missing the flash message.
  109. */
  110. public function __call($name, $args)
  111. {
  112. $element = Inflector::underscore($name);
  113. if (count($args) < 1) {
  114. throw new InternalErrorException('Flash message missing.');
  115. }
  116. $options = ['element' => $element];
  117. if (!empty($args[1])) {
  118. if (!empty($args[1]['plugin'])) {
  119. $options = ['element' => $args[1]['plugin'] . '.' . $element];
  120. unset($args[1]['plugin']);
  121. }
  122. $options += (array)$args[1];
  123. }
  124. $this->set($args[0], $options);
  125. }
  126. }