1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Autarky\Http;
12:
13: use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
14: use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
15: use Symfony\Component\HttpFoundation\Session\Session;
16: use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
17: use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
18: use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
19: use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
20: use Symfony\Component\HttpFoundation\Session\Storage\Handler\WriteCheckSessionHandler;
21:
22: use Autarky\Providers\AbstractProvider;
23:
24: 25: 26:
27: class SessionProvider extends AbstractProvider
28: {
29: 30: 31:
32: protected $config;
33:
34: 35: 36:
37: protected $dic;
38:
39: 40: 41:
42: public function register()
43: {
44: $this->config = $this->app->getConfig();
45: $this->dic = $this->app->getContainer();
46:
47: $this->dic->define('Autarky\Http\SessionHandlerFactory', function() {
48: return new SessionHandlerFactory($this->dic, $this->config);
49: });
50: $this->dic->share('Autarky\Http\SessionHandlerFactory');
51: $this->dic->define('SessionHandlerInterface', [$this, 'makeSessionHandler']);
52: $this->dic->share('SessionHandlerInterface');
53:
54: $this->dic->define('Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface',
55: [$this, 'makeSessionStorage']);
56: $this->dic->share('Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface');
57:
58: $this->dic->define('Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag', function() {
59: return new AttributeBag('_autarky_attributes');
60: });
61: $this->dic->alias(
62: 'Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag',
63: 'Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface'
64: );
65:
66: $this->dic->define('Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag', function() {
67: return new AutoExpireFlashBag('_autarky_flashes');
68: });
69: $this->dic->alias(
70: 'Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag',
71: 'Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface'
72: );
73:
74: $this->dic->define('Symfony\Component\HttpFoundation\Session\Session',
75: [$this, 'makeSession']);
76: $this->dic->share('Symfony\Component\HttpFoundation\Session\Session');
77: $this->dic->alias(
78: 'Symfony\Component\HttpFoundation\Session\Session',
79: 'Symfony\Component\HttpFoundation\Session\SessionInterface'
80: );
81:
82: $this->app->addMiddleware(['Autarky\Http\SessionMiddleware', $this->app]);
83: }
84:
85: 86: 87: 88: 89:
90: public function makeSessionHandler()
91: {
92: $handler = $this->dic->resolve('Autarky\Http\SessionHandlerFactory')
93: ->makeHandler($this->config->get('session.handler'));
94:
95: if ($this->config->get('session.write_check') === true) {
96: $handler = new WriteCheckSessionHandler($handler);
97: }
98:
99: return $handler;
100: }
101:
102: 103: 104: 105: 106:
107: public function makeSessionStorage()
108: {
109: $storage = $this->config->get('session.storage');
110:
111: if ($storage == 'mock_array') {
112: return new MockArraySessionStorage;
113: }
114: if ($storage == 'mock_file') {
115: return new MockFileSessionStorage;
116: }
117:
118: $handler = $this->dic->resolve('SessionHandlerInterface');
119:
120: if ($storage == 'bridge') {
121: return new PhpBridgeSessionStorage($handler);
122: }
123:
124: $options = $this->config->get('session.storage_options', []);
125:
126: if ($storage == 'native') {
127: return new NativeSessionStorage($options, $handler);
128: }
129:
130: if (!is_string($storage)) {
131: $storage = gettype($storage);
132: }
133:
134: throw new \RuntimeException("Unknown session storage driver: $storage");
135: }
136:
137: 138: 139: 140: 141:
142: public function makeSession()
143: {
144: $session = new Session(
145: $this->dic->resolve('Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface'),
146: $this->dic->resolve('Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface'),
147: $this->dic->resolve('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface')
148: );
149:
150: $session->setName($this->config->get('session.cookie.name', 'autarky_session'));
151:
152: return $session;
153: }
154: }
155: