-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Session.zep
172 lines (136 loc) · 3.86 KB
/
Session.zep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Phalcon\Flash;
use Phalcon\Di\DiInterface;
use Phalcon\Session\ManagerInterface;
/**
* This is an implementation of the Phalcon\Flash\FlashInterface that
* temporarily stores the messages in session, then messages can be printed in
* the next request.
*/
class Session extends AbstractFlash
{
/**
* Clear messages in the session messenger
*/
public function clear() -> void
{
this->getSessionMessages(true);
parent::clear();
}
/**
* Returns the messages in the session flasher
*/
public function getMessages(type = null, bool remove = true) -> array
{
return this->getSessionMessages(remove, type);
}
/**
* Checks whether there are messages
*/
public function has(type = null) -> bool
{
var messages;
let messages = this->getSessionMessages(false);
if typeof type != "string" {
return true;
}
return isset messages[type];
}
/**
* Adds a message to the session flasher
*
* @return null|string|void
*/
public function message(string type, string message) -> string | null
{
var messages;
let messages = this->getSessionMessages(false);
if !isset messages[type] {
let messages[type] = [];
}
let messages[type][] = message;
this->setSessionMessages(messages);
return null;
}
/**
* Prints the messages in the session flasher
*/
public function output(bool remove = true) -> void
{
var type, message, messages;
let messages = this->getSessionMessages(remove);
for type, message in messages {
this->outputMessage(type, message);
}
parent::clear();
}
/**
* Returns the messages stored in session
*/
protected function getSessionMessages(bool remove, type = null) -> array
{
var session, messages, returnMessages;
let session = this->getSessionService(),
messages = session->get("_flashMessages");
/**
* Session might be empty
*/
if typeof messages != "array" {
let messages = [];
}
if typeof type == "string" {
if fetch returnMessages, messages[type] {
if remove {
unset(messages[type]);
session->set("_flashMessages", messages);
}
return returnMessages;
}
return [];
}
if remove {
session->remove("_flashMessages");
}
return messages;
}
/**
* Stores the messages in session
*/
protected function setSessionMessages(array! messages) -> array
{
var session;
let session = this->getSessionService();
session->set("_flashMessages", messages);
return messages;
}
/**
* Returns the Session Service
*/
public function getSessionService() -> <ManagerInterface>
{
var container;
if this->sessionService {
return this->sessionService;
}
let container = <DiInterface> this->container;
if unlikely typeof container != "object" {
throw new Exception(
Exception::containerServiceNotFound("the 'session' service")
);
}
if likely container->has("session") {
return <ManagerInterface> container->getShared("session");
} else {
throw new Exception(
Exception::containerServiceNotFound("the 'session' service")
);
}
}
}