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 Jnjxp\Molniya\FlashMessengerFactory as MessengerFactory; |
32
|
|
|
|
33
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
34
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* SessionHandler |
38
|
|
|
* |
39
|
|
|
* @category Middleware |
40
|
|
|
* @package Jnjxp\Molniya |
41
|
|
|
* @author Jake Johns <[email protected]> |
42
|
|
|
* @license http://www.gnu.org/licenses/agpl-3.0.txt AGPL V3 |
43
|
|
|
* @link https://github.com/jnjxp/jnjxp.molniya |
44
|
|
|
*/ |
45
|
|
|
class SessionHandler |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* Session factory |
49
|
|
|
* |
50
|
|
|
* @var SessionFactory |
51
|
|
|
* |
52
|
|
|
* @access protected |
53
|
|
|
*/ |
54
|
|
|
protected $sessionFactory; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Flash messenger factory |
58
|
|
|
* |
59
|
|
|
* @var MessengerFactory |
60
|
|
|
* |
61
|
|
|
* @access protected |
62
|
|
|
*/ |
63
|
|
|
protected $messengerFactory; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Session attribute |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
* |
70
|
|
|
* @access protected |
71
|
|
|
*/ |
72
|
|
|
protected $sessionAttribute = 'aura/session:session'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Messenger attribute |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
* |
79
|
|
|
* @access protected |
80
|
|
|
*/ |
81
|
|
|
protected $messengerAttribute = 'jnjxp/molniya:messenger'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Messenger namespace |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
* |
88
|
|
|
* @access protected |
89
|
|
|
*/ |
90
|
|
|
protected $messengerNamespace = 'Jnjxp\Molniya'; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create a session handler |
94
|
|
|
* |
95
|
|
|
* @param SessionFactory $sessionFactory Session factory |
96
|
|
|
* @param MessengerFactory $messengerFactory Flash messenger factory |
97
|
|
|
* |
98
|
|
|
* @access public |
99
|
|
|
*/ |
100
|
1 |
|
public function __construct( |
101
|
|
|
SessionFactory $sessionFactory = null, |
102
|
|
|
MessengerFactory $messengerFactory = null |
103
|
|
|
) { |
104
|
1 |
|
$this->sessionFactory = $sessionFactory ?: new SessionFactory(); |
105
|
1 |
|
$this->messengerFactory = $messengerFactory ?: new MessengerFactory(); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set session attribute |
110
|
|
|
* |
111
|
|
|
* @param string $attr name of attribute for session |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
* |
115
|
|
|
* @access public |
116
|
|
|
*/ |
117
|
1 |
|
public function setSessionAttribute($attr) |
118
|
|
|
{ |
119
|
1 |
|
$this->sessionAttribute = $attr; |
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set messenger attribute |
125
|
|
|
* |
126
|
|
|
* @param string $attr name of attribute for messenger |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
* |
130
|
|
|
* @access public |
131
|
|
|
*/ |
132
|
1 |
|
public function setMessengerAttribute($attr) |
133
|
|
|
{ |
134
|
1 |
|
$this->messengerAttribute = $attr; |
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set messenger namespace |
140
|
|
|
* |
141
|
|
|
* @param string $name namespace for messenger segment |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
* |
145
|
|
|
* @access public |
146
|
|
|
*/ |
147
|
1 |
|
public function setMessengerNamespace($name) |
148
|
|
|
{ |
149
|
1 |
|
$this->messengerNamespace = $name; |
150
|
1 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Create session and messenger and set them as request attributes |
155
|
|
|
* |
156
|
|
|
* @param Request $request PSR7 Request |
157
|
|
|
* @param Response $response PSR7 Response |
158
|
|
|
* @param callable $next Next callable middleware |
159
|
|
|
* |
160
|
|
|
* @return Response |
161
|
|
|
* |
162
|
|
|
* @access public |
163
|
|
|
*/ |
164
|
1 |
|
public function __invoke(Request $request, Response $response, callable $next) |
165
|
|
|
{ |
166
|
|
|
// Create Session |
167
|
1 |
|
$session = $this->sessionFactory |
168
|
1 |
|
->newInstance($request->getCookieParams()); |
169
|
|
|
|
170
|
|
|
// Set session on request if attribute present |
171
|
1 |
|
if ($this->sessionAttribute !== null) { |
172
|
1 |
|
$request = $request->withAttribute( |
173
|
1 |
|
$this->sessionAttribute, |
174
|
|
|
$session |
175
|
1 |
|
); |
176
|
1 |
|
} |
177
|
|
|
|
178
|
|
|
// Create Messenger with Session |
179
|
1 |
|
$messenger = $this->messengerFactory |
180
|
1 |
|
->setSession($session) |
181
|
1 |
|
->newInstance($this->messengerNamespace); |
182
|
|
|
|
183
|
|
|
// Set messenger on request if attribute present |
184
|
1 |
|
if ($this->messengerAttribute !== null) { |
185
|
1 |
|
$request = $request->withAttribute( |
186
|
1 |
|
$this->messengerAttribute, |
187
|
|
|
$messenger |
188
|
1 |
|
); |
189
|
1 |
|
} |
190
|
|
|
|
191
|
1 |
|
return $next($request, $response); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|