fix:更新已知bug,优化代码
This commit is contained in:
@@ -18,8 +18,8 @@ namespace Symfony\Component\HttpFoundation\Session\Attribute;
|
||||
*/
|
||||
class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable
|
||||
{
|
||||
private $name = 'attributes';
|
||||
private $storageKey;
|
||||
private string $name = 'attributes';
|
||||
private string $storageKey;
|
||||
|
||||
protected $attributes = [];
|
||||
|
||||
@@ -34,7 +34,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
@@ -55,7 +55,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorageKey()
|
||||
public function getStorageKey(): string
|
||||
{
|
||||
return $this->storageKey;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has(string $name)
|
||||
public function has(string $name): bool
|
||||
{
|
||||
return \array_key_exists($name, $this->attributes);
|
||||
}
|
||||
@@ -71,7 +71,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get(string $name, $default = null)
|
||||
public function get(string $name, mixed $default = null): mixed
|
||||
{
|
||||
return \array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
|
||||
}
|
||||
@@ -79,7 +79,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set(string $name, $value)
|
||||
public function set(string $name, mixed $value)
|
||||
{
|
||||
$this->attributes[$name] = $value;
|
||||
}
|
||||
@@ -87,7 +87,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
public function all(): array
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
@@ -106,7 +106,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove(string $name)
|
||||
public function remove(string $name): mixed
|
||||
{
|
||||
$retval = null;
|
||||
if (\array_key_exists($name, $this->attributes)) {
|
||||
@@ -120,7 +120,7 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
public function clear(): mixed
|
||||
{
|
||||
$return = $this->attributes;
|
||||
$this->attributes = [];
|
||||
@@ -133,19 +133,15 @@ class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Counta
|
||||
*
|
||||
* @return \ArrayIterator<string, mixed>
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator()
|
||||
public function getIterator(): \ArrayIterator
|
||||
{
|
||||
return new \ArrayIterator($this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of attributes.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function count()
|
||||
public function count(): int
|
||||
{
|
||||
return \count($this->attributes);
|
||||
}
|
||||
|
||||
@@ -22,33 +22,25 @@ interface AttributeBagInterface extends SessionBagInterface
|
||||
{
|
||||
/**
|
||||
* Checks if an attribute is defined.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name);
|
||||
public function has(string $name): bool;
|
||||
|
||||
/**
|
||||
* Returns an attribute.
|
||||
*
|
||||
* @param mixed $default The default value if not found
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name, $default = null);
|
||||
public function get(string $name, mixed $default = null): mixed;
|
||||
|
||||
/**
|
||||
* Sets an attribute.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set(string $name, $value);
|
||||
public function set(string $name, mixed $value);
|
||||
|
||||
/**
|
||||
* Returns attributes.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function all();
|
||||
public function all(): array;
|
||||
|
||||
public function replace(array $attributes);
|
||||
|
||||
@@ -57,5 +49,5 @@ interface AttributeBagInterface extends SessionBagInterface
|
||||
*
|
||||
* @return mixed The removed value or null when it does not exist
|
||||
*/
|
||||
public function remove(string $name);
|
||||
public function remove(string $name): mixed;
|
||||
}
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
<?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\Attribute;
|
||||
|
||||
trigger_deprecation('symfony/http-foundation', '5.3', 'The "%s" class is deprecated.', NamespacedAttributeBag::class);
|
||||
|
||||
/**
|
||||
* This class provides structured storage of session attributes using
|
||||
* a name spacing character in the key.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @deprecated since Symfony 5.3
|
||||
*/
|
||||
class NamespacedAttributeBag extends AttributeBag
|
||||
{
|
||||
private $namespaceCharacter;
|
||||
|
||||
/**
|
||||
* @param string $storageKey Session storage key
|
||||
* @param string $namespaceCharacter Namespace character to use in keys
|
||||
*/
|
||||
public function __construct(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')
|
||||
{
|
||||
$this->namespaceCharacter = $namespaceCharacter;
|
||||
parent::__construct($storageKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has(string $name)
|
||||
{
|
||||
// reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
|
||||
$attributes = $this->resolveAttributePath($name);
|
||||
$name = $this->resolveKey($name);
|
||||
|
||||
if (null === $attributes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return \array_key_exists($name, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get(string $name, $default = null)
|
||||
{
|
||||
// reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
|
||||
$attributes = $this->resolveAttributePath($name);
|
||||
$name = $this->resolveKey($name);
|
||||
|
||||
if (null === $attributes) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return \array_key_exists($name, $attributes) ? $attributes[$name] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set(string $name, $value)
|
||||
{
|
||||
$attributes = &$this->resolveAttributePath($name, true);
|
||||
$name = $this->resolveKey($name);
|
||||
$attributes[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove(string $name)
|
||||
{
|
||||
$retval = null;
|
||||
$attributes = &$this->resolveAttributePath($name);
|
||||
$name = $this->resolveKey($name);
|
||||
if (null !== $attributes && \array_key_exists($name, $attributes)) {
|
||||
$retval = $attributes[$name];
|
||||
unset($attributes[$name]);
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a path in attributes property and returns it as a reference.
|
||||
*
|
||||
* This method allows structured namespacing of session attributes.
|
||||
*
|
||||
* @param string $name Key name
|
||||
* @param bool $writeContext Write context, default false
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
protected function &resolveAttributePath(string $name, bool $writeContext = false)
|
||||
{
|
||||
$array = &$this->attributes;
|
||||
$name = (str_starts_with($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
|
||||
|
||||
// Check if there is anything to do, else return
|
||||
if (!$name) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
$parts = explode($this->namespaceCharacter, $name);
|
||||
if (\count($parts) < 2) {
|
||||
if (!$writeContext) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
$array[$parts[0]] = [];
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
unset($parts[\count($parts) - 1]);
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if (null !== $array && !\array_key_exists($part, $array)) {
|
||||
if (!$writeContext) {
|
||||
$null = null;
|
||||
|
||||
return $null;
|
||||
}
|
||||
|
||||
$array[$part] = [];
|
||||
}
|
||||
|
||||
$array = &$array[$part];
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the key from the name.
|
||||
*
|
||||
* This is the last part in a dot separated string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function resolveKey(string $name)
|
||||
{
|
||||
if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
|
||||
$name = substr($name, $pos + 1);
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,9 @@ namespace Symfony\Component\HttpFoundation\Session\Flash;
|
||||
*/
|
||||
class AutoExpireFlashBag implements FlashBagInterface
|
||||
{
|
||||
private $name = 'flashes';
|
||||
private $flashes = ['display' => [], 'new' => []];
|
||||
private $storageKey;
|
||||
private string $name = 'flashes';
|
||||
private array $flashes = ['display' => [], 'new' => []];
|
||||
private string $storageKey;
|
||||
|
||||
/**
|
||||
* @param string $storageKey The key used to store flashes in the session
|
||||
@@ -33,7 +33,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add(string $type, $message)
|
||||
public function add(string $type, mixed $message)
|
||||
{
|
||||
$this->flashes['new'][$type][] = $message;
|
||||
}
|
||||
@@ -68,7 +68,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peek(string $type, array $default = [])
|
||||
public function peek(string $type, array $default = []): array
|
||||
{
|
||||
return $this->has($type) ? $this->flashes['display'][$type] : $default;
|
||||
}
|
||||
@@ -76,7 +76,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peekAll()
|
||||
public function peekAll(): array
|
||||
{
|
||||
return \array_key_exists('display', $this->flashes) ? $this->flashes['display'] : [];
|
||||
}
|
||||
@@ -84,7 +84,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get(string $type, array $default = [])
|
||||
public function get(string $type, array $default = []): array
|
||||
{
|
||||
$return = $default;
|
||||
|
||||
@@ -103,7 +103,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
public function all(): array
|
||||
{
|
||||
$return = $this->flashes['display'];
|
||||
$this->flashes['display'] = [];
|
||||
@@ -122,7 +122,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set(string $type, $messages)
|
||||
public function set(string $type, string|array $messages)
|
||||
{
|
||||
$this->flashes['new'][$type] = (array) $messages;
|
||||
}
|
||||
@@ -130,7 +130,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has(string $type)
|
||||
public function has(string $type): bool
|
||||
{
|
||||
return \array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];
|
||||
}
|
||||
@@ -138,7 +138,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function keys()
|
||||
public function keys(): array
|
||||
{
|
||||
return array_keys($this->flashes['display']);
|
||||
}
|
||||
@@ -146,7 +146,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorageKey()
|
||||
public function getStorageKey(): string
|
||||
{
|
||||
return $this->storageKey;
|
||||
}
|
||||
@@ -154,7 +154,7 @@ class AutoExpireFlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
public function clear(): mixed
|
||||
{
|
||||
return $this->all();
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ namespace Symfony\Component\HttpFoundation\Session\Flash;
|
||||
*/
|
||||
class FlashBag implements FlashBagInterface
|
||||
{
|
||||
private $name = 'flashes';
|
||||
private $flashes = [];
|
||||
private $storageKey;
|
||||
private string $name = 'flashes';
|
||||
private array $flashes = [];
|
||||
private string $storageKey;
|
||||
|
||||
/**
|
||||
* @param string $storageKey The key used to store flashes in the session
|
||||
@@ -33,7 +33,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
@@ -54,7 +54,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add(string $type, $message)
|
||||
public function add(string $type, mixed $message)
|
||||
{
|
||||
$this->flashes[$type][] = $message;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peek(string $type, array $default = [])
|
||||
public function peek(string $type, array $default = []): array
|
||||
{
|
||||
return $this->has($type) ? $this->flashes[$type] : $default;
|
||||
}
|
||||
@@ -70,7 +70,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peekAll()
|
||||
public function peekAll(): array
|
||||
{
|
||||
return $this->flashes;
|
||||
}
|
||||
@@ -78,7 +78,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get(string $type, array $default = [])
|
||||
public function get(string $type, array $default = []): array
|
||||
{
|
||||
if (!$this->has($type)) {
|
||||
return $default;
|
||||
@@ -94,7 +94,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
public function all(): array
|
||||
{
|
||||
$return = $this->peekAll();
|
||||
$this->flashes = [];
|
||||
@@ -105,7 +105,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set(string $type, $messages)
|
||||
public function set(string $type, string|array $messages)
|
||||
{
|
||||
$this->flashes[$type] = (array) $messages;
|
||||
}
|
||||
@@ -121,7 +121,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has(string $type)
|
||||
public function has(string $type): bool
|
||||
{
|
||||
return \array_key_exists($type, $this->flashes) && $this->flashes[$type];
|
||||
}
|
||||
@@ -129,7 +129,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function keys()
|
||||
public function keys(): array
|
||||
{
|
||||
return array_keys($this->flashes);
|
||||
}
|
||||
@@ -137,7 +137,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorageKey()
|
||||
public function getStorageKey(): string
|
||||
{
|
||||
return $this->storageKey;
|
||||
}
|
||||
@@ -145,7 +145,7 @@ class FlashBag implements FlashBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
public function clear(): mixed
|
||||
{
|
||||
return $this->all();
|
||||
}
|
||||
|
||||
@@ -22,50 +22,38 @@ interface FlashBagInterface extends SessionBagInterface
|
||||
{
|
||||
/**
|
||||
* Adds a flash message for the given type.
|
||||
*
|
||||
* @param mixed $message
|
||||
*/
|
||||
public function add(string $type, $message);
|
||||
public function add(string $type, mixed $message);
|
||||
|
||||
/**
|
||||
* Registers one or more messages for a given type.
|
||||
*
|
||||
* @param string|array $messages
|
||||
*/
|
||||
public function set(string $type, $messages);
|
||||
public function set(string $type, string|array $messages);
|
||||
|
||||
/**
|
||||
* Gets flash messages for a given type.
|
||||
*
|
||||
* @param string $type Message category type
|
||||
* @param array $default Default value if $type does not exist
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function peek(string $type, array $default = []);
|
||||
public function peek(string $type, array $default = []): array;
|
||||
|
||||
/**
|
||||
* Gets all flash messages.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function peekAll();
|
||||
public function peekAll(): array;
|
||||
|
||||
/**
|
||||
* Gets and clears flash from the stack.
|
||||
*
|
||||
* @param array $default Default value if $type does not exist
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get(string $type, array $default = []);
|
||||
public function get(string $type, array $default = []): array;
|
||||
|
||||
/**
|
||||
* Gets and clears flashes from the stack.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all();
|
||||
public function all(): array;
|
||||
|
||||
/**
|
||||
* Sets all flash messages.
|
||||
@@ -74,15 +62,11 @@ interface FlashBagInterface extends SessionBagInterface
|
||||
|
||||
/**
|
||||
* Has flash messages for a given type?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $type);
|
||||
public function has(string $type): bool;
|
||||
|
||||
/**
|
||||
* Returns a list of all defined types.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function keys();
|
||||
public function keys(): array;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
|
||||
|
||||
@@ -33,16 +34,16 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
{
|
||||
protected $storage;
|
||||
|
||||
private $flashName;
|
||||
private $attributeName;
|
||||
private $data = [];
|
||||
private $usageIndex = 0;
|
||||
private $usageReporter;
|
||||
private string $flashName;
|
||||
private string $attributeName;
|
||||
private array $data = [];
|
||||
private int $usageIndex = 0;
|
||||
private ?\Closure $usageReporter;
|
||||
|
||||
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, callable $usageReporter = null)
|
||||
{
|
||||
$this->storage = $storage ?? new NativeSessionStorage();
|
||||
$this->usageReporter = $usageReporter;
|
||||
$this->usageReporter = $usageReporter instanceof \Closure || !\is_callable($usageReporter) ? $usageReporter : \Closure::fromCallable($usageReporter);
|
||||
|
||||
$attributes = $attributes ?? new AttributeBag();
|
||||
$this->attributeName = $attributes->getName();
|
||||
@@ -56,7 +57,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
public function start(): bool
|
||||
{
|
||||
return $this->storage->start();
|
||||
}
|
||||
@@ -64,7 +65,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has(string $name)
|
||||
public function has(string $name): bool
|
||||
{
|
||||
return $this->getAttributeBag()->has($name);
|
||||
}
|
||||
@@ -72,7 +73,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get(string $name, $default = null)
|
||||
public function get(string $name, mixed $default = null): mixed
|
||||
{
|
||||
return $this->getAttributeBag()->get($name, $default);
|
||||
}
|
||||
@@ -80,7 +81,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set(string $name, $value)
|
||||
public function set(string $name, mixed $value)
|
||||
{
|
||||
$this->getAttributeBag()->set($name, $value);
|
||||
}
|
||||
@@ -88,7 +89,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
public function all(): array
|
||||
{
|
||||
return $this->getAttributeBag()->all();
|
||||
}
|
||||
@@ -104,7 +105,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove(string $name)
|
||||
public function remove(string $name): mixed
|
||||
{
|
||||
return $this->getAttributeBag()->remove($name);
|
||||
}
|
||||
@@ -120,7 +121,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isStarted()
|
||||
public function isStarted(): bool
|
||||
{
|
||||
return $this->storage->isStarted();
|
||||
}
|
||||
@@ -130,19 +131,15 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
*
|
||||
* @return \ArrayIterator<string, mixed>
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator()
|
||||
public function getIterator(): \ArrayIterator
|
||||
{
|
||||
return new \ArrayIterator($this->getAttributeBag()->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of attributes.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function count()
|
||||
public function count(): int
|
||||
{
|
||||
return \count($this->getAttributeBag()->all());
|
||||
}
|
||||
@@ -175,7 +172,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function invalidate(int $lifetime = null)
|
||||
public function invalidate(int $lifetime = null): bool
|
||||
{
|
||||
$this->storage->clear();
|
||||
|
||||
@@ -185,7 +182,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function migrate(bool $destroy = false, int $lifetime = null)
|
||||
public function migrate(bool $destroy = false, int $lifetime = null): bool
|
||||
{
|
||||
return $this->storage->regenerate($destroy, $lifetime);
|
||||
}
|
||||
@@ -201,7 +198,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->storage->getId();
|
||||
}
|
||||
@@ -219,7 +216,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->storage->getName();
|
||||
}
|
||||
@@ -235,7 +232,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadataBag()
|
||||
public function getMetadataBag(): MetadataBag
|
||||
{
|
||||
++$this->usageIndex;
|
||||
if ($this->usageReporter && 0 <= $this->usageIndex) {
|
||||
@@ -256,7 +253,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBag(string $name)
|
||||
public function getBag(string $name): SessionBagInterface
|
||||
{
|
||||
$bag = $this->storage->getBag($name);
|
||||
|
||||
@@ -265,10 +262,8 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
|
||||
/**
|
||||
* Gets the flashbag interface.
|
||||
*
|
||||
* @return FlashBagInterface
|
||||
*/
|
||||
public function getFlashBag()
|
||||
public function getFlashBag(): FlashBagInterface
|
||||
{
|
||||
return $this->getBag($this->flashName);
|
||||
}
|
||||
|
||||
@@ -20,10 +20,8 @@ interface SessionBagInterface
|
||||
{
|
||||
/**
|
||||
* Gets this bag's name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Initializes the Bag.
|
||||
@@ -32,15 +30,13 @@ interface SessionBagInterface
|
||||
|
||||
/**
|
||||
* Gets the storage key for this bag.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getStorageKey();
|
||||
public function getStorageKey(): string;
|
||||
|
||||
/**
|
||||
* Clears out data from bag.
|
||||
*
|
||||
* @return mixed Whatever data was contained
|
||||
*/
|
||||
public function clear();
|
||||
public function clear(): mixed;
|
||||
}
|
||||
|
||||
@@ -19,16 +19,16 @@ namespace Symfony\Component\HttpFoundation\Session;
|
||||
final class SessionBagProxy implements SessionBagInterface
|
||||
{
|
||||
private $bag;
|
||||
private $data;
|
||||
private $usageIndex;
|
||||
private $usageReporter;
|
||||
private array $data;
|
||||
private ?int $usageIndex;
|
||||
private ?\Closure $usageReporter;
|
||||
|
||||
public function __construct(SessionBagInterface $bag, array &$data, ?int &$usageIndex, ?callable $usageReporter)
|
||||
{
|
||||
$this->bag = $bag;
|
||||
$this->data = &$data;
|
||||
$this->usageIndex = &$usageIndex;
|
||||
$this->usageReporter = $usageReporter;
|
||||
$this->usageReporter = $usageReporter instanceof \Closure || !\is_callable($usageReporter) ? $usageReporter : \Closure::fromCallable($usageReporter);
|
||||
}
|
||||
|
||||
public function getBag(): SessionBagInterface
|
||||
@@ -88,7 +88,7 @@ final class SessionBagProxy implements SessionBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
public function clear(): mixed
|
||||
{
|
||||
return $this->bag->clear();
|
||||
}
|
||||
|
||||
@@ -24,13 +24,13 @@ class SessionFactory implements SessionFactoryInterface
|
||||
{
|
||||
private $requestStack;
|
||||
private $storageFactory;
|
||||
private $usageReporter;
|
||||
private ?\Closure $usageReporter;
|
||||
|
||||
public function __construct(RequestStack $requestStack, SessionStorageFactoryInterface $storageFactory, callable $usageReporter = null)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->storageFactory = $storageFactory;
|
||||
$this->usageReporter = $usageReporter;
|
||||
$this->usageReporter = $usageReporter instanceof \Closure || !\is_callable($usageReporter) ? $usageReporter : \Closure::fromCallable($usageReporter);
|
||||
}
|
||||
|
||||
public function createSession(): SessionInterface
|
||||
|
||||
@@ -23,18 +23,14 @@ interface SessionInterface
|
||||
/**
|
||||
* Starts the session storage.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \RuntimeException if session fails to start
|
||||
*/
|
||||
public function start();
|
||||
public function start(): bool;
|
||||
|
||||
/**
|
||||
* Returns the session ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId();
|
||||
public function getId(): string;
|
||||
|
||||
/**
|
||||
* Sets the session ID.
|
||||
@@ -43,10 +39,8 @@ interface SessionInterface
|
||||
|
||||
/**
|
||||
* Returns the session name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Sets the session name.
|
||||
@@ -63,10 +57,8 @@ interface SessionInterface
|
||||
* will leave the system settings unchanged, 0 sets the cookie
|
||||
* to expire with browser session. Time is in seconds, and is
|
||||
* not a Unix timestamp.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function invalidate(int $lifetime = null);
|
||||
public function invalidate(int $lifetime = null): bool;
|
||||
|
||||
/**
|
||||
* Migrates the current session to a new session id while maintaining all
|
||||
@@ -77,10 +69,8 @@ interface SessionInterface
|
||||
* will leave the system settings unchanged, 0 sets the cookie
|
||||
* to expire with browser session. Time is in seconds, and is
|
||||
* not a Unix timestamp.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function migrate(bool $destroy = false, int $lifetime = null);
|
||||
public function migrate(bool $destroy = false, int $lifetime = null): bool;
|
||||
|
||||
/**
|
||||
* Force the session to be saved and closed.
|
||||
@@ -93,33 +83,23 @@ interface SessionInterface
|
||||
|
||||
/**
|
||||
* Checks if an attribute is defined.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name);
|
||||
public function has(string $name): bool;
|
||||
|
||||
/**
|
||||
* Returns an attribute.
|
||||
*
|
||||
* @param mixed $default The default value if not found
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name, $default = null);
|
||||
public function get(string $name, mixed $default = null): mixed;
|
||||
|
||||
/**
|
||||
* Sets an attribute.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set(string $name, $value);
|
||||
public function set(string $name, mixed $value);
|
||||
|
||||
/**
|
||||
* Returns attributes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all();
|
||||
public function all(): array;
|
||||
|
||||
/**
|
||||
* Sets attributes.
|
||||
@@ -131,7 +111,7 @@ interface SessionInterface
|
||||
*
|
||||
* @return mixed The removed value or null when it does not exist
|
||||
*/
|
||||
public function remove(string $name);
|
||||
public function remove(string $name): mixed;
|
||||
|
||||
/**
|
||||
* Clears all attributes.
|
||||
@@ -140,10 +120,8 @@ interface SessionInterface
|
||||
|
||||
/**
|
||||
* Checks if the session was started.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isStarted();
|
||||
public function isStarted(): bool;
|
||||
|
||||
/**
|
||||
* Registers a SessionBagInterface with the session.
|
||||
@@ -152,15 +130,11 @@ interface SessionInterface
|
||||
|
||||
/**
|
||||
* Gets a bag instance by name.
|
||||
*
|
||||
* @return SessionBagInterface
|
||||
*/
|
||||
public function getBag(string $name);
|
||||
public function getBag(string $name): SessionBagInterface;
|
||||
|
||||
/**
|
||||
* Gets session meta.
|
||||
*
|
||||
* @return MetadataBag
|
||||
*/
|
||||
public function getMetadataBag();
|
||||
public function getMetadataBag(): MetadataBag;
|
||||
}
|
||||
|
||||
@@ -22,17 +22,13 @@ use Symfony\Component\HttpFoundation\Session\SessionUtils;
|
||||
*/
|
||||
abstract class AbstractSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
|
||||
{
|
||||
private $sessionName;
|
||||
private $prefetchId;
|
||||
private $prefetchData;
|
||||
private $newSessionId;
|
||||
private $igbinaryEmptyData;
|
||||
private string $sessionName;
|
||||
private string $prefetchId;
|
||||
private string $prefetchData;
|
||||
private ?string $newSessionId = null;
|
||||
private string $igbinaryEmptyData;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function open($savePath, $sessionName)
|
||||
public function open(string $savePath, string $sessionName): bool
|
||||
{
|
||||
$this->sessionName = $sessionName;
|
||||
if (!headers_sent() && !\ini_get('session.cache_limiter') && '0' !== \ini_get('session.cache_limiter')) {
|
||||
@@ -42,52 +38,26 @@ abstract class AbstractSessionHandler implements \SessionHandlerInterface, \Sess
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function doRead(string $sessionId);
|
||||
abstract protected function doRead(string $sessionId): string;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function doWrite(string $sessionId, string $data);
|
||||
abstract protected function doWrite(string $sessionId, string $data): bool;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function doDestroy(string $sessionId);
|
||||
abstract protected function doDestroy(string $sessionId): bool;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function validateId($sessionId)
|
||||
public function validateId(string $sessionId): bool
|
||||
{
|
||||
$this->prefetchData = $this->read($sessionId);
|
||||
$this->prefetchId = $sessionId;
|
||||
|
||||
if (\PHP_VERSION_ID < 70317 || (70400 <= \PHP_VERSION_ID && \PHP_VERSION_ID < 70405)) {
|
||||
// work around https://bugs.php.net/79413
|
||||
foreach (debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
|
||||
if (!isset($frame['class']) && isset($frame['function']) && \in_array($frame['function'], ['session_regenerate_id', 'session_create_id'], true)) {
|
||||
return '' === $this->prefetchData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '' !== $this->prefetchData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function read($sessionId)
|
||||
public function read(string $sessionId): string
|
||||
{
|
||||
if (null !== $this->prefetchId) {
|
||||
if (isset($this->prefetchId)) {
|
||||
$prefetchId = $this->prefetchId;
|
||||
$prefetchData = $this->prefetchData;
|
||||
$this->prefetchId = $this->prefetchData = null;
|
||||
unset($this->prefetchId, $this->prefetchData);
|
||||
|
||||
if ($prefetchId === $sessionId || '' === $prefetchData) {
|
||||
$this->newSessionId = '' === $prefetchData ? $sessionId : null;
|
||||
@@ -102,16 +72,10 @@ abstract class AbstractSessionHandler implements \SessionHandlerInterface, \Sess
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function write($sessionId, $data)
|
||||
public function write(string $sessionId, string $data): bool
|
||||
{
|
||||
if (null === $this->igbinaryEmptyData) {
|
||||
// see https://github.com/igbinary/igbinary/issues/146
|
||||
$this->igbinaryEmptyData = \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
|
||||
}
|
||||
// see https://github.com/igbinary/igbinary/issues/146
|
||||
$this->igbinaryEmptyData ??= \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
|
||||
if ('' === $data || $this->igbinaryEmptyData === $data) {
|
||||
return $this->destroy($sessionId);
|
||||
}
|
||||
@@ -120,14 +84,10 @@ abstract class AbstractSessionHandler implements \SessionHandlerInterface, \Sess
|
||||
return $this->doWrite($sessionId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function destroy($sessionId)
|
||||
public function destroy(string $sessionId): bool
|
||||
{
|
||||
if (!headers_sent() && filter_var(\ini_get('session.use_cookies'), \FILTER_VALIDATE_BOOLEAN)) {
|
||||
if (!$this->sessionName) {
|
||||
if (!isset($this->sessionName)) {
|
||||
throw new \LogicException(sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', static::class));
|
||||
}
|
||||
$cookie = SessionUtils::popSessionCookie($this->sessionName, $sessionId);
|
||||
@@ -140,13 +100,9 @@ abstract class AbstractSessionHandler implements \SessionHandlerInterface, \Sess
|
||||
* started the session).
|
||||
*/
|
||||
if (null === $cookie || isset($_COOKIE[$this->sessionName])) {
|
||||
if (\PHP_VERSION_ID < 70300) {
|
||||
setcookie($this->sessionName, '', 0, \ini_get('session.cookie_path'), \ini_get('session.cookie_domain'), filter_var(\ini_get('session.cookie_secure'), \FILTER_VALIDATE_BOOLEAN), filter_var(\ini_get('session.cookie_httponly'), \FILTER_VALIDATE_BOOLEAN));
|
||||
} else {
|
||||
$params = session_get_cookie_params();
|
||||
unset($params['lifetime']);
|
||||
setcookie($this->sessionName, '', $params);
|
||||
}
|
||||
$params = session_get_cookie_params();
|
||||
unset($params['lifetime']);
|
||||
setcookie($this->sessionName, '', $params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,56 +27,32 @@ class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpd
|
||||
$this->marshaller = $marshaller;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function open($savePath, $name)
|
||||
public function open(string $savePath, string $name): bool
|
||||
{
|
||||
return $this->handler->open($savePath, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return $this->handler->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function destroy($sessionId)
|
||||
public function destroy(string $sessionId): bool
|
||||
{
|
||||
return $this->handler->destroy($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
return $this->handler->gc($maxlifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function read($sessionId)
|
||||
public function read(string $sessionId): string
|
||||
{
|
||||
return $this->marshaller->unmarshall($this->handler->read($sessionId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function write($sessionId, $data)
|
||||
public function write(string $sessionId, string $data): bool
|
||||
{
|
||||
$failed = [];
|
||||
$marshalledData = $this->marshaller->marshall(['data' => $data], $failed);
|
||||
@@ -88,20 +64,12 @@ class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpd
|
||||
return $this->handler->write($sessionId, $marshalledData['data']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function validateId($sessionId)
|
||||
public function validateId(string $sessionId): bool
|
||||
{
|
||||
return $this->handler->validateId($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
return $this->handler->updateTimestamp($sessionId, $data);
|
||||
}
|
||||
|
||||
@@ -24,14 +24,14 @@ class MemcachedSessionHandler extends AbstractSessionHandler
|
||||
private $memcached;
|
||||
|
||||
/**
|
||||
* @var int Time to live in seconds
|
||||
* Time to live in seconds.
|
||||
*/
|
||||
private $ttl;
|
||||
private ?int $ttl;
|
||||
|
||||
/**
|
||||
* @var string Key prefix for shared environments
|
||||
* Key prefix for shared environments.
|
||||
*/
|
||||
private $prefix;
|
||||
private string $prefix;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -54,11 +54,7 @@ class MemcachedSessionHandler extends AbstractSessionHandler
|
||||
$this->prefix = $options['prefix'] ?? 'sf2s';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return $this->memcached->quit();
|
||||
}
|
||||
@@ -66,16 +62,12 @@ class MemcachedSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doRead(string $sessionId)
|
||||
protected function doRead(string $sessionId): string
|
||||
{
|
||||
return $this->memcached->get($this->prefix.$sessionId) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
$this->memcached->touch($this->prefix.$sessionId, time() + (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
|
||||
|
||||
@@ -85,7 +77,7 @@ class MemcachedSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doWrite(string $sessionId, string $data)
|
||||
protected function doWrite(string $sessionId, string $data): bool
|
||||
{
|
||||
return $this->memcached->set($this->prefix.$sessionId, $data, time() + (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
|
||||
}
|
||||
@@ -93,18 +85,14 @@ class MemcachedSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doDestroy(string $sessionId)
|
||||
protected function doDestroy(string $sessionId): bool
|
||||
{
|
||||
$result = $this->memcached->delete($this->prefix.$sessionId);
|
||||
|
||||
return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
// not required here because memcached will auto expire the records anyhow.
|
||||
return 0;
|
||||
@@ -112,10 +100,8 @@ class MemcachedSessionHandler extends AbstractSessionHandler
|
||||
|
||||
/**
|
||||
* Return a Memcached instance.
|
||||
*
|
||||
* @return \Memcached
|
||||
*/
|
||||
protected function getMemcached()
|
||||
protected function getMemcached(): \Memcached
|
||||
{
|
||||
return $this->memcached;
|
||||
}
|
||||
|
||||
@@ -25,12 +25,12 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
|
||||
/**
|
||||
* @var \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface
|
||||
*/
|
||||
private $currentHandler;
|
||||
private \SessionHandlerInterface $currentHandler;
|
||||
|
||||
/**
|
||||
* @var \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface
|
||||
*/
|
||||
private $writeOnlyHandler;
|
||||
private \SessionHandlerInterface $writeOnlyHandler;
|
||||
|
||||
public function __construct(\SessionHandlerInterface $currentHandler, \SessionHandlerInterface $writeOnlyHandler)
|
||||
{
|
||||
@@ -45,11 +45,7 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
|
||||
$this->writeOnlyHandler = $writeOnlyHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
$result = $this->currentHandler->close();
|
||||
$this->writeOnlyHandler->close();
|
||||
@@ -57,11 +53,7 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function destroy($sessionId)
|
||||
public function destroy(string $sessionId): bool
|
||||
{
|
||||
$result = $this->currentHandler->destroy($sessionId);
|
||||
$this->writeOnlyHandler->destroy($sessionId);
|
||||
@@ -69,11 +61,7 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
$result = $this->currentHandler->gc($maxlifetime);
|
||||
$this->writeOnlyHandler->gc($maxlifetime);
|
||||
@@ -81,11 +69,7 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function open($savePath, $sessionName)
|
||||
public function open(string $savePath, string $sessionName): bool
|
||||
{
|
||||
$result = $this->currentHandler->open($savePath, $sessionName);
|
||||
$this->writeOnlyHandler->open($savePath, $sessionName);
|
||||
@@ -93,21 +77,13 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function read($sessionId)
|
||||
public function read(string $sessionId): string
|
||||
{
|
||||
// No reading from new handler until switch-over
|
||||
return $this->currentHandler->read($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function write($sessionId, $sessionData)
|
||||
public function write(string $sessionId, string $sessionData): bool
|
||||
{
|
||||
$result = $this->currentHandler->write($sessionId, $sessionData);
|
||||
$this->writeOnlyHandler->write($sessionId, $sessionData);
|
||||
@@ -115,21 +91,13 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function validateId($sessionId)
|
||||
public function validateId(string $sessionId): bool
|
||||
{
|
||||
// No reading from new handler until switch-over
|
||||
return $this->currentHandler->validateId($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $sessionData)
|
||||
public function updateTimestamp(string $sessionId, string $sessionData): bool
|
||||
{
|
||||
$result = $this->currentHandler->updateTimestamp($sessionId, $sessionData);
|
||||
$this->writeOnlyHandler->updateTimestamp($sessionId, $sessionData);
|
||||
|
||||
@@ -27,16 +27,8 @@ use MongoDB\Collection;
|
||||
class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
{
|
||||
private $mongo;
|
||||
|
||||
/**
|
||||
* @var Collection
|
||||
*/
|
||||
private $collection;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $options;
|
||||
private array $options;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -84,11 +76,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
], $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -96,7 +84,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doDestroy(string $sessionId)
|
||||
protected function doDestroy(string $sessionId): bool
|
||||
{
|
||||
$this->getCollection()->deleteOne([
|
||||
$this->options['id_field'] => $sessionId,
|
||||
@@ -105,11 +93,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
return $this->getCollection()->deleteMany([
|
||||
$this->options['expiry_field'] => ['$lt' => new UTCDateTime()],
|
||||
@@ -119,7 +103,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doWrite(string $sessionId, string $data)
|
||||
protected function doWrite(string $sessionId, string $data): bool
|
||||
{
|
||||
$expiry = new UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
|
||||
|
||||
@@ -138,11 +122,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
$expiry = new UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
|
||||
|
||||
@@ -160,7 +140,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doRead(string $sessionId)
|
||||
protected function doRead(string $sessionId): string
|
||||
{
|
||||
$dbData = $this->getCollection()->findOne([
|
||||
$this->options['id_field'] => $sessionId,
|
||||
@@ -176,17 +156,10 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
|
||||
private function getCollection(): Collection
|
||||
{
|
||||
if (null === $this->collection) {
|
||||
$this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
|
||||
}
|
||||
|
||||
return $this->collection;
|
||||
return $this->collection ??= $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Client
|
||||
*/
|
||||
protected function getMongo()
|
||||
protected function getMongo(): Client
|
||||
{
|
||||
return $this->mongo;
|
||||
}
|
||||
|
||||
@@ -18,20 +18,12 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
||||
*/
|
||||
class NullSessionHandler extends AbstractSessionHandler
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function validateId($sessionId)
|
||||
public function validateId(string $sessionId): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -39,16 +31,12 @@ class NullSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doRead(string $sessionId)
|
||||
protected function doRead(string $sessionId): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -56,7 +44,7 @@ class NullSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doWrite(string $sessionId, string $data)
|
||||
protected function doWrite(string $sessionId, string $data): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -64,16 +52,12 @@ class NullSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doDestroy(string $sessionId)
|
||||
protected function doDestroy(string $sessionId): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -65,105 +65,61 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
*/
|
||||
public const LOCK_TRANSACTIONAL = 2;
|
||||
|
||||
private const MAX_LIFETIME = 315576000;
|
||||
|
||||
/**
|
||||
* @var \PDO|null PDO instance or null when not connected yet
|
||||
*/
|
||||
private $pdo;
|
||||
|
||||
/**
|
||||
* DSN string or null for session.save_path or false when lazy connection disabled.
|
||||
*
|
||||
* @var string|false|null
|
||||
*/
|
||||
private $dsn = false;
|
||||
private string|false|null $dsn = false;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $driver;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $table = 'sessions';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $idCol = 'sess_id';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $dataCol = 'sess_data';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $lifetimeCol = 'sess_lifetime';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $timeCol = 'sess_time';
|
||||
private string $driver;
|
||||
private string $table = 'sessions';
|
||||
private string $idCol = 'sess_id';
|
||||
private string $dataCol = 'sess_data';
|
||||
private string $lifetimeCol = 'sess_lifetime';
|
||||
private string $timeCol = 'sess_time';
|
||||
|
||||
/**
|
||||
* Username when lazy-connect.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $username = '';
|
||||
private string $username = '';
|
||||
|
||||
/**
|
||||
* Password when lazy-connect.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $password = '';
|
||||
private string $password = '';
|
||||
|
||||
/**
|
||||
* Connection options when lazy-connect.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $connectionOptions = [];
|
||||
private array $connectionOptions = [];
|
||||
|
||||
/**
|
||||
* The strategy for locking, see constants.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $lockMode = self::LOCK_TRANSACTIONAL;
|
||||
private int $lockMode = self::LOCK_TRANSACTIONAL;
|
||||
|
||||
/**
|
||||
* It's an array to support multiple reads before closing which is manual, non-standard usage.
|
||||
*
|
||||
* @var \PDOStatement[] An array of statements to release advisory locks
|
||||
*/
|
||||
private $unlockStatements = [];
|
||||
private array $unlockStatements = [];
|
||||
|
||||
/**
|
||||
* True when the current session exists but expired according to session.gc_maxlifetime.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $sessionExpired = false;
|
||||
private bool $sessionExpired = false;
|
||||
|
||||
/**
|
||||
* Whether a transaction is active.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $inTransaction = false;
|
||||
private bool $inTransaction = false;
|
||||
|
||||
/**
|
||||
* Whether gc() has been called.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $gcCalled = false;
|
||||
private bool $gcCalled = false;
|
||||
|
||||
/**
|
||||
* You can either pass an existing database connection as PDO instance or
|
||||
@@ -186,7 +142,7 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
*
|
||||
* @throws \InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
|
||||
*/
|
||||
public function __construct($pdoOrDsn = null, array $options = [])
|
||||
public function __construct(\PDO|string $pdoOrDsn = null, array $options = [])
|
||||
{
|
||||
if ($pdoOrDsn instanceof \PDO) {
|
||||
if (\PDO::ERRMODE_EXCEPTION !== $pdoOrDsn->getAttribute(\PDO::ATTR_ERRMODE)) {
|
||||
@@ -267,34 +223,24 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
* Returns true when the current session exists but expired according to session.gc_maxlifetime.
|
||||
*
|
||||
* Can be used to distinguish between a new session and one that expired due to inactivity.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSessionExpired()
|
||||
public function isSessionExpired(): bool
|
||||
{
|
||||
return $this->sessionExpired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function open($savePath, $sessionName)
|
||||
public function open(string $savePath, string $sessionName): bool
|
||||
{
|
||||
$this->sessionExpired = false;
|
||||
|
||||
if (null === $this->pdo) {
|
||||
if (!isset($this->pdo)) {
|
||||
$this->connect($this->dsn ?: $savePath);
|
||||
}
|
||||
|
||||
return parent::open($savePath, $sessionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function read($sessionId)
|
||||
public function read(string $sessionId): string
|
||||
{
|
||||
try {
|
||||
return parent::read($sessionId);
|
||||
@@ -305,11 +251,7 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
// We delay gc() to close() so that it is executed outside the transactional and blocking read-write process.
|
||||
// This way, pruning expired sessions does not block them from being started while the current session is used.
|
||||
@@ -321,7 +263,7 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doDestroy(string $sessionId)
|
||||
protected function doDestroy(string $sessionId): bool
|
||||
{
|
||||
// delete the record associated with this id
|
||||
$sql = "DELETE FROM $this->table WHERE $this->idCol = :id";
|
||||
@@ -342,7 +284,7 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doWrite(string $sessionId, string $data)
|
||||
protected function doWrite(string $sessionId, string $data): bool
|
||||
{
|
||||
$maxlifetime = (int) \ini_get('session.gc_maxlifetime');
|
||||
|
||||
@@ -385,11 +327,7 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
$expiry = time() + (int) \ini_get('session.gc_maxlifetime');
|
||||
|
||||
@@ -410,11 +348,7 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
$this->commit();
|
||||
|
||||
@@ -426,27 +360,14 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
$this->gcCalled = false;
|
||||
|
||||
// delete the session records that have expired
|
||||
$sql = "DELETE FROM $this->table WHERE $this->lifetimeCol < :time AND $this->lifetimeCol > :min";
|
||||
$sql = "DELETE FROM $this->table WHERE $this->lifetimeCol < :time";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
|
||||
$stmt->bindValue(':min', self::MAX_LIFETIME, \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
// to be removed in 6.0
|
||||
if ('mysql' === $this->driver) {
|
||||
$legacySql = "DELETE FROM $this->table WHERE $this->lifetimeCol <= :min AND $this->lifetimeCol + $this->timeCol < :time";
|
||||
} else {
|
||||
$legacySql = "DELETE FROM $this->table WHERE $this->lifetimeCol <= :min AND $this->lifetimeCol < :time - $this->timeCol";
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare($legacySql);
|
||||
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
|
||||
$stmt->bindValue(':min', self::MAX_LIFETIME, \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
if (false !== $this->dsn) {
|
||||
$this->pdo = null; // only close lazy-connection
|
||||
$this->driver = null;
|
||||
unset($this->pdo, $this->driver); // only close lazy-connection
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -649,10 +570,8 @@ 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.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function doRead(string $sessionId)
|
||||
protected function doRead(string $sessionId): string
|
||||
{
|
||||
if (self::LOCK_ADVISORY === $this->lockMode) {
|
||||
$this->unlockStatements[] = $this->doAdvisoryLock($sessionId);
|
||||
@@ -669,9 +588,6 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
|
||||
if ($sessionRows) {
|
||||
$expiry = (int) $sessionRows[0][1];
|
||||
if ($expiry <= self::MAX_LIFETIME) {
|
||||
$expiry += $sessionRows[0][2];
|
||||
}
|
||||
|
||||
if ($expiry < time()) {
|
||||
$this->sessionExpired = true;
|
||||
@@ -804,14 +720,13 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
if (self::LOCK_TRANSACTIONAL === $this->lockMode) {
|
||||
$this->beginTransaction();
|
||||
|
||||
// selecting the time column should be removed in 6.0
|
||||
switch ($this->driver) {
|
||||
case 'mysql':
|
||||
case 'oci':
|
||||
case 'pgsql':
|
||||
return "SELECT $this->dataCol, $this->lifetimeCol, $this->timeCol FROM $this->table WHERE $this->idCol = :id FOR UPDATE";
|
||||
return "SELECT $this->dataCol, $this->lifetimeCol FROM $this->table WHERE $this->idCol = :id FOR UPDATE";
|
||||
case 'sqlsrv':
|
||||
return "SELECT $this->dataCol, $this->lifetimeCol, $this->timeCol FROM $this->table WITH (UPDLOCK, ROWLOCK) WHERE $this->idCol = :id";
|
||||
return "SELECT $this->dataCol, $this->lifetimeCol FROM $this->table WITH (UPDLOCK, ROWLOCK) WHERE $this->idCol = :id";
|
||||
case 'sqlite':
|
||||
// we already locked when starting transaction
|
||||
break;
|
||||
@@ -820,7 +735,7 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
}
|
||||
}
|
||||
|
||||
return "SELECT $this->dataCol, $this->lifetimeCol, $this->timeCol FROM $this->table WHERE $this->idCol = :id";
|
||||
return "SELECT $this->dataCol, $this->lifetimeCol FROM $this->table WHERE $this->idCol = :id";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -929,12 +844,10 @@ class PdoSessionHandler extends AbstractSessionHandler
|
||||
|
||||
/**
|
||||
* Return a PDO instance.
|
||||
*
|
||||
* @return \PDO
|
||||
*/
|
||||
protected function getConnection()
|
||||
protected function getConnection(): \PDO
|
||||
{
|
||||
if (null === $this->pdo) {
|
||||
if (!isset($this->pdo)) {
|
||||
$this->connect($this->dsn ?: \ini_get('session.save_path'));
|
||||
}
|
||||
|
||||
|
||||
@@ -26,37 +26,24 @@ class RedisSessionHandler extends AbstractSessionHandler
|
||||
private $redis;
|
||||
|
||||
/**
|
||||
* @var string Key prefix for shared environments
|
||||
* Key prefix for shared environments.
|
||||
*/
|
||||
private $prefix;
|
||||
private string $prefix;
|
||||
|
||||
/**
|
||||
* @var int Time to live in seconds
|
||||
* Time to live in seconds.
|
||||
*/
|
||||
private $ttl;
|
||||
private ?int $ttl;
|
||||
|
||||
/**
|
||||
* List of available options:
|
||||
* * prefix: The prefix to use for the keys in order to avoid collision on the Redis server
|
||||
* * ttl: The time to live in seconds.
|
||||
*
|
||||
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis
|
||||
*
|
||||
* @throws \InvalidArgumentException When unsupported client or options are passed
|
||||
*/
|
||||
public function __construct($redis, array $options = [])
|
||||
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis, array $options = [])
|
||||
{
|
||||
if (
|
||||
!$redis instanceof \Redis &&
|
||||
!$redis instanceof \RedisArray &&
|
||||
!$redis instanceof \RedisCluster &&
|
||||
!$redis instanceof \Predis\ClientInterface &&
|
||||
!$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__, get_debug_type($redis)));
|
||||
}
|
||||
|
||||
if ($diff = array_diff(array_keys($options), ['prefix', 'ttl'])) {
|
||||
throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
|
||||
}
|
||||
@@ -115,23 +102,13 @@ class RedisSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
return (bool) $this->redis->expire($this->prefix.$sessionId, (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
|
||||
return $this->redis->expire($this->prefix.$sessionId, (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +21,8 @@ use Symfony\Component\Cache\Traits\RedisProxy;
|
||||
*/
|
||||
class SessionHandlerFactory
|
||||
{
|
||||
/**
|
||||
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\PDO|string $connection Connection or DSN
|
||||
*/
|
||||
public static function createHandler($connection): AbstractSessionHandler
|
||||
public static function createHandler(object|string $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__, get_debug_type($connection)));
|
||||
}
|
||||
|
||||
if ($options = \is_string($connection) ? parse_url($connection) : false) {
|
||||
parse_str($options['query'] ?? '', $options);
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
||||
*/
|
||||
class StrictSessionHandler extends AbstractSessionHandler
|
||||
{
|
||||
private $handler;
|
||||
private $doDestroy;
|
||||
private \SessionHandlerInterface $handler;
|
||||
private bool $doDestroy;
|
||||
|
||||
public function __construct(\SessionHandlerInterface $handler)
|
||||
{
|
||||
@@ -31,10 +31,16 @@ class StrictSessionHandler extends AbstractSessionHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function open($savePath, $sessionName)
|
||||
public function isWrapper(): bool
|
||||
{
|
||||
return $this->handler instanceof \SessionHandler;
|
||||
}
|
||||
|
||||
public function open(string $savePath, string $sessionName): bool
|
||||
{
|
||||
parent::open($savePath, $sessionName);
|
||||
|
||||
@@ -44,16 +50,12 @@ class StrictSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doRead(string $sessionId)
|
||||
protected function doRead(string $sessionId): string
|
||||
{
|
||||
return $this->handler->read($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
return $this->write($sessionId, $data);
|
||||
}
|
||||
@@ -61,16 +63,12 @@ class StrictSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doWrite(string $sessionId, string $data)
|
||||
protected function doWrite(string $sessionId, string $data): bool
|
||||
{
|
||||
return $this->handler->write($sessionId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function destroy($sessionId)
|
||||
public function destroy(string $sessionId): bool
|
||||
{
|
||||
$this->doDestroy = true;
|
||||
$destroyed = parent::destroy($sessionId);
|
||||
@@ -81,27 +79,19 @@ class StrictSessionHandler extends AbstractSessionHandler
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doDestroy(string $sessionId)
|
||||
protected function doDestroy(string $sessionId): bool
|
||||
{
|
||||
$this->doDestroy = false;
|
||||
|
||||
return $this->handler->destroy($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return $this->handler->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
return $this->handler->gc($maxlifetime);
|
||||
}
|
||||
|
||||
@@ -26,15 +26,8 @@ class MetadataBag implements SessionBagInterface
|
||||
public const UPDATED = 'u';
|
||||
public const LIFETIME = 'l';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name = '__metadata';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $storageKey;
|
||||
private string $name = '__metadata';
|
||||
private string $storageKey;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
@@ -43,15 +36,10 @@ class MetadataBag implements SessionBagInterface
|
||||
|
||||
/**
|
||||
* Unix timestamp.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $lastUsed;
|
||||
private int $lastUsed;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $updateThreshold;
|
||||
private int $updateThreshold;
|
||||
|
||||
/**
|
||||
* @param string $storageKey The key used to store bag in the session
|
||||
@@ -84,10 +72,8 @@ class MetadataBag implements SessionBagInterface
|
||||
|
||||
/**
|
||||
* Gets the lifetime that the session cookie was set with.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLifetime()
|
||||
public function getLifetime(): int
|
||||
{
|
||||
return $this->meta[self::LIFETIME];
|
||||
}
|
||||
@@ -108,7 +94,7 @@ class MetadataBag implements SessionBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorageKey()
|
||||
public function getStorageKey(): string
|
||||
{
|
||||
return $this->storageKey;
|
||||
}
|
||||
@@ -118,7 +104,7 @@ class MetadataBag implements SessionBagInterface
|
||||
*
|
||||
* @return int Unix timestamp
|
||||
*/
|
||||
public function getCreated()
|
||||
public function getCreated(): int
|
||||
{
|
||||
return $this->meta[self::CREATED];
|
||||
}
|
||||
@@ -128,7 +114,7 @@ class MetadataBag implements SessionBagInterface
|
||||
*
|
||||
* @return int Unix timestamp
|
||||
*/
|
||||
public function getLastUsed()
|
||||
public function getLastUsed(): int
|
||||
{
|
||||
return $this->lastUsed;
|
||||
}
|
||||
@@ -136,7 +122,7 @@ class MetadataBag implements SessionBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
public function clear(): mixed
|
||||
{
|
||||
// nothing to do
|
||||
return null;
|
||||
@@ -145,7 +131,7 @@ class MetadataBag implements SessionBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
public function start(): bool
|
||||
{
|
||||
if ($this->started) {
|
||||
return true;
|
||||
@@ -94,7 +94,7 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null)
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null): bool
|
||||
{
|
||||
if (!$this->started) {
|
||||
$this->start();
|
||||
@@ -109,7 +109,7 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
@@ -129,7 +129,7 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
@@ -183,7 +183,7 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBag(string $name)
|
||||
public function getBag(string $name): SessionBagInterface
|
||||
{
|
||||
if (!isset($this->bags[$name])) {
|
||||
throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
|
||||
@@ -199,7 +199,7 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isStarted()
|
||||
public function isStarted(): bool
|
||||
{
|
||||
return $this->started;
|
||||
}
|
||||
@@ -215,10 +215,8 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
|
||||
/**
|
||||
* Gets the MetadataBag.
|
||||
*
|
||||
* @return MetadataBag
|
||||
*/
|
||||
public function getMetadataBag()
|
||||
public function getMetadataBag(): MetadataBag
|
||||
{
|
||||
return $this->metadataBag;
|
||||
}
|
||||
@@ -228,10 +226,8 @@ class MockArraySessionStorage implements SessionStorageInterface
|
||||
*
|
||||
* This doesn't need to be particularly cryptographically secure since this is just
|
||||
* a mock.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateId()
|
||||
protected function generateId(): string
|
||||
{
|
||||
return hash('sha256', uniqid('ss_mock_', true));
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
*/
|
||||
class MockFileSessionStorage extends MockArraySessionStorage
|
||||
{
|
||||
private $savePath;
|
||||
private string $savePath;
|
||||
|
||||
/**
|
||||
* @param string|null $savePath Path of directory to save session files
|
||||
@@ -48,7 +48,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
public function start(): bool
|
||||
{
|
||||
if ($this->started) {
|
||||
return true;
|
||||
@@ -68,7 +68,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null)
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null): bool
|
||||
{
|
||||
if (!$this->started) {
|
||||
$this->start();
|
||||
|
||||
@@ -21,8 +21,8 @@ class_exists(MockFileSessionStorage::class);
|
||||
*/
|
||||
class MockFileSessionStorageFactory implements SessionStorageFactoryInterface
|
||||
{
|
||||
private $savePath;
|
||||
private $name;
|
||||
private ?string $savePath;
|
||||
private string $name;
|
||||
private $metaBag;
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionUtils;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
|
||||
@@ -54,11 +53,6 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
*/
|
||||
protected $metadataBag;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $emulateSameSite;
|
||||
|
||||
/**
|
||||
* Depending on how you want the storage driver to behave you probably
|
||||
* want to override this constructor entirely.
|
||||
@@ -94,10 +88,8 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
* sid_bits_per_character, "5"
|
||||
* trans_sid_hosts, $_SERVER['HTTP_HOST']
|
||||
* trans_sid_tags, "a=href,area=href,frame=src,form="
|
||||
*
|
||||
* @param AbstractProxy|\SessionHandlerInterface|null $handler
|
||||
*/
|
||||
public function __construct(array $options = [], $handler = null, MetadataBag $metaBag = null)
|
||||
public function __construct(array $options = [], AbstractProxy|\SessionHandlerInterface $handler = null, MetadataBag $metaBag = null)
|
||||
{
|
||||
if (!\extension_loaded('session')) {
|
||||
throw new \LogicException('PHP extension "session" is required.');
|
||||
@@ -120,10 +112,8 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
|
||||
/**
|
||||
* Gets the save handler instance.
|
||||
*
|
||||
* @return AbstractProxy|\SessionHandlerInterface
|
||||
*/
|
||||
public function getSaveHandler()
|
||||
public function getSaveHandler(): AbstractProxy|\SessionHandlerInterface
|
||||
{
|
||||
return $this->saveHandler;
|
||||
}
|
||||
@@ -131,7 +121,7 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
public function start(): bool
|
||||
{
|
||||
if ($this->started) {
|
||||
return true;
|
||||
@@ -186,13 +176,6 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
throw new \RuntimeException('Failed to start the session.');
|
||||
}
|
||||
|
||||
if (null !== $this->emulateSameSite) {
|
||||
$originalCookie = SessionUtils::popSessionCookie(session_name(), session_id());
|
||||
if (null !== $originalCookie) {
|
||||
header(sprintf('%s; SameSite=%s', $originalCookie, $this->emulateSameSite), false);
|
||||
}
|
||||
}
|
||||
|
||||
$this->loadSession();
|
||||
|
||||
return true;
|
||||
@@ -201,7 +184,7 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->saveHandler->getId();
|
||||
}
|
||||
@@ -217,7 +200,7 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->saveHandler->getName();
|
||||
}
|
||||
@@ -233,7 +216,7 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null)
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null): bool
|
||||
{
|
||||
// Cannot regenerate the session ID for non-active sessions.
|
||||
if (\PHP_SESSION_ACTIVE !== session_status()) {
|
||||
@@ -254,16 +237,7 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
$this->metadataBag->stampNew();
|
||||
}
|
||||
|
||||
$isRegenerated = session_regenerate_id($destroy);
|
||||
|
||||
if (null !== $this->emulateSameSite) {
|
||||
$originalCookie = SessionUtils::popSessionCookie(session_name(), session_id());
|
||||
if (null !== $originalCookie) {
|
||||
header(sprintf('%s; SameSite=%s', $originalCookie, $this->emulateSameSite), false);
|
||||
}
|
||||
}
|
||||
|
||||
return $isRegenerated;
|
||||
return session_regenerate_id($destroy);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -340,7 +314,7 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBag(string $name)
|
||||
public function getBag(string $name): SessionBagInterface
|
||||
{
|
||||
if (!isset($this->bags[$name])) {
|
||||
throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
|
||||
@@ -366,10 +340,8 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
|
||||
/**
|
||||
* Gets the MetadataBag.
|
||||
*
|
||||
* @return MetadataBag
|
||||
*/
|
||||
public function getMetadataBag()
|
||||
public function getMetadataBag(): MetadataBag
|
||||
{
|
||||
return $this->metadataBag;
|
||||
}
|
||||
@@ -377,7 +349,7 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isStarted()
|
||||
public function isStarted(): bool
|
||||
{
|
||||
return $this->started;
|
||||
}
|
||||
@@ -404,31 +376,16 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
'gc_divisor', 'gc_maxlifetime', 'gc_probability',
|
||||
'lazy_write', 'name', 'referer_check',
|
||||
'serialize_handler', 'use_strict_mode', 'use_cookies',
|
||||
'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
|
||||
'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
|
||||
'upload_progress.freq', 'upload_progress.min_freq', 'url_rewriter.tags',
|
||||
'use_only_cookies', 'use_trans_sid',
|
||||
'sid_length', 'sid_bits_per_character', 'trans_sid_hosts', 'trans_sid_tags',
|
||||
]);
|
||||
|
||||
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.
|
||||
$this->emulateSameSite = $value;
|
||||
continue;
|
||||
}
|
||||
if ('cookie_secure' === $key && 'auto' === $value) {
|
||||
continue;
|
||||
}
|
||||
ini_set('url_rewriter.tags' !== $key ? 'session.'.$key : $key, $value);
|
||||
ini_set('session.'.$key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -449,11 +406,9 @@ class NativeSessionStorage implements SessionStorageInterface
|
||||
* @see https://php.net/sessionhandlerinterface
|
||||
* @see https://php.net/sessionhandler
|
||||
*
|
||||
* @param AbstractProxy|\SessionHandlerInterface|null $saveHandler
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function setSaveHandler($saveHandler = null)
|
||||
public function setSaveHandler(AbstractProxy|\SessionHandlerInterface $saveHandler = null)
|
||||
{
|
||||
if (!$saveHandler instanceof AbstractProxy &&
|
||||
!$saveHandler instanceof \SessionHandlerInterface &&
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
|
||||
|
||||
// Help opcache.preload discover always-needed symbols
|
||||
class_exists(NativeSessionStorage::class);
|
||||
@@ -21,15 +22,15 @@ class_exists(NativeSessionStorage::class);
|
||||
*/
|
||||
class NativeSessionStorageFactory implements SessionStorageFactoryInterface
|
||||
{
|
||||
private $options;
|
||||
private array $options;
|
||||
private $handler;
|
||||
private $metaBag;
|
||||
private $secure;
|
||||
private bool $secure;
|
||||
|
||||
/**
|
||||
* @see NativeSessionStorage constructor.
|
||||
*/
|
||||
public function __construct(array $options = [], $handler = null, MetadataBag $metaBag = null, bool $secure = false)
|
||||
public function __construct(array $options = [], AbstractProxy|\SessionHandlerInterface $handler = null, MetadataBag $metaBag = null, bool $secure = false)
|
||||
{
|
||||
$this->options = $options;
|
||||
$this->handler = $handler;
|
||||
|
||||
@@ -20,10 +20,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
|
||||
*/
|
||||
class PhpBridgeSessionStorage extends NativeSessionStorage
|
||||
{
|
||||
/**
|
||||
* @param AbstractProxy|\SessionHandlerInterface|null $handler
|
||||
*/
|
||||
public function __construct($handler = null, MetadataBag $metaBag = null)
|
||||
public function __construct(AbstractProxy|\SessionHandlerInterface $handler = null, MetadataBag $metaBag = null)
|
||||
{
|
||||
if (!\extension_loaded('session')) {
|
||||
throw new \LogicException('PHP extension "session" is required.');
|
||||
@@ -36,7 +33,7 @@ class PhpBridgeSessionStorage extends NativeSessionStorage
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
public function start(): bool
|
||||
{
|
||||
if ($this->started) {
|
||||
return true;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
|
||||
|
||||
// Help opcache.preload discover always-needed symbols
|
||||
class_exists(PhpBridgeSessionStorage::class);
|
||||
@@ -23,12 +24,9 @@ class PhpBridgeSessionStorageFactory implements SessionStorageFactoryInterface
|
||||
{
|
||||
private $handler;
|
||||
private $metaBag;
|
||||
private $secure;
|
||||
private bool $secure;
|
||||
|
||||
/**
|
||||
* @see PhpBridgeSessionStorage constructor.
|
||||
*/
|
||||
public function __construct($handler = null, MetadataBag $metaBag = null, bool $secure = false)
|
||||
public function __construct(AbstractProxy|\SessionHandlerInterface $handler = null, MetadataBag $metaBag = null, bool $secure = false)
|
||||
{
|
||||
$this->handler = $handler;
|
||||
$this->metaBag = $metaBag;
|
||||
|
||||
@@ -30,50 +30,40 @@ abstract class AbstractProxy
|
||||
|
||||
/**
|
||||
* Gets the session.save_handler name.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getSaveHandlerName()
|
||||
public function getSaveHandlerName(): ?string
|
||||
{
|
||||
return $this->saveHandlerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this proxy handler and instance of \SessionHandlerInterface.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSessionHandlerInterface()
|
||||
public function isSessionHandlerInterface(): bool
|
||||
{
|
||||
return $this instanceof \SessionHandlerInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isWrapper()
|
||||
public function isWrapper(): bool
|
||||
{
|
||||
return $this->wrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has a session started?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isActive()
|
||||
public function isActive(): bool
|
||||
{
|
||||
return \PHP_SESSION_ACTIVE === session_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the session ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
public function getId(): string
|
||||
{
|
||||
return session_id();
|
||||
}
|
||||
@@ -94,10 +84,8 @@ abstract class AbstractProxy
|
||||
|
||||
/**
|
||||
* Gets the session name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return session_name();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
|
||||
|
||||
/**
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
@@ -22,87 +24,52 @@ class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterf
|
||||
{
|
||||
$this->handler = $handler;
|
||||
$this->wrapper = $handler instanceof \SessionHandler;
|
||||
$this->saveHandlerName = $this->wrapper ? \ini_get('session.save_handler') : 'user';
|
||||
$this->saveHandlerName = $this->wrapper || ($handler instanceof StrictSessionHandler && $handler->isWrapper()) ? \ini_get('session.save_handler') : 'user';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \SessionHandlerInterface
|
||||
*/
|
||||
public function getHandler()
|
||||
public function getHandler(): \SessionHandlerInterface
|
||||
{
|
||||
return $this->handler;
|
||||
}
|
||||
|
||||
// \SessionHandlerInterface
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function open($savePath, $sessionName)
|
||||
public function open(string $savePath, string $sessionName): bool
|
||||
{
|
||||
return $this->handler->open($savePath, $sessionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return $this->handler->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function read($sessionId)
|
||||
public function read(string $sessionId): string|false
|
||||
{
|
||||
return $this->handler->read($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function write($sessionId, $data)
|
||||
public function write(string $sessionId, string $data): bool
|
||||
{
|
||||
return $this->handler->write($sessionId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function destroy($sessionId)
|
||||
public function destroy(string $sessionId): bool
|
||||
{
|
||||
return $this->handler->destroy($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
return $this->handler->gc($maxlifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function validateId($sessionId)
|
||||
public function validateId(string $sessionId): bool
|
||||
{
|
||||
return !$this->handler instanceof \SessionUpdateTimestampHandlerInterface || $this->handler->validateId($sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
return $this->handler instanceof \SessionUpdateTimestampHandlerInterface ? $this->handler->updateTimestamp($sessionId, $data) : $this->write($sessionId, $data);
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -24,25 +24,19 @@ interface SessionStorageInterface
|
||||
/**
|
||||
* Starts the session.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \RuntimeException if something goes wrong starting the session
|
||||
*/
|
||||
public function start();
|
||||
public function start(): bool;
|
||||
|
||||
/**
|
||||
* Checks if the session is started.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isStarted();
|
||||
public function isStarted(): bool;
|
||||
|
||||
/**
|
||||
* Returns the session ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId();
|
||||
public function getId(): string;
|
||||
|
||||
/**
|
||||
* Sets the session ID.
|
||||
@@ -51,10 +45,8 @@ interface SessionStorageInterface
|
||||
|
||||
/**
|
||||
* Returns the session name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Sets the session name.
|
||||
@@ -86,11 +78,9 @@ interface SessionStorageInterface
|
||||
* to expire with browser session. Time is in seconds, and is
|
||||
* not a Unix timestamp.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while regenerating this storage
|
||||
*/
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null);
|
||||
public function regenerate(bool $destroy = false, int $lifetime = null): bool;
|
||||
|
||||
/**
|
||||
* Force the session to be saved and closed.
|
||||
@@ -113,19 +103,14 @@ interface SessionStorageInterface
|
||||
/**
|
||||
* Gets a SessionBagInterface by name.
|
||||
*
|
||||
* @return SessionBagInterface
|
||||
*
|
||||
* @throws \InvalidArgumentException If the bag does not exist
|
||||
*/
|
||||
public function getBag(string $name);
|
||||
public function getBag(string $name): SessionBagInterface;
|
||||
|
||||
/**
|
||||
* Registers a SessionBagInterface for use.
|
||||
*/
|
||||
public function registerBag(SessionBagInterface $bag);
|
||||
|
||||
/**
|
||||
* @return MetadataBag
|
||||
*/
|
||||
public function getMetadataBag();
|
||||
public function getMetadataBag(): MetadataBag;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user