Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Session\Handler; | ||
|
||
use Psr\SimpleCache\CacheInterface; | ||
use Psr\SimpleCache\InvalidArgumentException; | ||
use Spiral\Cache\CacheStorageProviderInterface; | ||
|
||
final class CacheHandler implements \SessionHandlerInterface | ||
{ | ||
private readonly CacheInterface $cache; | ||
|
||
public function __construct( | ||
CacheStorageProviderInterface $storageProvider, | ||
private readonly ?string $storage = null, | ||
private readonly int $ttl = 86400, | ||
private readonly string $prefix = 'session:' | ||
) { | ||
$this->cache = $storageProvider->storage($this->storage); | ||
} | ||
|
||
public function close(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function destroy(string $id): bool | ||
{ | ||
$this->cache->delete($this->getKey($id)); | ||
|
||
return true; | ||
} | ||
|
||
public function gc(int $max_lifetime): int|false | ||
{ | ||
return 0; | ||
} | ||
|
||
public function open(string $path, string $name): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function read(string $id): string|false | ||
{ | ||
$result = $this->cache->get($this->getKey($id)); | ||
|
||
return (string) $result; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function write(string $id, string $data): bool | ||
{ | ||
return $this->cache->set($this->getKey($id), $data, $this->ttl); | ||
} | ||
|
||
private function getKey(string $id): string | ||
{ | ||
return $this->prefix . $id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Session; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\SimpleCache\CacheInterface; | ||
use Spiral\Cache\CacheStorageProviderInterface; | ||
use Spiral\Session\Handler\CacheHandler; | ||
use Mockery as m; | ||
|
||
final class CacheHandlerTest extends TestCase | ||
{ | ||
private CacheHandler $handler; | ||
private m\MockInterface|CacheInterface $cache; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$storage = m::mock(CacheStorageProviderInterface::class); | ||
|
||
$storage->shouldReceive('storage')->once()->andReturn($this->cache = m::mock(CacheInterface::class)); | ||
|
||
$this->handler = new CacheHandler( | ||
$storage | ||
); | ||
} | ||
|
||
public function testClose(): void | ||
{ | ||
$this->assertTrue($this->handler->close()); | ||
} | ||
|
||
public function testDestroy(): void | ||
{ | ||
$this->cache->shouldReceive('delete')->with('session:foo')->andReturn(true); | ||
|
||
$this->assertTrue($this->handler->destroy('foo')); | ||
} | ||
|
||
public function testGc(): void | ||
{ | ||
$this->assertSame(0, $this->handler->gc(100)); | ||
} | ||
|
||
public function testOpen(): void | ||
{ | ||
$this->assertTrue($this->handler->open('root', 'test')); | ||
} | ||
|
||
public function testRead(): void | ||
{ | ||
$this->cache->shouldReceive('get')->with('session:foo')->andReturn('bar'); | ||
|
||
$this->assertSame('bar', $this->handler->read('foo')); | ||
} | ||
|
||
public function testReadExpired(): void | ||
{ | ||
$this->cache->shouldReceive('get')->with('session:foo')->andReturn(null); | ||
|
||
$this->assertSame('', $this->handler->read('foo')); | ||
} | ||
|
||
public function testWrite(): void | ||
{ | ||
$this->cache->shouldReceive('set')->with('session:foo', 'bar', 86400)->andReturn(true); | ||
|
||
$this->assertTrue($this->handler->write('foo', 'bar')); | ||
} | ||
} |