pictcode / lib / Cake / Controller / Component / FlashComponent.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (3.446 KB)
1 | 635eef61 | spyder1211 | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * Flash Component
|
||
4 | *
|
||
5 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||
6 | * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
7 | *
|
||
8 | * Licensed under The MIT License
|
||
9 | * For full copyright and license information, please see the LICENSE.txt
|
||
10 | * Redistributions of files must retain the above copyright notice.
|
||
11 | *
|
||
12 | * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
13 | * @link http://cakephp.org CakePHP(tm) Project
|
||
14 | * @package Cake.Controller.Component
|
||
15 | * @since CakePHP(tm) v 2.7.0-dev
|
||
16 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
17 | */
|
||
18 | |||
19 | App::uses('Component', 'Controller'); |
||
20 | App::uses('Inflector', 'Utility'); |
||
21 | App::uses('CakeSession', 'Model/Datasource'); |
||
22 | |||
23 | /**
|
||
24 | * The CakePHP FlashComponent provides a way for you to write a flash variable
|
||
25 | * to the session from your controllers, to be rendered in a view with the
|
||
26 | * FlashHelper.
|
||
27 | *
|
||
28 | * @package Cake.Controller.Component
|
||
29 | */
|
||
30 | class FlashComponent extends Component { |
||
31 | |||
32 | /**
|
||
33 | * Default configuration
|
||
34 | *
|
||
35 | * @var array
|
||
36 | */
|
||
37 | protected $_defaultConfig = array( |
||
38 | 'key' => 'flash', |
||
39 | 'element' => 'default', |
||
40 | 'params' => array(), |
||
41 | ); |
||
42 | |||
43 | /**
|
||
44 | * Constructor
|
||
45 | *
|
||
46 | * @param ComponentCollection $collection The ComponentCollection object
|
||
47 | * @param array $settings Settings passed via controller
|
||
48 | */
|
||
49 | public function __construct(ComponentCollection $collection, $settings = array()) { |
||
50 | $this->_defaultConfig = Hash::merge($this->_defaultConfig, $settings); |
||
51 | } |
||
52 | |||
53 | /**
|
||
54 | * Used to set a session variable that can be used to output messages in the view.
|
||
55 | *
|
||
56 | * In your controller: $this->Flash->set('This has been saved');
|
||
57 | *
|
||
58 | * ### Options:
|
||
59 | *
|
||
60 | * - `key` The key to set under the session's Flash key
|
||
61 | * - `element` The element used to render the flash message. Default to 'default'.
|
||
62 | * - `params` An array of variables to make available when using an element
|
||
63 | *
|
||
64 | * @param string $message Message to be flashed. If an instance
|
||
65 | * of Exception the exception message will be used and code will be set
|
||
66 | * in params.
|
||
67 | * @param array $options An array of options.
|
||
68 | * @return void
|
||
69 | */
|
||
70 | |||
71 | public function set($message, $options = array()) { |
||
72 | $options += $this->_defaultConfig; |
||
73 | |||
74 | if ($message instanceof Exception) { |
||
75 | $options['params'] += array('code' => $message->getCode()); |
||
76 | $message = $message->getMessage(); |
||
77 | } |
||
78 | |||
79 | list($plugin, $element) = pluginSplit($options['element'], true); |
||
80 | if (!empty($options['plugin'])) { |
||
81 | $plugin = $options['plugin'] . '.'; |
||
82 | } |
||
83 | $options['element'] = $plugin . 'Flash/' . $element; |
||
84 | |||
85 | CakeSession::write('Message.' . $options['key'], array( |
||
86 | 'message' => $message, |
||
87 | 'key' => $options['key'], |
||
88 | 'element' => $options['element'], |
||
89 | 'params' => $options['params'] |
||
90 | )); |
||
91 | } |
||
92 | |||
93 | /**
|
||
94 | * Magic method for verbose flash methods based on element names.
|
||
95 | *
|
||
96 | * For example: $this->Flash->success('My message') would use the
|
||
97 | * success.ctp element under `app/View/Element/Flash` for rendering the
|
||
98 | * flash message.
|
||
99 | *
|
||
100 | * @param string $name Element name to use.
|
||
101 | * @param array $args Parameters to pass when calling `FlashComponent::set()`.
|
||
102 | * @return void
|
||
103 | * @throws InternalErrorException If missing the flash message.
|
||
104 | */
|
||
105 | public function __call($name, $args) { |
||
106 | $options = array('element' => Inflector::underscore($name)); |
||
107 | |||
108 | if (count($args) < 1) { |
||
109 | throw new InternalErrorException('Flash message missing.'); |
||
110 | } |
||
111 | |||
112 | if (!empty($args[1])) { |
||
113 | $options += (array)$args[1]; |
||
114 | } |
||
115 | |||
116 | $this->set($args[0], $options); |
||
117 | } |
||
118 | } |