Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Session code with PHP 8.1 features #636

Merged
merged 3 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
added return type `mixed` to the method `getContext` in `Spiral\Filters\FilterInterface` interface.
Added return type `mixed` to the method `getValue` in `Spiral\Filters\InputInterface`.
- [spiral/http] Config `Spiral\Config\JsonPayloadConfig` moved to the `Spiral\Bootloader\Http\JsonPayloadConfig`.
- [spiral/session] Added return type `void` to the method `resume` in `Spiral\Session\SessionInterface`.
- [spiral/session] Added return type `self` and `mixed` parameter type of `$value` to the method `set`
in `Spiral\Session\SessionSectionInterface`.
- [spiral/session] Added return type `bool` to the method `has` in `Spiral\Session\SessionSectionInterface`.
- [spiral/session] Added return type `mixed` and `mixed` parameter type of `$default` to the method `get`
in `Spiral\Session\SessionSectionInterface`.
- [spiral/session] Added return type `mixed` and `mixed` parameter type of `$default` to the method `pull`
in `Spiral\Session\SessionSectionInterface`.
- [spiral/session] Added return type `void` to the method `delete` in `Spiral\Session\SessionSectionInterface`.
- [spiral/session] Added return type `void` to the method `clear` in `Spiral\Session\SessionSectionInterface`.
- [spiral/pagination] Added return type `self` to the method `limit`, added return type `self` to the method `offset`
in `Spiral\Pagination\PaginableInterface`
- [spiral/prototype] Parameter `$printer` now is not nullable in `Spiral\Prototype\Injector` constructor.
Expand Down
9 changes: 2 additions & 7 deletions src/Framework/Session/SectionScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Spiral\Session;

use Spiral\Core\Exception\ScopeException;

final class SectionScope implements SessionSectionInterface
{
public function __construct(
Expand Down Expand Up @@ -72,9 +70,9 @@ public function getAll(): array
return $this->getActiveSection()->getAll();
}

public function set(string $name, mixed $value): void
public function set(string $name, mixed $value): SessionSectionInterface
{
$this->getActiveSection()->set($name, $value);
return $this->getActiveSection()->set($name, $value);
}

public function has(string $name): bool
Expand Down Expand Up @@ -102,9 +100,6 @@ public function clear(): void
$this->getActiveSection()->clear();
}

/**
* @throws ScopeException
*/
private function getActiveSection(): SessionSectionInterface
{
return $this->session->getActiveSession()->getSection($this->name);
Expand Down
9 changes: 1 addition & 8 deletions src/Session/src/Config/SessionConfig.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Session\Config;
Expand Down Expand Up @@ -61,7 +54,7 @@ public function getHandler(): ?Autowire
return $this->config['handler'];
}

if (class_exists($this->config['handler'])) {
if (\class_exists($this->config['handler'])) {
return new Autowire($this->config['handler']);
}

Expand Down
7 changes: 0 additions & 7 deletions src/Session/src/Exception/MultipleSessionException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Session\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Session/src/Exception/SessionException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Session\Exception;
Expand Down
74 changes: 15 additions & 59 deletions src/Session/src/Handler/FileHandler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Session\Handler;
Expand All @@ -18,96 +11,59 @@
*/
final class FileHandler implements \SessionHandlerInterface
{
/**
* @var FilesInterface
*/
protected $files;

/**
* @var string
*/
protected $directory = '';

/**
* @param int $lifetime Default session lifetime.
*/
public function __construct(FilesInterface $files, string $directory)
{
$this->files = $files;
$this->directory = $directory;
public function __construct(
private readonly FilesInterface $files,
private readonly string $directory
) {
}

/**
* @inheritdoc
*/
public function close(): bool
{
return true;
}

/**
* @inheritdoc
* @codeCoverageIgnore
*/
public function destroy($session_id): bool
public function destroy(string $id): bool
{
return $this->files->delete($this->getFilename($session_id));
return $this->files->delete($this->getFilename($id));
}

/**
* @inheritdoc
* @codeCoverageIgnore
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
public function gc(int $maxlifetime): int
{
foreach ($this->files->getFiles($this->directory) as $filename) {
if ($this->files->time($filename) < time() - $maxlifetime) {
if ($this->files->time($filename) < \time() - $maxlifetime) {
$this->files->delete($filename);
}
}

return $maxlifetime;
}

/**
* @inheritdoc
*/
public function open($save_path, $session_id): bool
public function open(string $path, string $id): bool
{
return true;
}

/**
* @inheritdoc
*/
public function read($session_id): string
public function read(string $id): string
{
return $this->files->exists($this->getFilename($session_id))
? $this->files->read($this->getFilename($session_id))
: '';
return $this->files->exists($this->getFilename($id)) ? $this->files->read($this->getFilename($id)) : '';
}

/**
* @inheritdoc
*/
public function write($session_id, $session_data): bool
public function write(string $id, string $data): bool
{
return $this->files->write(
$this->getFilename($session_id),
$session_data,
FilesInterface::RUNTIME,
true
);
return $this->files->write($this->getFilename($id), $data, FilesInterface::RUNTIME, true);
}

/**
* Session data filename.
*
* @param string $session_id
*/
protected function getFilename($session_id): string
protected function getFilename($id): string
{
return "{$this->directory}/{$session_id}";
return \sprintf('%s/%s', $this->directory, $id);
}
}
36 changes: 5 additions & 31 deletions src/Session/src/Handler/NullHandler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Session\Handler;
Expand All @@ -16,51 +9,32 @@
*/
final class NullHandler implements \SessionHandlerInterface
{
/**
* @inheritdoc
*/
public function close(): bool
{
return true;
}

/**
* @inheritdoc
*/
public function destroy($session_id): bool
public function destroy(string $id): bool
{
return true;
}

/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
public function gc(int $maxlifetime): int
{
return $maxlifetime;
}

/**
* @inheritdoc
*/
public function open($save_path, $session_id): bool
public function open(string $path, string $id): bool
{
return true;
}

/**
* @inheritdoc
*/
public function read($session_id): string
public function read(string $id): string
{
return '';
}

/**
* @inheritdoc
*/
public function write($session_id, $session_data): bool
public function write(string $id, string $data): bool
{
return true;
}
Expand Down
Loading