FlashComponent.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * The Session object instance
  28. *
  29. * @var \Cake\Network\Session
  30. */
  31. protected $_session;
  32. /**
  33. * Default configuration
  34. *
  35. * @var array
  36. */
  37. protected $_defaultConfig = [
  38. 'key' => 'flash',
  39. 'element' => 'default',
  40. 'params' => []
  41. ];
  42. /**
  43. * Constructor
  44. *
  45. * @param ComponentRegistry $registry A ComponentRegistry for this component
  46. * @param array $config Array of config.
  47. */
  48. public function __construct(ComponentRegistry $registry, array $config = []) {
  49. parent::__construct($registry, $config);
  50. $this->_session = $registry->getController()->request->session();
  51. }
  52. /**
  53. * Used to set a session variable that can be used to output messages in the view.
  54. *
  55. * In your controller: $this->Flash->set('This has been saved');
  56. *
  57. * ### Options:
  58. *
  59. * - `key` The key to set under the session's Flash key
  60. * - `element` The element used to render the flash message. Default to 'default'.
  61. * - `params` An array of variables to make available when using an element
  62. *
  63. * @param string|\Exception $message Message to be flashed. If an instance
  64. * of \Exception the exception message will be used and code will be set
  65. * in params.
  66. * @param array $options An array of options
  67. * @return void
  68. */
  69. public function set($message, array $options = []) {
  70. $options += $this->config();
  71. if ($message instanceof \Exception) {
  72. $options['params'] += ['code' => $message->getCode()];
  73. $message = $message->getMessage();
  74. }
  75. list($plugin, $element) = pluginSplit($options['element']);
  76. if ($plugin) {
  77. $options['element'] = $plugin . '.Flash/' . $element;
  78. } else {
  79. $options['element'] = 'Flash/' . $element;
  80. }
  81. $this->_session->write('Flash.' . $options['key'], [
  82. 'message' => $message,
  83. 'key' => $options['key'],
  84. 'element' => $options['element'],
  85. 'params' => $options['params']
  86. ]);
  87. }
  88. /**
  89. * Magic method for verbose flash methods based on element names.
  90. *
  91. * For example: $this->Flash->success('My message') would use the
  92. * success.ctp element under `App/Template/Element/Flash` for rendering the
  93. * flash message.
  94. *
  95. * @param string $name Element name to use.
  96. * @param array $args Parameters to pass when calling `FlashComponent::set()`.
  97. * @return void
  98. * @throws \Cake\Network\Exception\InternalErrorException If missing the flash message.
  99. */
  100. public function __call($name, $args) {
  101. $options = ['element' => Inflector::underscore($name)];
  102. if (count($args) < 1) {
  103. throw new InternalErrorException('Flash message missing.');
  104. }
  105. if (!empty($args[1])) {
  106. $options += (array)$args[1];
  107. }
  108. $this->set($args[0], $options);
  109. }
  110. }