Skip to content

Commit 4653803

Browse files
committed
Init
0 parents  commit 4653803

File tree

7 files changed

+335
-0
lines changed

7 files changed

+335
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
php:
3+
- 7.0
4+
- 5.6
5+
- 5.5
6+
- hhvm
7+
8+
matrix:
9+
fast_finish: true
10+
11+
sudo: false
12+
13+
before_install:
14+
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then phpenv config-rm xdebug.ini; fi;
15+
- pip install --user codecov
16+
17+
before_script:
18+
- travis_retry composer self-update
19+
- travis_retry composer install --prefer-source --no-interaction
20+
21+
script:
22+
- php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-clover=coverage.xml
23+
24+
after_success:
25+
- codecov

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Aaron Scherer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

composer.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "cache/session-handler",
3+
"type": "library",
4+
"description": "An implementation of PHP's SessionHandlerInterface that allows PSR-6",
5+
"keywords": [
6+
"cache",
7+
"psr-6",
8+
"session handler"
9+
],
10+
"homepage": "https://github.com/php-cache/session-handler",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Aaron Scherer",
15+
"email": "[email protected]",
16+
"homepage": "https://github.com/aequasi"
17+
},
18+
{
19+
"name": "Tobias Nyholm",
20+
"email": "[email protected]",
21+
"homepage": "https://github.com/nyholm"
22+
}
23+
],
24+
"require": {
25+
"php": "^5.5|^7.0",
26+
"psr/cache": "~1.0"
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "^5.1|^4.0",
30+
"mockery/mockery": "^0.9.4",
31+
"cache/array-adapter": "@stable"
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Cache\\SessionHandler\\": "src/"
36+
}
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Cache\\SessionHandler\\Tests\\": "tests/"
41+
}
42+
}
43+
}

phpunit.xml.dist

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<testsuites>
15+
<testsuite name="Main Test Suite">
16+
<directory>./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<logging>
21+
<log type="coverage-text" target="php://stdout"/>
22+
</logging>
23+
24+
<groups>
25+
<exclude>
26+
<group>benchmark</group>
27+
</exclude>
28+
</groups>
29+
30+
<filter>
31+
<whitelist>
32+
<directory>src/</directory>
33+
</whitelist>
34+
</filter>
35+
</phpunit>

src/Psr6SessionHandler.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\SessionHandler;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
16+
/**
17+
* Class SessionHandler.
18+
*
19+
* @author Aaron Scherer <[email protected]>
20+
*/
21+
class Psr6SessionHandler implements \SessionHandlerInterface
22+
{
23+
/**
24+
* @type CacheItemPoolInterface Cache driver.
25+
*/
26+
private $cache;
27+
28+
/**
29+
* @type int Time to live in seconds
30+
*/
31+
private $ttl;
32+
33+
/**
34+
* @type string Key prefix for shared environments.
35+
*/
36+
private $prefix;
37+
38+
/**
39+
* Constructor.
40+
*
41+
* List of available options:
42+
* * prefix: The prefix to use for the cache keys in order to avoid collision
43+
* * expiretime: The time to live in seconds
44+
*
45+
* @param CacheItemPoolInterface $cache A Cache instance
46+
* @param array $options An associative array of cache options
47+
*
48+
* @throws \InvalidArgumentException When unsupported options are passed
49+
*/
50+
public function __construct(CacheItemPoolInterface $cache, array $options = [])
51+
{
52+
$this->cache = $cache;
53+
54+
$this->ttl = isset($options['ttl']) ? (int) $options['ttl'] : 86400;
55+
$this->prefix = isset($options['prefix']) ? $options['prefix'] : 'psr6ses_';
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function open($savePath, $sessionName)
62+
{
63+
return true;
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function close()
70+
{
71+
return true;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function read($sessionId)
78+
{
79+
$item = $this->getCacheItem($sessionId);
80+
if ($item->isHit()) {
81+
return $item->get();
82+
}
83+
84+
return '';
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function write($sessionId, $data)
91+
{
92+
$item = $this->getCacheItem($sessionId);
93+
$item->set($data)
94+
->expiresAfter($this->ttl);
95+
96+
return $this->cache->save($item);
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function destroy($sessionId)
103+
{
104+
return $this->cache->deleteItem($this->prefix.$sessionId);
105+
}
106+
107+
/**
108+
* {@inheritdoc}
109+
*/
110+
public function gc($lifetime)
111+
{
112+
// not required here because cache will auto expire the records anyhow.
113+
return true;
114+
}
115+
116+
/**
117+
* @param $sessionId
118+
*
119+
* @return \Psr\Cache\CacheItemInterface
120+
*/
121+
private function getCacheItem($sessionId)
122+
{
123+
return $this->cache->getItem($this->prefix.$sessionId);
124+
}
125+
}

tests/Psr6SessionHandlerTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\doctrine-bridge package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\SessionHandler\Tests;
13+
14+
use Cache\SessionHandler\Psr6SessionHandler;
15+
use Mockery as m;
16+
use Psr\Cache\CacheItemInterface;
17+
use Psr\Cache\CacheItemPoolInterface;
18+
19+
/**
20+
* @author Aaron Scherer <[email protected]>
21+
*/
22+
class Psr6SessionHandlerTest extends \PHPUnit_Framework_TestCase
23+
{
24+
/**
25+
* @type Psr6SessionHandler
26+
*/
27+
private $handler;
28+
29+
/**
30+
* @type m\MockInterface|CacheItemPoolInterface
31+
*/
32+
private $mock;
33+
34+
/**
35+
* @type m\MockInterface|CacheItemInterface
36+
*/
37+
private $itemMock;
38+
39+
protected function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->mock = m::mock(CacheItemPoolInterface::class);
44+
$this->handler = new Psr6SessionHandler($this->mock);
45+
46+
$this->itemMock = m::mock(CacheItemInterface::class);
47+
}
48+
49+
public function testConstructor()
50+
{
51+
$this->assertInstanceOf(Psr6SessionHandler::class, $this->handler);
52+
}
53+
54+
public function testOpen()
55+
{
56+
$this->assertTrue($this->handler->open('foo', 'bar'));
57+
}
58+
59+
public function testClose()
60+
{
61+
$this->assertTrue($this->handler->close());
62+
}
63+
64+
public function testGc()
65+
{
66+
$this->assertTrue($this->handler->gc(4711));
67+
}
68+
69+
public function testRead()
70+
{
71+
72+
}
73+
74+
public function testWrite()
75+
{
76+
77+
}
78+
79+
public function testDestroy()
80+
{
81+
82+
}
83+
}

0 commit comments

Comments
 (0)