|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Pomm's Foundation package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace PommProject\Foundation\Session; |
|
11
|
|
|
|
|
12
|
|
|
use PommProject\Foundation\Converter; |
|
13
|
|
|
use PommProject\Foundation\ParameterHolder; |
|
14
|
|
|
use PommProject\Foundation\Client\ClientHolder; |
|
15
|
|
|
use PommProject\Foundation\Converter\ConverterHolder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* SessionBuilder |
|
19
|
|
|
* |
|
20
|
|
|
* Session factory. |
|
21
|
|
|
* This class is responsible of creating and configuring a session. It is a |
|
22
|
|
|
* default configuration for session and is dedicated to be overloaded. |
|
23
|
|
|
* |
|
24
|
|
|
* @package Foundation |
|
25
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
|
26
|
|
|
* @author Grégoire HUBERT |
|
27
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
|
28
|
|
|
*/ |
|
29
|
|
|
class SessionBuilder |
|
30
|
|
|
{ |
|
31
|
|
|
protected $configuration; |
|
32
|
|
|
protected $converter_holder; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* __construct |
|
36
|
|
|
* |
|
37
|
|
|
* Instantiate builder. |
|
38
|
|
|
* |
|
39
|
|
|
* Mandatory configuration options are: |
|
40
|
|
|
* dsn: connection parameters |
|
41
|
|
|
* name: database logical name |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $configuration |
|
44
|
|
|
* @param ConverterHolder $converter_holder |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(array $configuration, ConverterHolder $converter_holder = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->configuration = new ParameterHolder( |
|
49
|
|
|
array_merge( |
|
50
|
|
|
$this->getDefaultConfiguration(), |
|
51
|
|
|
$configuration |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
$converter_holder = $converter_holder === null |
|
55
|
|
|
? new ConverterHolder |
|
56
|
|
|
: $converter_holder |
|
57
|
|
|
; |
|
58
|
|
|
|
|
59
|
|
|
$this->initializeConverterHolder($converter_holder); |
|
60
|
|
|
$this->converter_holder = $converter_holder; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* addParameter |
|
65
|
|
|
* |
|
66
|
|
|
* Add a configuration parameter. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @param mixed $value |
|
70
|
|
|
* @return SessionBuilder $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function addParameter($name, $value) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->configuration->setParameter($name, $value); |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* getConverterHolder |
|
81
|
|
|
* |
|
82
|
|
|
* Return the converter holder. |
|
83
|
|
|
* |
|
84
|
|
|
* @return ConverterHolder |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getConverterHolder() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->converter_holder; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* buildSession |
|
93
|
|
|
* |
|
94
|
|
|
* Build a new session. |
|
95
|
|
|
* |
|
96
|
|
|
* @final |
|
97
|
|
|
* @param string $stamp |
|
98
|
|
|
* @return Session |
|
99
|
|
|
*/ |
|
100
|
|
|
final public function buildSession($stamp = null) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->preConfigure(); |
|
103
|
|
|
$dsn = $this |
|
104
|
|
|
->configuration->mustHave('dsn')->getParameter('dsn'); |
|
105
|
|
|
$connection_configuration = |
|
106
|
|
|
$this->configuration |
|
107
|
|
|
->mustHave('connection:configuration') |
|
108
|
|
|
->getParameter('connection:configuration') |
|
109
|
|
|
; |
|
110
|
|
|
$persist = |
|
111
|
|
|
$this->configuration |
|
112
|
|
|
->mustHave('connection:persist') |
|
113
|
|
|
->getParameter('connection:persist') |
|
114
|
|
|
; |
|
115
|
|
|
$session = $this->createSession( |
|
116
|
|
|
$this->createConnection($dsn, $persist, $connection_configuration), |
|
|
|
|
|
|
117
|
|
|
$this->createClientHolder(), |
|
118
|
|
|
$stamp |
|
119
|
|
|
); |
|
120
|
|
|
$this->postConfigure($session); |
|
121
|
|
|
|
|
122
|
|
|
return $session; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* getDefaultConfiguration |
|
127
|
|
|
* |
|
128
|
|
|
* This must return the default configuration for new sessions. Default |
|
129
|
|
|
* parameters are overrided by the configuration passed as parameter to |
|
130
|
|
|
* this builder. |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getDefaultConfiguration() |
|
135
|
|
|
{ |
|
136
|
|
|
return |
|
137
|
|
|
[ |
|
138
|
|
|
"connection:configuration" => |
|
139
|
|
|
[ |
|
140
|
|
|
'bytea_output' => 'hex', |
|
141
|
|
|
'intervalstyle' => 'ISO_8601', |
|
142
|
|
|
'datestyle' => 'ISO', |
|
143
|
|
|
'standard_conforming_strings' => 'true', |
|
144
|
|
|
'timezone' => date_default_timezone_get(), |
|
145
|
|
|
], |
|
146
|
|
|
'connection:persist' => false, |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* preConfigure |
|
152
|
|
|
* |
|
153
|
|
|
* If any computation to the configuration must be done before each session |
|
154
|
|
|
* creation, it goes here. |
|
155
|
|
|
* |
|
156
|
|
|
* @return SessionBuilder $this |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function preConfigure() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* createConnection |
|
165
|
|
|
* |
|
166
|
|
|
* Connection instantiation. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $dsn |
|
169
|
|
|
* @param bool $persist |
|
170
|
|
|
* @param string|array $connection_configuration |
|
171
|
|
|
* @return Connection |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function createConnection($dsn, $persist, $connection_configuration) |
|
174
|
|
|
{ |
|
175
|
|
|
return new Connection($dsn, $persist, $connection_configuration); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* createSession |
|
180
|
|
|
* |
|
181
|
|
|
* Session instantiation. |
|
182
|
|
|
* |
|
183
|
|
|
* @param Connection $connection |
|
184
|
|
|
* @param ClientHolder $client_holder |
|
185
|
|
|
* @param string|null $stamp |
|
186
|
|
|
* @return Session |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function createSession(Connection $connection, ClientHolder $client_holder, $stamp) |
|
189
|
|
|
{ |
|
190
|
|
|
$session_class = $this->configuration->getParameter('class:session', '\PommProject\Foundation\Session\Session'); |
|
191
|
|
|
|
|
192
|
|
|
return new $session_class($connection, $client_holder, $stamp); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* createClientHolder |
|
197
|
|
|
* |
|
198
|
|
|
* Instantiate ClientHolder. |
|
199
|
|
|
* |
|
200
|
|
|
* @return ClientHolder |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function createClientHolder() |
|
203
|
|
|
{ |
|
204
|
|
|
return new ClientHolder(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* postConfigure |
|
209
|
|
|
* |
|
210
|
|
|
* Session configuration once created. |
|
211
|
|
|
* All pooler registration stuff goes here. |
|
212
|
|
|
* |
|
213
|
|
|
* @param Session $session |
|
214
|
|
|
* @return SessionBuilder $this |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function postConfigure(Session $session) |
|
217
|
|
|
{ |
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* initializeConverterHolder |
|
223
|
|
|
* |
|
224
|
|
|
* Converter initialization at startup. |
|
225
|
|
|
* If new converters are to be registered, it goes here. |
|
226
|
|
|
* |
|
227
|
|
|
* @param ConverterHolder $converter_holder |
|
228
|
|
|
* @return SessionBuilder $this |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function initializeConverterHolder(ConverterHolder $converter_holder) |
|
231
|
|
|
{ |
|
232
|
|
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.