fix:更新已知bug,优化代码
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user