Skip to content

Commit 8847c67

Browse files
committed
Deprecate session.storage
1 parent 93f133c commit 8847c67

7 files changed

+242
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.3
55
---
66

7+
* Add the `SessionFactory`, `NativeSessionStorageFactory`, `PhpBridgeSessionStorageFactory` and `MockFileSessionStorageFactory` classes
78
* Calling `Request::getSession()` when there is no available session throws a `SessionNotFoundException`
89
* Add the `RequestStack::getSession` method
910
* Deprecate the `NamespacedAttributeBag` class

Session/SessionFactory.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Session;
13+
14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageFactoryInterface;
16+
17+
// Help opcache.preload discover always-needed symbols
18+
class_exists(Session::class);
19+
20+
/**
21+
* @author Jérémy Derussé <jeremy@derusse.com>
22+
*/
23+
class SessionFactory
24+
{
25+
private $requestStack;
26+
private $storageFactory;
27+
private $usageReporter;
28+
29+
public function __construct(RequestStack $requestStack, SessionStorageFactoryInterface $storageFactory, callable $usageReporter = null)
30+
{
31+
$this->requestStack = $requestStack;
32+
$this->storageFactory = $storageFactory;
33+
$this->usageReporter = $usageReporter;
34+
}
35+
36+
public function createSession(): SessionInterface
37+
{
38+
return new Session($this->storageFactory->createStorage($this->requestStack->getMasterRequest()), null, null, $this->usageReporter);
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Session\Storage;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
// Help opcache.preload discover always-needed symbols
17+
class_exists(MockFileSessionStorage::class);
18+
19+
/**
20+
* @author Jérémy Derussé <jeremy@derusse.com>
21+
*/
22+
class MockFileSessionStorageFactory implements SessionStorageFactoryInterface
23+
{
24+
private $savePath;
25+
private $name;
26+
private $metaBag;
27+
28+
/**
29+
* @see MockFileSessionStorage constructor.
30+
*/
31+
public function __construct(string $savePath = null, string $name = 'MOCKSESSID', MetadataBag $metaBag = null)
32+
{
33+
$this->savePath = $savePath;
34+
$this->name = $name;
35+
$this->metaBag = $metaBag;
36+
}
37+
38+
public function createStorage(?Request $request): SessionStorageInterface
39+
{
40+
return new MockFileSessionStorage($this->savePath, $this->name, $this->metaBag);
41+
}
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Session\Storage;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
// Help opcache.preload discover always-needed symbols
17+
class_exists(NativeSessionStorage::class);
18+
19+
/**
20+
* @author Jérémy Derussé <jeremy@derusse.com>
21+
*/
22+
class NativeSessionStorageFactory implements SessionStorageFactoryInterface
23+
{
24+
private $options;
25+
private $handler;
26+
private $metaBag;
27+
private $secure;
28+
29+
/**
30+
* @see NativeSessionStorage constructor.
31+
*/
32+
public function __construct(array $options = [], $handler = null, MetadataBag $metaBag = null, bool $secure = false)
33+
{
34+
$this->options = $options;
35+
$this->handler = $handler;
36+
$this->metaBag = $metaBag;
37+
$this->secure = $secure;
38+
}
39+
40+
public function createStorage(?Request $request): SessionStorageInterface
41+
{
42+
$storage = new NativeSessionStorage($this->options, $this->handler, $this->metaBag);
43+
if ($this->secure && $request && $request->isSecure()) {
44+
$storage->setOptions(['cookie_secure' => true]);
45+
}
46+
47+
return $storage;
48+
}
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Session\Storage;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
// Help opcache.preload discover always-needed symbols
17+
class_exists(PhpBridgeSessionStorage::class);
18+
19+
/**
20+
* @author Jérémy Derussé <jeremy@derusse.com>
21+
*/
22+
class PhpBridgeSessionStorageFactory implements SessionStorageFactoryInterface
23+
{
24+
private $handler;
25+
private $metaBag;
26+
private $secure;
27+
28+
/**
29+
* @see PhpBridgeSessionStorage constructor.
30+
*/
31+
public function __construct($handler = null, MetadataBag $metaBag = null, bool $secure = false)
32+
{
33+
$this->handler = $handler;
34+
$this->metaBag = $metaBag;
35+
$this->secure = $secure;
36+
}
37+
38+
public function createStorage(?Request $request): SessionStorageInterface
39+
{
40+
$storage = new PhpBridgeSessionStorage($this->handler, $this->metaBag);
41+
if ($this->secure && $request && $request->isSecure()) {
42+
$storage->setOptions(['cookie_secure' => true]);
43+
}
44+
45+
return $storage;
46+
}
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Session\Storage;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
/**
17+
* @author Jérémy Derussé <jeremy@derusse.com>
18+
*
19+
* @internal to be removed in Symfony 6
20+
*/
21+
final class ServiceSessionFactory implements SessionStorageFactoryInterface
22+
{
23+
private $storage;
24+
25+
public function __construct(SessionStorageInterface $storage)
26+
{
27+
$this->storage = $storage;
28+
}
29+
30+
public function createStorage(?Request $request): SessionStorageInterface
31+
{
32+
if ($this->storage instanceof NativeSessionStorage && $request && $request->isSecure()) {
33+
$this->storage->setOptions(['cookie_secure' => true]);
34+
}
35+
36+
return $this->storage;
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Session\Storage;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
/**
17+
* @author Jérémy Derussé <jeremy@derusse.com>
18+
*/
19+
interface SessionStorageFactoryInterface
20+
{
21+
/**
22+
* Creates a new instance of SessionStorageInterface
23+
*/
24+
public function createStorage(?Request $request): SessionStorageInterface;
25+
}

0 commit comments

Comments
 (0)