upgraded dependencies

This commit is contained in:
RafficMohammed
2023-01-08 01:59:16 +05:30
parent 51056e3aad
commit f9ae387337
6895 changed files with 133617 additions and 178680 deletions

View File

@@ -43,26 +43,19 @@ abstract class AbstractSessionHandler implements \SessionHandlerInterface, \Sess
}
/**
* @param string $sessionId
*
* @return string
*/
abstract protected function doRead($sessionId);
abstract protected function doRead(string $sessionId);
/**
* @param string $sessionId
* @param string $data
*
* @return bool
*/
abstract protected function doWrite($sessionId, $data);
abstract protected function doWrite(string $sessionId, string $data);
/**
* @param string $sessionId
*
* @return bool
*/
abstract protected function doDestroy($sessionId);
abstract protected function doDestroy(string $sessionId);
/**
* @return bool

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
/**
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
*/
class IdentityMarshaller implements MarshallerInterface
{
/**
* {@inheritdoc}
*/
public function marshall(array $values, ?array &$failed): array
{
foreach ($values as $key => $value) {
if (!\is_string($value)) {
throw new \LogicException(sprintf('%s accepts only string as data.', __METHOD__));
}
}
return $values;
}
/**
* {@inheritdoc}
*/
public function unmarshall(string $value): string
{
return $value;
}
}

View File

@@ -0,0 +1,108 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
/**
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
*/
class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
{
private $handler;
private $marshaller;
public function __construct(AbstractSessionHandler $handler, MarshallerInterface $marshaller)
{
$this->handler = $handler;
$this->marshaller = $marshaller;
}
/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function open($savePath, $name)
{
return $this->handler->open($savePath, $name);
}
/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function close()
{
return $this->handler->close();
}
/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function destroy($sessionId)
{
return $this->handler->destroy($sessionId);
}
/**
* @return int|false
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
{
return $this->handler->gc($maxlifetime);
}
/**
* @return string
*/
#[\ReturnTypeWillChange]
public function read($sessionId)
{
return $this->marshaller->unmarshall($this->handler->read($sessionId));
}
/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function write($sessionId, $data)
{
$failed = [];
$marshalledData = $this->marshaller->marshall(['data' => $data], $failed);
if (isset($failed['data'])) {
return false;
}
return $this->handler->write($sessionId, $marshalledData['data']);
}
/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function validateId($sessionId)
{
return $this->handler->validateId($sessionId);
}
/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function updateTimestamp($sessionId, $data)
{
return $this->handler->updateTimestamp($sessionId, $data);
}
}

View File

