Repository navigation
Expand file tree
/
Copy pathSessionServiceProvider.php
More file actions
267 lines (224 loc) · 8.86 KB
/
Copy pathSessionServiceProvider.php
File metadata and controls
267 lines (224 loc) · 8.86 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace Bolt\Session\Bridge\Silex2;
use Bolt\Session\Generator\NativeGenerator;
use Bolt\Session\Handler\Factory\MemcachedFactory;
use Bolt\Session\Handler\Factory\PredisFactory;
use Bolt\Session\Handler\Factory\RedisFactory;
use Bolt\Session\Handler\FileHandler;
use Bolt\Session\Handler\FilesystemHandler;
use Bolt\Session\Handler\MemcachedHandler;
use Bolt\Session\Handler\RedisHandler;
use Bolt\Session\IniBag;
use Bolt\Session\OptionsBag;
use Bolt\Session\Serializer\NativeSerializer;
use Bolt\Session\SessionListener;
use Bolt\Session\SessionStorage;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
/**
* Because screw PHP core.
*
* @author Carson Full <carsonfull@gmail.com>
*/
class SessionServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
/**
* {@inheritdoc}
*/
public function register(Container $app)
{
$app['session'] = function ($app) {
return new Session(
$app['session.storage'],
$app['session.bag.attribute'],
$app['session.bag.flash']
);
};
$app['session.storage'] = function ($app) {
return new SessionStorage(
$app['session.options_bag'],
$app['session.handler'],
$app['session.generator'],
$app['session.serializer']
);
};
$app['session.handler'] = function ($app) {
$options = $app['session.options_bag'];
return $app['session.handler_factory']($options['save_handler'], $options);
};
$app['session.listener'] = function ($app) {
return new SessionListener($app['session'], $app['session.options_bag']);
};
$this->registerOptions($app);
$this->registerHandlers($app);
$app['session.generator'] = function ($app) {
return new NativeGenerator($app['session.generator.bytes_length']);
};
$app['session.generator.bytes_length'] = function ($app) {
$options = $app['session.options_bag'];
return $options->getInt('sid_length', 32);
};
$app['session.serializer'] = function () {
return new NativeSerializer();
};
$app['session.bag.attribute'] = $app->factory(function () {
return new AttributeBag();
});
$app['session.bag.flash'] = $app->factory(function () {
return new FlashBag();
});
$app['session.bag.metadata'] = $app->factory(function () {
return new MetadataBag();
});
}
/**
* {@inheritdoc}
*/
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['session.listener']);
}
protected function registerOptions(Container $app)
{
$app['session.options'] = [];
$app['session.options.import_from_ini'] = true;
$app['session.options_bag'] = function () use ($app) {
/*
* This does two things.
* 1) Merges options together. Precedence is as follows:
* - Options from session.options
* - Options from ini (if enabled with "session.options.import_from_ini")
* - Options hardcoded below
* 2) Converts options to an OptionsBag instance
*
* These defaults are limited to those that are useful and secure.
*/
$defaults = [
'save_handler' => 'files',
'save_path' => '/tmp',
'name' => 'PHPSESSID',
'lazy_write' => true,
'gc_probability' => 1,
'gc_divisor' => 1000,
'gc_maxlifetime' => 1440,
'cookie_lifetime' => 0,
'cookie_path' => '/',
'cookie_domain' => null,
'cookie_secure' => false,
'cookie_httponly' => false,
'sid_length' => 32,
'restrict_realm' => false,
];
$options = new OptionsBag($defaults);
if ($app['session.options.import_from_ini']) {
$ini = new IniBag('session');
foreach ($options as $key => $value) {
if (!$ini->has($key)) {
continue;
}
if (is_int($value)) {
$value = $ini->getInt($key);
} elseif (is_bool($value)) {
$value = $ini->getBoolean($key);
} else {
$value = $ini->get($key);
}
$options[$key] = $value;
}
}
$overrides = $app['session.options'];
// Don't let save_path for different save_handler bleed in.
if (isset($overrides['save_handler']) && $overrides['save_handler'] !== $options['save_handler']) {
$options->remove('save_path');
}
$options->add($overrides);
return $options;
};
}
protected function registerHandlers(Container $app)
{
$app['session.handler_factory'] = $app->protect(
function ($handler, OptionsBag $options) use ($app) {
$key = 'session.handler_factory.' . $handler;
if (isset($app[$key])) {
return $app[$key]($options);
}
throw new \RuntimeException("Unsupported handler type '$handler' specified");
}
);
$this->registerFilesHandler($app);
$this->registerFilesystemHandler($app);
$this->registerMemcacheHandler($app);
$this->registerRedisHandler($app);
}
protected function registerFilesHandler(Container $app)
{
$app['session.handler_factory.files'] = $app->protect(
function ($options) use ($app) {
return new FileHandler($options['save_path'], $app['logger']);
}
);
}
protected function registerFilesystemHandler(Container $app)
{
$app['session.handler_factory.filesystem'] = $app->protect(
function ($options) use ($app) {
$dir = $app['filesystem']->getDir($options['save_path']);
return new FilesystemHandler($dir);
}
);
}
protected function registerMemcacheHandler(Container $app)
{
$app['session.handler_factory.backing_memcached'] = $app->protect(
function (OptionsBag $options) {
return (new MemcachedFactory())->create($options);
}
);
$app['session.handler_factory.memcached'] = $app->protect(
function (OptionsBag $options) use ($app) {
$memcache = $app['session.handler_factory.backing_memcached']($options);
$handlerOptions = new OptionsBag($options->get('options', []));
$memcacheOptions = [];
if (isset($handlerOptions['expiretime'])) {
$memcacheOptions['expiretime'] = $handlerOptions['expiretime'];
}
if (isset($handlerOptions['prefix'])) {
$memcacheOptions['prefix'] = $handlerOptions['prefix'];
}
return new MemcachedHandler($memcache, $memcacheOptions);
}
);
}
protected function registerRedisHandler(Container $app)
{
$app['session.handler_factory.backing_redis'] = $app->protect(
function (OptionsBag $options) use ($app) {
return (new RedisFactory())->create($options);
}
);
$app['session.handler_factory.redis'] = $app->protect(
function (OptionsBag $options) use ($app) {
$redis = $app['session.handler_factory.backing_redis']($options);
return new RedisHandler($redis, $options['gc_maxlifetime']);
}
);
$app['session.handler_factory.backing_predis'] = $app->protect(
function (OptionsBag $options) use ($app) {
return (new PredisFactory())->create($options);
}
);
$app['session.handler_factory.predis'] = $app->protect(
function (OptionsBag $options) use ($app) {
$redis = $app['session.handler_factory.backing_predis']($options);
return new RedisHandler($redis, $options['gc_maxlifetime']);
}
);
}
}