FlashComponent.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Error\InternalErrorException;
  19. use Cake\Event\Event;
  20. /**
  21. * FlashComponent
  22. */
  23. class FlashComponent extends Component {
  24. /**
  25. * The session
  26. *
  27. * @var \Cake\Network\Session
  28. */
  29. protected $_session;
  30. /**
  31. * Default configuration
  32. *
  33. * @var array
  34. */
  35. protected $_defaultConfig = [
  36. 'key' => 'flash',
  37. 'element' => null,
  38. 'class' => 'info',
  39. 'params' => []
  40. ];
  41. /**
  42. * Constructor
  43. *
  44. * @param ComponentRegistry $collection A ComponentRegistry for this component
  45. * @param array $config Array of config.
  46. */
  47. public function __construct(ComponentRegistry $collection, array $config = []) {
  48. parent::__construct($collection, $config);
  49. $this->_session = $collection->getController()->request->session();
  50. }
  51. /**
  52. * Used to set a session variable that can be used to output messages in the view.
  53. *
  54. * In your controller: $this->Flash->set('This has been saved');
  55. *
  56. * ### Options:
  57. *
  58. * - `key` The key to set under the session's Message key
  59. * - `element` The element used to render the flash message
  60. * - `class` The class to give the flash message whenever an element isn't supplied
  61. * - `params` An array of variables to make available when using an element
  62. *
  63. * @param string $message Message to be flashed
  64. * @param array $options An array of options
  65. * @return void
  66. */
  67. public function set($message, array $options = []) {
  68. $opts = array_merge($this->_defaultConfig, $options);
  69. if ($message instanceof \Exception) {
  70. $message = $message->getMessage();
  71. }
  72. $this->_session->write("Message.{$opts['key']}", [
  73. 'message' => $message,
  74. 'key' => $opts['key'],
  75. 'element' => $opts['element'],
  76. 'class' => $opts['class'],
  77. 'params' => $opts['params']
  78. ]);
  79. }
  80. /**
  81. * Magic method for verbose flash methods based on element names.
  82. *
  83. * @param string $name Element name to use, omitting the "flash_" prefix.
  84. * @param array $args Parameters to pass when calling `FlashComponent::set`.
  85. * @return void
  86. * @throws \Cake\Error\InternalErrorException If missing the flash message.
  87. */
  88. public function __call($name, $args) {
  89. $options = ['element' => 'flash_' . $name];
  90. if (count($args) < 1) {
  91. throw new InternalErrorException('Flash message missing.');
  92. }
  93. if (!empty($args[1])) {
  94. $options += (array)$args[1];
  95. }
  96. $this->set($args[0], $options);
  97. }
  98. }