@@ -38,7 +38,7 @@ class MemcachedSessionHandler extends AbstractSessionHandler
*
* List of available options:
* * prefix: The prefix to use for the memcached keys in order to avoid collision
* * expiretime: The time to live in seconds.
* * ttl: The time to live in seconds.
*
* @throws \InvalidArgumentException When unsupported options are passed
*/
@@ -46,11 +46,11 @@ class MemcachedSessionHandler extends AbstractSessionHandler
{
$this->memcached = $memcached;
if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime'])) {
if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime', 'ttl'])) {
throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
}
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
$this->ttl = $options['expiretime'] ?? $options['ttl'] ?? null;
$this->prefix = $options['prefix'] ?? 'sf2s';
}
@@ -66,7 +66,7 @@ class MemcachedSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doRead($sessionId)
protected function doRead(string $sessionId)
{
return $this->memcached->get($this->prefix.$sessionId) ?: '';
}
@@ -77,7 +77,7 @@ class MemcachedSessionHandler extends AbstractSessionHandler
#[\ReturnTypeWillChange]
public function updateTimestamp($sessionId, $data)
{
$this->memcached->touch($this->prefix.$sessionId, time() + $this->ttl);
$this->memcached->touch($this->prefix.$sessionId, $this->getCompatibleTtl());
return true;
}
@@ -85,15 +85,28 @@ class MemcachedSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doWrite($sessionId, $data)
protected function doWrite(string $sessionId, string $data)
{
return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
return $this->memcached->set($this->prefix.$sessionId, $data, $this->getCompatibleTtl());
}
private function getCompatibleTtl(): int
{
$ttl = (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime'));
// If the relative TTL that is used exceeds 30 days, memcached will treat the value as Unix time.
// We have to convert it to an absolute Unix time at this point, to make sure the TTL is correct.
if ($ttl > 60 * 60 * 24 * 30) {
$ttl += time();
}
return $ttl;
}
/**
* {@inheritdoc}
*/
protected function doDestroy($sessionId)
protected function doDestroy(string $sessionId)
{
$result = $this->memcached->delete($this->prefix.$sessionId);

View File

@@ -22,7 +22,14 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
*/
class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
{
/**
* @var \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface
*/
private $currentHandler;
/**
* @var \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface
*/
private $writeOnlyHandler;
public function __construct(\SessionHandlerInterface $currentHandler, \SessionHandlerInterface $writeOnlyHandler)

View File

@@ -11,6 +11,11 @@
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
use MongoDB\BSON\Binary;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Client;
use MongoDB\Collection;
/**
* Session handler using the mongodb/mongodb package and MongoDB driver extension.
*
@@ -24,7 +29,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
private $mongo;
/**
* @var \MongoDB\Collection
* @var Collection
*/
private $collection;
@@ -63,7 +68,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
*
* @throws \InvalidArgumentException When "database" or "collection" not provided
*/
public function __construct(\MongoDB\Client $mongo, array $options)
public function __construct(Client $mongo, array $options)
{
if (!isset($options['database']) || !isset($options['collection'])) {
throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler.');
@@ -91,7 +96,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doDestroy($sessionId)
protected function doDestroy(string $sessionId)
{
$this->getCollection()->deleteOne([
$this->options['id_field'] => $sessionId,
@@ -107,21 +112,21 @@ class MongoDbSessionHandler extends AbstractSessionHandler
public function gc($maxlifetime)
{
return $this->getCollection()->deleteMany([
$this->options['expiry_field'] => ['$lt' => new \MongoDB\BSON\UTCDateTime()],
$this->options['expiry_field'] => ['$lt' => new UTCDateTime()],
])->getDeletedCount();
}
/**
* {@inheritdoc}
*/
protected function doWrite($sessionId, $data)
protected function doWrite(string $sessionId, string $data)
{
$expiry = new \MongoDB\BSON\UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
$expiry = new UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
$fields = [
$this->options['time_field'] => new \MongoDB\BSON\UTCDateTime(),
$this->options['time_field'] => new UTCDateTime(),
$this->options['expiry_field'] => $expiry,
$this->options['data_field'] => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_OLD_BINARY),
$this->options['data_field'] => new Binary($data, Binary::TYPE_OLD_BINARY),
];
$this->getCollection()->updateOne(
@@ -139,12 +144,12 @@ class MongoDbSessionHandler extends AbstractSessionHandler
#[\ReturnTypeWillChange]
public function updateTimestamp($sessionId, $data)
{
$expiry = new \MongoDB\BSON\UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
$expiry = new UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
$this->getCollection()->updateOne(
[$this->options['id_field'] => $sessionId],
['$set' => [
$this->options['time_field'] => new \MongoDB\BSON\UTCDateTime(),
$this->options['time_field'] => new UTCDateTime(),
$this->options['expiry_field'] => $expiry,
]]
);
@@ -155,11 +160,11 @@ class MongoDbSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doRead($sessionId)
protected function doRead(string $sessionId)
{
$dbData = $this->getCollection()->findOne([
$this->options['id_field'] => $sessionId,
$this->options['expiry_field'] => ['$gte' => new \MongoDB\BSON\UTCDateTime()],
$this->options['expiry_field'] => ['$gte' => new UTCDateTime()],
]);
if (null === $dbData) {
@@ -169,7 +174,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
return $dbData[$this->options['data_field']]->getData();
}
private function getCollection(): \MongoDB\Collection
private function getCollection(): Collection
{
if (null === $this->collection) {
$this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
@@ -179,7 +184,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
}
/**
* @return \MongoDB\Client
* @return Client
*/
protected function getMongo()
{

View File

@@ -39,7 +39,7 @@ class NullSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doRead($sessionId)
protected function doRead(string $sessionId)
{
return '';
}
@@ -56,7 +56,7 @@ class NullSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doWrite($sessionId, $data)
protected function doWrite(string $sessionId, string $data)
{
return true;
}
@@ -64,7 +64,7 @@ class NullSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doDestroy($sessionId)
protected function doDestroy(string $sessionId)
{
return true;
}

View File

@@ -73,57 +73,67 @@ class PdoSessionHandler extends AbstractSessionHandler
private $pdo;
/**
* @var string|false|null DSN string or null for session.save_path or false when lazy connection disabled
* DSN string or null for session.save_path or false when lazy connection disabled.
*
* @var string|false|null
*/
private $dsn = false;
/**
* @var string Database driver
* @var string|null
*/
private $driver;
/**
* @var string Table name
* @var string
*/
private $table = 'sessions';
/**
* @var string Column for session id
* @var string
*/
private $idCol = 'sess_id';
/**
* @var string Column for session data
* @var string
*/
private $dataCol = 'sess_data';
/**
* @var string Column for lifetime
* @var string
*/
private $lifetimeCol = 'sess_lifetime';
/**
* @var string Column for timestamp
* @var string
*/
private $timeCol = 'sess_time';
/**
* @var string Username when lazy-connect
* Username when lazy-connect.
*
* @var string
*/
private $username = '';
/**
* @var string Password when lazy-connect
* Password when lazy-connect.
*
* @var string
*/
private $password = '';
/**
* @var array Connection options when lazy-connect
* Connection options when lazy-connect.
*
* @var array
*/
private $connectionOptions = [];
/**
* @var int The strategy for locking, see constants
* The strategy for locking, see constants.
*
* @var int
*/
private $lockMode = self::LOCK_TRANSACTIONAL;
@@ -135,17 +145,23 @@ class PdoSessionHandler extends AbstractSessionHandler
private $unlockStatements = [];
/**
* @var bool True when the current session exists but expired according to session.gc_maxlifetime
* True when the current session exists but expired according to session.gc_maxlifetime.
*
* @var bool
*/
private $sessionExpired = false;
/**
* @var bool Whether a transaction is active
* Whether a transaction is active.
*
* @var bool
*/
private $inTransaction = false;
/**
* @var bool Whether gc() has been called
* Whether gc() has been called.
*
* @var bool
*/
private $gcCalled = false;
@@ -252,7 +268,7 @@ class PdoSessionHandler extends AbstractSessionHandler
*
* Can be used to distinguish between a new session and one that expired due to inactivity.
*
* @return bool Whether current session expired
* @return bool
*/
public function isSessionExpired()
{
@@ -305,7 +321,7 @@ class PdoSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doDestroy($sessionId)
protected function doDestroy(string $sessionId)
{
// delete the record associated with this id
$sql = "DELETE FROM $this->table WHERE $this->idCol = :id";
@@ -326,7 +342,7 @@ class PdoSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doWrite($sessionId, $data)
protected function doWrite(string $sessionId, string $data)
{
$maxlifetime = (int) \ini_get('session.gc_maxlifetime');
@@ -430,6 +446,7 @@ class PdoSessionHandler extends AbstractSessionHandler
if (false !== $this->dsn) {
$this->pdo = null; // only close lazy-connection
$this->driver = null;
}
return true;
@@ -491,10 +508,32 @@ class PdoSessionHandler extends AbstractSessionHandler
$driver = substr($driver, 4);
}
$dsn = null;
switch ($driver) {
case 'mysql':
$dsn = 'mysql:';
if ('' !== ($params['query'] ?? '')) {
$queryParams = [];
parse_str($params['query'], $queryParams);
if ('' !== ($queryParams['charset'] ?? '')) {
$dsn .= 'charset='.$queryParams['charset'].';';
}
if ('' !== ($queryParams['unix_socket'] ?? '')) {
$dsn .= 'unix_socket='.$queryParams['unix_socket'].';';
if (isset($params['path'])) {
$dbName = substr($params['path'], 1); // Remove the leading slash
$dsn .= 'dbname='.$dbName.';';
}
return $dsn;
}
}
// If "unix_socket" is not in the query, we continue with the same process as pgsql
// no break
case 'pgsql':
$dsn = $driver.':';
$dsn ?? $dsn = 'pgsql:';
if (isset($params['host']) && '' !== $params['host']) {
$dsn .= 'host='.$params['host'].';';
@@ -611,11 +650,9 @@ class PdoSessionHandler extends AbstractSessionHandler
* We need to make sure we do not return session data that is already considered garbage according
* to the session.gc_maxlifetime setting because gc() is called after read() and only sometimes.
*
* @param string $sessionId Session ID
*
* @return string The session data
* @return string
*/
protected function doRead($sessionId)
protected function doRead(string $sessionId)
{
if (self::LOCK_ADVISORY === $this->lockMode) {
$this->unlockStatements[] = $this->doAdvisoryLock($sessionId);

View File

@@ -54,7 +54,7 @@ class RedisSessionHandler extends AbstractSessionHandler
!$redis instanceof RedisProxy &&
!$redis instanceof RedisClusterProxy
) {
throw new \InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
throw new \InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, get_debug_type($redis)));
}
if ($diff = array_diff(array_keys($options), ['prefix', 'ttl'])) {
@@ -69,7 +69,7 @@ class RedisSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doRead($sessionId): string
protected function doRead(string $sessionId): string
{
return $this->redis->get($this->prefix.$sessionId) ?: '';
}
@@ -77,7 +77,7 @@ class RedisSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doWrite($sessionId, $data): bool
protected function doWrite(string $sessionId, string $data): bool
{
$result = $this->redis->setEx($this->prefix.$sessionId, (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')), $data);
@@ -87,9 +87,21 @@ class RedisSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doDestroy($sessionId): bool
protected function doDestroy(string $sessionId): bool
{
$this->redis->del($this->prefix.$sessionId);
static $unlink = true;
if ($unlink) {
try {
$unlink = false !== $this->redis->unlink($this->prefix.$sessionId);
} catch (\Throwable $e) {
$unlink = false;
}
}
if (!$unlink) {
$this->redis->del($this->prefix.$sessionId);
}
return true;
}

View File

@@ -27,7 +27,11 @@ class SessionHandlerFactory
public static function createHandler($connection): AbstractSessionHandler
{
if (!\is_string($connection) && !\is_object($connection)) {
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, \gettype($connection)));
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, get_debug_type($connection)));
}
if ($options = \is_string($connection) ? parse_url($connection) : false) {
parse_str($options['query'] ?? '', $options);
}
switch (true) {
@@ -46,7 +50,7 @@ class SessionHandlerFactory
return new PdoSessionHandler($connection);
case !\is_string($connection):
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', \get_class($connection)));
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
case str_starts_with($connection, 'file://'):
$savePath = substr($connection, 7);
@@ -61,7 +65,7 @@ class SessionHandlerFactory
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
return new $handlerClass($connection);
return new $handlerClass($connection, array_intersect_key($options ?: [], ['prefix' => 1, 'ttl' => 1]));
case str_starts_with($connection, 'pdo_oci://'):
if (!class_exists(DriverManager::class)) {
@@ -79,7 +83,7 @@ class SessionHandlerFactory
case str_starts_with($connection, 'sqlsrv://'):
case str_starts_with($connection, 'sqlite://'):
case str_starts_with($connection, 'sqlite3://'):
return new PdoSessionHandler($connection);
return new PdoSessionHandler($connection, $options ?: []);
}
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));

View File

@@ -24,7 +24,7 @@ class StrictSessionHandler extends AbstractSessionHandler
public function __construct(\SessionHandlerInterface $handler)
{
if ($handler instanceof \SessionUpdateTimestampHandlerInterface) {
throw new \LogicException(sprintf('"%s" is already an instance of "SessionUpdateTimestampHandlerInterface", you cannot wrap it with "%s".', \get_class($handler), self::class));
throw new \LogicException(sprintf('"%s" is already an instance of "SessionUpdateTimestampHandlerInterface", you cannot wrap it with "%s".', get_debug_type($handler), self::class));
}
$this->handler = $handler;
@@ -54,7 +54,7 @@ class StrictSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doRead($sessionId)
protected function doRead(string $sessionId)
{
return $this->handler->read($sessionId);
}
@@ -71,7 +71,7 @@ class StrictSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doWrite($sessionId, $data)
protected function doWrite(string $sessionId, string $data)
{
return $this->handler->write($sessionId, $data);
}
@@ -91,7 +91,7 @@ class StrictSessionHandler extends AbstractSessionHandler
/**
* {@inheritdoc}
*/
protected function doDestroy($sessionId)
protected function doDestroy(string $sessionId)
{
$this->doDestroy = false;

View File

@@ -100,7 +100,7 @@ class MetadataBag implements SessionBagInterface
* to expire with browser session. Time is in seconds, and is
* not a Unix timestamp.
*/
public function stampNew($lifetime = null)
public function stampNew(int $lifetime = null)
{
$this->stampCreated($lifetime);
}
@@ -139,6 +139,7 @@ class MetadataBag implements SessionBagInterface
public function clear()
{
// nothing to do
return null;
}
/**
@@ -151,10 +152,8 @@ class MetadataBag implements SessionBagInterface
/**
* Sets name.
*
* @param string $name
*/
public function setName($name)
public function setName(string $name)
{
$this->name = $name;
}

View File

@@ -94,7 +94,7 @@ class MockArraySessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function regenerate($destroy = false, $lifetime = null)
public function regenerate(bool $destroy = false, int $lifetime = null)
{
if (!$this->started) {
$this->start();
@@ -117,7 +117,7 @@ class MockArraySessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function setId($id)
public function setId(string $id)
{
if ($this->started) {
throw new \LogicException('Cannot set session ID after the session has started.');
@@ -137,7 +137,7 @@ class MockArraySessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function setName($name)
public function setName(string $name)
{
$this->name = $name;
}
@@ -183,7 +183,7 @@ class MockArraySessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function getBag($name)
public function getBag(string $name)
{
if (!isset($this->bags[$name])) {
throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));

View File

@@ -28,8 +28,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
private $savePath;
/**
* @param string $savePath Path of directory to save session files
* @param string $name Session name
* @param string|null $savePath Path of directory to save session files
*/
public function __construct(string $savePath = null, string $name = 'MOCKSESSID', MetadataBag $metaBag = null)
{
@@ -69,7 +68,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
/**
* {@inheritdoc}
*/
public function regenerate($destroy = false, $lifetime = null)
public function regenerate(bool $destroy = false, int $lifetime = null)
{
if (!$this->started) {
$this->start();

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Request;
// Help opcache.preload discover always-needed symbols
class_exists(MockFileSessionStorage::class);
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class MockFileSessionStorageFactory implements SessionStorageFactoryInterface
{
private $savePath;
private $name;
private $metaBag;
/**
* @see MockFileSessionStorage constructor.
*/
public function __construct(string $savePath = null, string $name = 'MOCKSESSID', MetadataBag $metaBag = null)
{
$this->savePath = $savePath;
$this->name = $name;
$this->metaBag = $metaBag;
}
public function createStorage(?Request $request): SessionStorageInterface
{
return new MockFileSessionStorage($this->savePath, $this->name, $this->metaBag);
}
}

View File

@@ -90,13 +90,6 @@ class NativeSessionStorage implements SessionStorageInterface
* use_cookies, "1"
* use_only_cookies, "1"
* use_trans_sid, "0"
* upload_progress.enabled, "1"
* upload_progress.cleanup, "1"
* upload_progress.prefix, "upload_progress_"
* upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
* upload_progress.freq, "1%"
* upload_progress.min-freq, "1"
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
* sid_length, "32"
* sid_bits_per_character, "5"
* trans_sid_hosts, $_SERVER['HTTP_HOST']
@@ -216,7 +209,7 @@ class NativeSessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function setId($id)
public function setId(string $id)
{
$this->saveHandler->setId($id);
}
@@ -232,7 +225,7 @@ class NativeSessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function setName($name)
public function setName(string $name)
{
$this->saveHandler->setName($name);
}
@@ -240,7 +233,7 @@ class NativeSessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function regenerate($destroy = false, $lifetime = null)
public function regenerate(bool $destroy = false, int $lifetime = null)
{
// Cannot regenerate the session ID for non-active sessions.
if (\PHP_SESSION_ACTIVE !== session_status()) {
@@ -347,7 +340,7 @@ class NativeSessionStorage implements SessionStorageInterface
/**
* {@inheritdoc}
*/
public function getBag($name)
public function getBag(string $name)
{
if (!isset($this->bags[$name])) {
throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
@@ -419,6 +412,13 @@ class NativeSessionStorage implements SessionStorageInterface
foreach ($options as $key => $value) {
if (isset($validOptions[$key])) {
if (str_starts_with($key, 'upload_progress.')) {
trigger_deprecation('symfony/http-foundation', '5.4', 'Support for the "%s" session option is deprecated. The settings prefixed with "session.upload_progress." can not be changed at runtime.', $key);
continue;
}
if ('url_rewriter.tags' === $key) {
trigger_deprecation('symfony/http-foundation', '5.4', 'Support for the "%s" session option is deprecated. Use "trans_sid_tags" instead.', $key);
}
if ('cookie_samesite' === $key && \PHP_VERSION_ID < 70300) {
// PHP < 7.3 does not support same_site cookies. We will emulate it in
// the start() method instead.

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Request;
// Help opcache.preload discover always-needed symbols
class_exists(NativeSessionStorage::class);
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class NativeSessionStorageFactory implements SessionStorageFactoryInterface
{
private $options;
private $handler;
private $metaBag;
private $secure;
/**
* @see NativeSessionStorage constructor.
*/
public function __construct(array $options = [], $handler = null, MetadataBag $metaBag = null, bool $secure = false)
{
$this->options = $options;
$this->handler = $handler;
$this->metaBag = $metaBag;
$this->secure = $secure;
}
public function createStorage(?Request $request): SessionStorageInterface
{
$storage = new NativeSessionStorage($this->options, $this->handler, $this->metaBag);
if ($this->secure && $request && $request->isSecure()) {
$storage->setOptions(['cookie_secure' => true]);
}
return $storage;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Request;
// Help opcache.preload discover always-needed symbols
class_exists(PhpBridgeSessionStorage::class);
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class PhpBridgeSessionStorageFactory implements SessionStorageFactoryInterface
{
private $handler;
private $metaBag;
private $secure;
/**
* @see PhpBridgeSessionStorage constructor.
*/
public function __construct($handler = null, MetadataBag $metaBag = null, bool $secure = false)
{
$this->handler = $handler;
$this->metaBag = $metaBag;
$this->secure = $secure;
}
public function createStorage(?Request $request): SessionStorageInterface
{
$storage = new PhpBridgeSessionStorage($this->handler, $this->metaBag);
if ($this->secure && $request && $request->isSecure()) {
$storage->setOptions(['cookie_secure' => true]);
}
return $storage;
}
}

View File

@@ -81,11 +81,9 @@ abstract class AbstractProxy
/**
* Sets the session ID.
*
* @param string $id
*
* @throws \LogicException
*/
public function setId($id)
public function setId(string $id)
{
if ($this->isActive()) {
throw new \LogicException('Cannot change the ID of an active session.');
@@ -107,11 +105,9 @@ abstract class AbstractProxy
/**
* Sets the session name.
*
* @param string $name
*
* @throws \LogicException
*/
public function setName($name)
public function setName(string $name)
{
if ($this->isActive()) {
throw new \LogicException('Cannot change the name of an active session.');

View File

@@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*
* @internal to be removed in Symfony 6
*/
final class ServiceSessionFactory implements SessionStorageFactoryInterface
{
private $storage;
public function __construct(SessionStorageInterface $storage)
{
$this->storage = $storage;
}
public function createStorage(?Request $request): SessionStorageInterface
{
if ($this->storage instanceof NativeSessionStorage && $request && $request->isSecure()) {
$this->storage->setOptions(['cookie_secure' => true]);
}
return $this->storage;
}
}

View File

@@ -0,0 +1,25 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
interface SessionStorageFactoryInterface
{
/**
* Creates a new instance of SessionStorageInterface.
*/
public function createStorage(?Request $request): SessionStorageInterface;
}

View File

@@ -24,7 +24,7 @@ interface SessionStorageInterface
/**
* Starts the session.
*
* @return bool True if started
* @return bool
*
* @throws \RuntimeException if something goes wrong starting the session
*/
@@ -33,37 +33,33 @@ interface SessionStorageInterface
/**
* Checks if the session is started.
*
* @return bool True if started, false otherwise
* @return bool
*/
public function isStarted();
/**
* Returns the session ID.
*
* @return string The session ID or empty
* @return string
*/
public function getId();
/**
* Sets the session ID.
*
* @param string $id
*/
public function setId($id);
public function setId(string $id);
/**
* Returns the session name.
*
* @return mixed The session name
* @return string
*/
public function getName();
/**
* Sets the session name.
*
* @param string $name
*/
public function setName($name);
public function setName(string $name);
/**
* Regenerates id that represents this storage.
@@ -90,11 +86,11 @@ interface SessionStorageInterface
* to expire with browser session. Time is in seconds, and is
* not a Unix timestamp.
*
* @return bool True if session regenerated, false if error
* @return bool
*
* @throws \RuntimeException If an error occurs while regenerating this storage
*/
public function regenerate($destroy = false, $lifetime = null);
public function regenerate(bool $destroy = false, int $lifetime = null);
/**
* Force the session to be saved and closed.
@@ -117,13 +113,11 @@ interface SessionStorageInterface
/**
* Gets a SessionBagInterface by name.
*
* @param string $name
*
* @return SessionBagInterface
*
* @throws \InvalidArgumentException If the bag does not exist
*/
public function getBag($name);
public function getBag(string $name);
/**
* Registers a SessionBagInterface for use.