1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZfcUser\Authentication\Storage; |
4
|
|
|
|
5
|
|
|
use Zend\Authentication\Storage; |
6
|
|
|
use Zend\Authentication\Storage\StorageInterface; |
7
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface; |
8
|
|
|
use Zend\ServiceManager\ServiceManager; |
9
|
|
|
use ZfcUser\Mapper\UserInterface as UserMapper; |
10
|
|
|
|
11
|
|
|
class Db implements Storage\StorageInterface, ServiceManagerAwareInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var StorageInterface |
15
|
|
|
*/ |
16
|
|
|
protected $storage; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var UserMapper |
20
|
|
|
*/ |
21
|
|
|
protected $mapper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
protected $resolvedIdentity; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ServiceManager |
30
|
|
|
*/ |
31
|
|
|
protected $serviceManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns true if and only if storage is empty |
35
|
|
|
* |
36
|
|
|
* @throws \Zend\Authentication\Exception\InvalidArgumentException If it is impossible to determine whether |
37
|
|
|
* storage is empty or not |
38
|
|
|
* @return boolean |
39
|
|
|
*/ |
40
|
|
|
public function isEmpty() |
41
|
|
|
{ |
42
|
|
|
if ($this->getStorage()->isEmpty()) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
$identity = $this->read(); |
46
|
|
|
if ($identity === null) { |
47
|
|
|
$this->clear(); |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the contents of storage |
56
|
|
|
* |
57
|
|
|
* Behavior is undefined when storage is empty. |
58
|
|
|
* |
59
|
|
|
* @throws \Zend\Authentication\Exception\InvalidArgumentException If reading contents from storage is impossible |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function read() |
63
|
|
|
{ |
64
|
|
|
if (null !== $this->resolvedIdentity) { |
65
|
|
|
return $this->resolvedIdentity; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$identity = $this->getStorage()->read(); |
69
|
|
|
|
70
|
|
|
if (is_int($identity) || is_scalar($identity)) { |
71
|
|
|
$identity = $this->getMapper()->findById($identity); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($identity) { |
75
|
|
|
$this->resolvedIdentity = $identity; |
76
|
|
|
} else { |
77
|
|
|
$this->resolvedIdentity = null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->resolvedIdentity; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Writes $contents to storage |
85
|
|
|
* |
86
|
|
|
* @param mixed $contents |
87
|
|
|
* @throws \Zend\Authentication\Exception\InvalidArgumentException If writing $contents to storage is impossible |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function write($contents) |
91
|
|
|
{ |
92
|
|
|
$this->resolvedIdentity = null; |
93
|
|
|
$this->getStorage()->write($contents); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Clears contents from storage |
98
|
|
|
* |
99
|
|
|
* @throws \Zend\Authentication\Exception\InvalidArgumentException If clearing contents from storage is impossible |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function clear() |
103
|
|
|
{ |
104
|
|
|
$this->resolvedIdentity = null; |
105
|
|
|
$this->getStorage()->clear(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* getStorage |
110
|
|
|
* |
111
|
|
|
* @return Storage\StorageInterface |
112
|
|
|
*/ |
113
|
|
|
public function getStorage() |
114
|
|
|
{ |
115
|
|
|
if (null === $this->storage) { |
116
|
|
|
$this->setStorage(new Storage\Session); |
117
|
|
|
} |
118
|
|
|
return $this->storage; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* setStorage |
123
|
|
|
* |
124
|
|
|
* @param Storage\StorageInterface $storage |
125
|
|
|
* @access public |
126
|
|
|
* @return Db |
127
|
|
|
*/ |
128
|
|
|
public function setStorage(Storage\StorageInterface $storage) |
129
|
|
|
{ |
130
|
|
|
$this->storage = $storage; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* getMapper |
136
|
|
|
* |
137
|
|
|
* @return UserMapper |
138
|
|
|
*/ |
139
|
|
|
public function getMapper() |
140
|
|
|
{ |
141
|
|
|
if (null === $this->mapper) { |
142
|
|
|
$this->mapper = $this->getServiceManager()->get('zfcuser_user_mapper'); |
143
|
|
|
} |
144
|
|
|
return $this->mapper; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* setMapper |
149
|
|
|
* |
150
|
|
|
* @param UserMapper $mapper |
151
|
|
|
* @return Db |
152
|
|
|
*/ |
153
|
|
|
public function setMapper(UserMapper $mapper) |
154
|
|
|
{ |
155
|
|
|
$this->mapper = $mapper; |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Retrieve service manager instance |
161
|
|
|
* |
162
|
|
|
* @return ServiceManager |
163
|
|
|
*/ |
164
|
|
|
public function getServiceManager() |
165
|
|
|
{ |
166
|
|
|
return $this->serviceManager; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set service manager instance |
171
|
|
|
* |
172
|
|
|
* @param ServiceManager $locator |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function setServiceManager(ServiceManager $serviceManager) |
176
|
|
|
{ |
177
|
|
|
$this->serviceManager = $serviceManager; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|