-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
39 lines (32 loc) · 1.16 KB
/
index.php
File metadata and controls
39 lines (32 loc) · 1.16 KB
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
<?php
/*
* Session addon for Bear Framework
* https://github.com/bearframework/session-addon
* Copyright (c) Ivo Petkov
* Free to use under the MIT license.
*/
use BearFramework\App;
$app = App::get();
$context = $app->contexts->get(__DIR__);
$context->classes
->add('BearFramework\Session', 'classes/Session.php');
$cookieName = 'bfsesid';
$app->shortcuts
->add('session', function () use ($app, $cookieName) {
return new \BearFramework\Session($app->request->cookies->getValue($cookieName));
});
$app
->addEventListener('beforeSendResponse', function (App\BeforeSendResponseEventDetails $details) use ($app, $cookieName): void {
$response = $details->response;
$cookieSessionID = $app->request->cookies->getValue($cookieName);
$currentSessionID = $app->session->id;
if ($currentSessionID === null) {
if ($cookieSessionID !== null) {
$response->cookies->delete($cookieName);
}
} else {
if ($cookieSessionID !== $currentSessionID) {
$response->cookies->set($response->cookies->make($cookieName, $currentSessionID));
}
}
});