Skip to content

Commit 103549a

Browse files
committed
update readme. add a flash tool class.
1 parent ca69abf commit 103549a

File tree

3 files changed

+249
-2
lines changed

3 files changed

+249
-2
lines changed

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 inhere
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
- a simple php view renderer, front assets load manage
44
- url,html,curl helper class
55

6-
## usage
6+
## Usage
77

8-
### view renderer
8+
### View renderer
99

1010
- support layout, data render
1111
- support simple assets manage and load
@@ -86,3 +86,19 @@ $renderer
8686
</body>
8787
</html>
8888
```
89+
90+
### Flash Messages
91+
92+
```php
93+
$flash = new Flash();
94+
95+
// a page
96+
$flash->warning('page-msg', 'Please login to operate!');
97+
98+
// an other page
99+
$msg = $flash->get('page-msg');
100+
```
101+
102+
## license
103+
104+
**[MIT](LICENSE)**

src/Util/Flash.php

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2018/6/1 0001
6+
* Time: 21:19
7+
*/
8+
9+
namespace Toolkit\Web\Util;
10+
11+
/**
12+
* Class Flash
13+
* @package Toolkit\Web\Util
14+
* @method normal(string $key, string $message, array $opts = [])
15+
* @method info(string $key, string $message, array $opts = [])
16+
* @method error(string $key, string $message, array $opts = [])
17+
* @method danger(string $key, string $message, array $opts = [])
18+
* @method success(string $key, string $message, array $opts = [])
19+
* @method warning(string $key, string $message, array $opts = [])
20+
* @method dark(string $key, string $message, array $opts = [])
21+
*/
22+
class Flash
23+
{
24+
// use BootstrapStyleMessageTrait;
25+
26+
// alert style
27+
const LIGHT = 'light';
28+
const DARK = 'dark';
29+
const INFO = 'info';
30+
const SUCCESS = 'success';
31+
const PRIMARY = 'primary';
32+
const WARN = 'warning';
33+
const WARNING = 'warning';
34+
const ERROR = 'danger';
35+
const DANGER = 'danger';
36+
const SECONDARY = 'secondary';
37+
38+
const SESS_DATA_KEY = '_user_flash_data';
39+
40+
const FLASH_STYLES = [
41+
'normal' => 'secondary',
42+
'default' => 'secondary',
43+
'primary' => 'primary',
44+
'secondary' => 'secondary',
45+
'success' => 'success',
46+
'error' => 'danger',
47+
'danger' => 'danger',
48+
'warning' => 'warning',
49+
'info' => 'info',
50+
'light' => 'light',
51+
'dark' => 'dark'
52+
];
53+
54+
/**
55+
* @var string
56+
*/
57+
private $storageKey = self::SESS_DATA_KEY;
58+
59+
/**
60+
* Messages from previous request
61+
* @var string[]
62+
*/
63+
protected $previous = [];
64+
65+
/**
66+
* Message storage
67+
* @var null|array|\ArrayAccess
68+
*/
69+
protected $storage;
70+
71+
/**
72+
* Flash constructor.
73+
* @param null|array|\ArrayAccess $storage
74+
* @throws \RuntimeException
75+
* @throws \InvalidArgumentException
76+
*/
77+
public function __construct(&$storage = null)
78+
{
79+
// Set storage
80+
if (\is_array($storage) || $storage instanceof \ArrayAccess) {
81+
$this->storage = &$storage;
82+
} elseif (null === $storage) {
83+
if ($_SESSION === null) {
84+
throw new \RuntimeException('Flash messages middleware failed. Session not found.');
85+
}
86+
87+
$this->storage = &$_SESSION;
88+
} else {
89+
throw new \InvalidArgumentException('Flash messages storage must be an array or implement \ArrayAccess');
90+
}
91+
92+
// load previous request messages
93+
if ($previous = $this->storage[$this->storageKey] ?? null) {
94+
$this->previous = $previous;
95+
}
96+
97+
// clear old, init for current request
98+
$this->storage[$this->storageKey] = [];
99+
}
100+
101+
/**
102+
* flash message
103+
* @param string $key
104+
* @param string $message
105+
* @param string $style
106+
* @param array $opts
107+
* [
108+
* 'title' => 'custom title',
109+
* 'closeable' => 1
110+
* ]
111+
*/
112+
public function addMessage(string $key, string $message, string $style = 'info', array $opts = [])
113+
{
114+
if (!$key || !$message) {
115+
return;
116+
}
117+
118+
$body = \array_merge([
119+
'closeable' => 1,
120+
], $opts, [
121+
'type' => $style,
122+
'msg' => $message,
123+
]);
124+
125+
if (!isset($body['title'])) {
126+
$body['title'] = \ucfirst($style);
127+
}
128+
129+
// add message
130+
$this->storage[$this->storageKey][$key] = \json_encode($body);
131+
}
132+
133+
/**
134+
* flash old request data.
135+
* @param string $key
136+
* @param array $data
137+
*/
138+
public function save(string $key, array $data)
139+
{
140+
$this->storage[$this->storageKey][$key] = \json_encode($data);
141+
}
142+
143+
/**
144+
* @param string $key
145+
* @return null|array
146+
*/
147+
public function get(string $key)
148+
{
149+
if ($json = $this->previous[$key] ?? null) {
150+
return \json_decode($json, true);
151+
}
152+
153+
return null;
154+
}
155+
156+
/**
157+
* @param string $key
158+
* @return string
159+
*/
160+
public function getMessage(string $key): string
161+
{
162+
if ($body = $this->get($key)) {
163+
return $body['msg'] ?? '';
164+
}
165+
166+
return '';
167+
}
168+
169+
/**
170+
* @param string $name style name
171+
* @param array $args
172+
* @return self
173+
* @throws \InvalidArgumentException
174+
*/
175+
public function __call($name, array $args)
176+
{
177+
if ($style = self::FLASH_STYLES[$name] ?? null) {
178+
if (!isset($args[1])) {
179+
throw new \InvalidArgumentException('missing enough parameters');
180+
}
181+
182+
$this->addMessage($args[0], $args[1], $style, isset($args[2]) ? (array)$args[2] : []);
183+
}
184+
185+
return $this;
186+
}
187+
188+
/**
189+
* @return array
190+
*/
191+
public function getMessages(): array
192+
{
193+
return $this->storage[$this->storageKey];
194+
}
195+
196+
/**
197+
* @return string
198+
*/
199+
public function getStorageKey(): string
200+
{
201+
return $this->storageKey;
202+
}
203+
204+
/**
205+
* @param string $storageKey
206+
*/
207+
public function setStorageKey(string $storageKey)
208+
{
209+
$this->storageKey = $storageKey;
210+
}
211+
}

0 commit comments

Comments
 (0)