1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Molniya - Flash Messenger |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify it |
8
|
|
|
* under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at your |
10
|
|
|
* option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
* @category Middleware |
21
|
|
|
* @package Jnjxp\Molniya |
22
|
|
|
* @author Jake Johns <[email protected]> |
23
|
|
|
* @copyright 2016 Jake Johns |
24
|
|
|
* @license http://www.gnu.org/licenses/agpl-3.0.txt AGPL V3 |
25
|
|
|
* @link https://github.com/jnjxp/jnjxp.molniya |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace Jnjxp\Molniya; |
29
|
|
|
|
30
|
|
|
use Aura\Session\SessionFactory; |
31
|
|
|
use Aura\Session\Session; |
32
|
|
|
|
33
|
|
|
use Jnjxp\Molniya\FlashMessengerFactory as MessengerFactory; |
34
|
|
|
use Jnjxp\Molniya\FlashMessenger as Messenger; |
35
|
|
|
|
36
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
37
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SessionHandler |
41
|
|
|
* |
42
|
|
|
* @category Middleware |
43
|
|
|
* @package Jnjxp\Molniya |
44
|
|
|
* @author Jake Johns <[email protected]> |
45
|
|
|
* @license http://www.gnu.org/licenses/agpl-3.0.txt AGPL V3 |
46
|
|
|
* @link https://github.com/jnjxp/jnjxp.molniya |
47
|
|
|
*/ |
48
|
|
|
class SessionHandler |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Session factory |
52
|
|
|
* |
53
|
|
|
* @var SessionFactory |
54
|
|
|
* |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
protected $sessionFactory; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Flash messenger factory |
61
|
|
|
* |
62
|
|
|
* @var MessengerFactory |
63
|
|
|
* |
64
|
|
|
* @access protected |
65
|
|
|
*/ |
66
|
|
|
protected $messengerFactory; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Session attribute |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
* |
73
|
|
|
* @access protected |
74
|
|
|
*/ |
75
|
|
|
protected $sessionAttribute = 'aura/session:session'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Messenger attribute |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
* |
82
|
|
|
* @access protected |
83
|
|
|
*/ |
84
|
|
|
protected $messengerAttribute = 'jnjxp/molniya:messenger'; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Messenger namespace |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
* |
91
|
|
|
* @access protected |
92
|
|
|
*/ |
93
|
|
|
protected $messengerNamespace = 'Jnjxp\Molniya'; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create a session handler |
97
|
|
|
* |
98
|
|
|
* @param SessionFactory $sessionFactory Session factory |
99
|
|
|
* @param MessengerFactory $messengerFactory Flash messenger factory |
100
|
|
|
* |
101
|
|
|
* @access public |
102
|
|
|
*/ |
103
|
1 |
|
public function __construct( |
104
|
|
|
SessionFactory $sessionFactory = null, |
105
|
|
|
MessengerFactory $messengerFactory = null |
106
|
|
|
) { |
107
|
1 |
|
$this->sessionFactory = $sessionFactory ?: new SessionFactory(); |
108
|
1 |
|
$this->messengerFactory = $messengerFactory ?: new MessengerFactory(); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set session attribute |
113
|
|
|
* |
114
|
|
|
* @param string $attr name of attribute for session |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
* |
118
|
|
|
* @access public |
119
|
|
|
*/ |
120
|
1 |
|
public function setSessionAttribute($attr) |
121
|
|
|
{ |
122
|
1 |
|
$this->sessionAttribute = $attr; |
123
|
1 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set messenger attribute |
128
|
|
|
* |
129
|
|
|
* @param string $attr name of attribute for messenger |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
* |
133
|
|
|
* @access public |
134
|
|
|
*/ |
135
|
1 |
|
public function setMessengerAttribute($attr) |
136
|
|
|
{ |
137
|
1 |
|
$this->messengerAttribute = $attr; |
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set messenger namespace |
143
|
|
|
* |
144
|
|
|
* @param string $name namespace for messenger segment |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
* |
148
|
|
|
* @access public |
149
|
|
|
*/ |
150
|
1 |
|
public function setMessengerNamespace($name) |
151
|
|
|
{ |
152
|
1 |
|
$this->messengerNamespace = $name; |
153
|
1 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Create session and messenger and set them as request attributes |
158
|
|
|
* |
159
|
|
|
* @param Request $request PSR7 Request |
160
|
|
|
* @param Response $response PSR7 Response |
161
|
|
|
* @param callable $next Next callable middleware |
162
|
|
|
* |
163
|
|
|
* @return Response |
164
|
|
|
* |
165
|
|
|
* @access public |
166
|
|
|
*/ |
167
|
1 |
|
public function __invoke(Request $request, Response $response, callable $next) |
168
|
|
|
{ |
169
|
1 |
|
$session = $this->newSession($request); |
170
|
1 |
|
$request = $this->setRequestSession($request, $session); |
171
|
|
|
|
172
|
1 |
|
$messenger = $this->newMessenger($session); |
173
|
1 |
|
$request = $this->setRequestMessenger($request, $messenger); |
174
|
|
|
|
175
|
1 |
|
return $next($request, $response); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Create a new session from request |
180
|
|
|
* |
181
|
|
|
* @param Request $request PSR7 request |
182
|
|
|
* |
183
|
|
|
* @return Session |
184
|
|
|
* |
185
|
|
|
* @access protected |
186
|
|
|
*/ |
187
|
1 |
|
protected function newSession(Request $request) |
188
|
|
|
{ |
189
|
1 |
|
return $this->sessionFactory->newInstance($request->getCookieParams()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Store session in request attribute if attributes present |
194
|
|
|
* |
195
|
|
|
* @param Request $request PSR7 request |
196
|
|
|
* @param Session $session Session object |
197
|
|
|
* |
198
|
|
|
* @return Request |
199
|
|
|
* |
200
|
|
|
* @access protected |
201
|
|
|
*/ |
202
|
1 |
|
protected function setRequestSession(Request $request, Session $session) |
203
|
|
|
{ |
204
|
1 |
|
if ($this->sessionAttribute !== null) { |
205
|
1 |
|
$request = $request->withAttribute( |
206
|
1 |
|
$this->sessionAttribute, |
207
|
|
|
$session |
208
|
1 |
|
); |
209
|
1 |
|
} |
210
|
1 |
|
return $request; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Create new messenger from session |
215
|
|
|
* |
216
|
|
|
* @param Session $session Session object |
217
|
|
|
* |
218
|
|
|
* @return Messenger |
219
|
|
|
* |
220
|
|
|
* @access protected |
221
|
|
|
*/ |
222
|
1 |
|
protected function newMessenger(Session $session) |
223
|
|
|
{ |
224
|
1 |
|
return $this->messengerFactory |
225
|
1 |
|
->setSession($session) |
226
|
1 |
|
->newInstance($this->messengerNamespace); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set messenger as request attribute if attribute preesent |
231
|
|
|
* |
232
|
|
|
* @param Request $request DESCRIPTION |
233
|
|
|
* @param Messenger $messenger DESCRIPTION |
234
|
|
|
* |
235
|
|
|
* @return Request |
236
|
|
|
* |
237
|
|
|
* @access protected |
238
|
|
|
*/ |
239
|
1 |
|
protected function setRequestMessenger(Request $request, Messenger $messenger) |
240
|
|
|
{ |
241
|
1 |
|
if ($this->messengerAttribute !== null) { |
242
|
1 |
|
$request = $request->withAttribute( |
243
|
1 |
|
$this->messengerAttribute, |
244
|
|
|
$messenger |
245
|
1 |
|
); |
246
|
1 |
|
} |
247
|
1 |
|
return $request; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|