forked from silexphp/Silex-Providers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionServiceProvider.php
85 lines (70 loc) · 2.66 KB
/
SessionServiceProvider.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
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Silex\Provider\Session\SessionListener;
use Silex\Provider\Session\TestSessionListener;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* Symfony HttpFoundation component Provider for sessions.
*
* @author Fabien Potencier <[email protected]>
*/
class SessionServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
public function register(Container $app)
{
$app['session.test'] = false;
$app['session'] = function ($app) {
return new Session($app['session.storage'], $app['session.attribute_bag'], $app['session.flash_bag']);
};
$app['session.storage'] = function ($app) {
if ($app['session.test']) {
return $app['session.storage.test'];
}
return $app['session.storage.native'];
};
$app['session.storage.handler'] = function ($app) {
return new NativeFileSessionHandler($app['session.storage.save_path']);
};
$app['session.storage.native'] = function ($app) {
return new NativeSessionStorage(
$app['session.storage.options'],
$app['session.storage.handler']
);
};
$app['session.listener'] = function ($app) {
return new SessionListener($app);
};
$app['session.storage.test'] = function () {
return new MockFileSessionStorage();
};
$app['session.listener.test'] = function ($app) {
return new TestSessionListener($app);
};
$app['session.storage.options'] = [];
$app['session.storage.save_path'] = null;
$app['session.attribute_bag'] = null;
$app['session.flash_bag'] = null;
}
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['session.listener']);
if ($app['session.test']) {
$dispatcher->addSubscriber($app['session.listener.test']);
}
}
}