-
Notifications
You must be signed in to change notification settings - Fork 2
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
0 parents
commit 5206ef2
Showing
12 changed files
with
816 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml | ||
.DS_Store |
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,12 @@ | ||
language: php | ||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
before_script: | ||
- composer install | ||
script: | ||
- ./vendor/bin/phpunit --coverage-text | ||
- ./vendor/bin/phpcs --extensions=php --report=summary --standard=PSR2 ./src ./tests |
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,17 @@ | ||
# Minphp/Session | ||
|
||
[![Build Status](https://travis-ci.org/phillipsdata/minphp-session.svg?branch=master)](https://travis-ci.org/phillipsdata/minphp-session) [![Coverage Status](https://coveralls.io/repos/phillipsdata/minphp-session/badge.svg)](https://coveralls.io/r/phillipsdata/minphp-session) | ||
|
||
Session Management Library. | ||
|
||
## Installation | ||
|
||
Install via composer: | ||
|
||
```sh | ||
composer require minphp/session:dev-master | ||
``` | ||
|
||
## Basic Usage | ||
|
||
TODO |
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,28 @@ | ||
{ | ||
"name": "minphp/session", | ||
"description": "Session Management Library", | ||
"homepage": "http://github.com/phillipsdata/minphp-session", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Cody Phillips", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4.0" | ||
}, | ||
"require-dev": { | ||
"php": ">=5.4.0", | ||
"phpunit/phpunit": "~4.0", | ||
"squizlabs/php_codesniffer": "~2.2", | ||
"satooshi/php-coveralls": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-4": {"Minphp\\Session\\": "src"} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": {"Minphp\\Session\\Tests\\": "tests"} | ||
} | ||
} |
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,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
bootstrap="./vendor/autoload.php" | ||
backupGlobals="false" | ||
beStrictAboutTestsThatDoNotTestAnything="true" | ||
checkForUnintentionallyCoveredCode="true" | ||
forceCoversAnnotation="true"> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory suffix="Test.php">tests/Unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,12 @@ | ||
<?php | ||
namespace Minphp\Session\Handlers; | ||
|
||
use SessionHandler; | ||
|
||
/** | ||
* Native PHP Session Handler | ||
*/ | ||
class NativeHandler extends SessionHandler | ||
{ | ||
|
||
} |
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,112 @@ | ||
<?php | ||
namespace Minphp\Session\Handlers; | ||
|
||
use SessionHandlerInterface; | ||
use PDO; | ||
|
||
/** | ||
* PDO Database Session Handler | ||
*/ | ||
class PdoHandler implements SessionHandlerInterface | ||
{ | ||
protected $db; | ||
protected $options; | ||
|
||
public function __construct(PDO $db, array $options = []) | ||
{ | ||
$this->options = array_merge( | ||
[ | ||
'tbl' => 'sessions', | ||
'tbl_id' => 'id', | ||
'tbl_exp' => 'expire', | ||
'tbl_val' => 'value' | ||
], | ||
$options | ||
); | ||
$this->db = $db; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function close() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function destroy($sessionId) | ||
{ | ||
$query = "DELETE FROM {$this->options['tbl']} WHERE {$this->options['tbl_id']} = :id"; | ||
$this->db->prepare($query) | ||
->execute([':id' => $sessionId]); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function gc($maxlifetime) | ||
{ | ||
$query = "DELETE FROM {$this->options['tbl']} WHERE {$this->options['tbl_exp']} < :expire"; | ||
$this->db->prepare($query) | ||
->execute([':expire' => date('Y-m-d H:i:s', time() - $maxlifetime)]); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function open($savePath, $name) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($sessionId) | ||
{ | ||
$query = "SELECT {$this->options['tbl_val']} FROM {$this->options['tbl']} " | ||
. "WHERE {$this->options['tbl_id']} = :id AND {$this->options['tbl_exp']} >= :expire"; | ||
$row = $this->db->prepare($query, [PDO::FETCH_OBJ]) | ||
->execute([':id' => $sessionId, ':expire' => date('Y-m-d H:i:s')]); | ||
|
||
if ($row) { | ||
return $row->{$this->options['tbl_val']}; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($sessionId, $data) | ||
{ | ||
$ttl = ini_get('session.gc_maxlifetime'); | ||
$session = [ | ||
':value' => $data, | ||
':id' => $sessionId, | ||
':expire' => date('Y-m-d H:i:s', time() + $ttl) | ||
]; | ||
|
||
$updateQuery = "UPDATE {$this->options['tbl']} SET {$this->options['tbl_val']} = :value, " | ||
. "{$this->options['tbl_exp']} = :expire " | ||
. "WHERE {$this->options['tbl_id']} = :id"; | ||
$updateStmt = $this->db->prepare($updateQuery); | ||
$updateStmt->execute($session); | ||
|
||
if (!$updateStmt->rowCount()) { | ||
// Session does not exist, so create it | ||
$insertQuery = "INSERT INTO {$this->options['tbl']} " | ||
. "({$this->options['tbl_id']}, {$this->options['tbl_val']}, {$this->options['tbl_exp']}) " | ||
. "VALUES (:id, :value, :expire)"; | ||
$this->db->prepare($insertQuery)->execute($session); | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.