forked from yiisoft/yii2-authclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionStateStorage.php
85 lines (76 loc) · 1.95 KB
/
SessionStateStorage.php
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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient;
use Yii;
use yii\base\Component;
use yii\di\Instance;
use yii\web\Session;
/**
* SessionStateStorage provides Auth client state storage based on web session.
*
* @see StateStorageInterface
* @see Session
*
* @author Paul Klimov <[email protected]>
* @since 2.1
*/
class SessionStateStorage extends Component implements StateStorageInterface
{
/**
* @var Session|array|string session object or the application component ID of the session object to be used.
*
* After the SessionStateStorage object is created, if you want to change this property,
* you should only assign it with a session object.
*
* If not set - application 'session' component will be used, but only, if it is available (e.g. in web application),
* otherwise - no session will be used and no data saving will be performed.
*/
public $session;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->session === null) {
if (Yii::$app->has('session')) {
$this->session = Yii::$app->get('session');
}
} else {
$this->session = Instance::ensure($this->session, Session::className());
}
}
/**
* @inheritdoc
*/
public function set($key, $value)
{
if ($this->session !== null) {
$this->session->set($key, $value);
}
}
/**
* @inheritdoc
*/
public function get($key)
{
if ($this->session !== null) {
return $this->session->get($key);
}
return null;
}
/**
* @inheritdoc
*/
public function remove($key)
{
if ($this->session !== null) {
$this->session->remove($key);
}
return true;
}
}