Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
clphillips committed Dec 2, 2015
0 parents commit 5206ef2
Show file tree
Hide file tree
Showing 12 changed files with 816 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
composer.lock
phpunit.xml
.DS_Store
12 changes: 12 additions & 0 deletions .travis.yml
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
17 changes: 17 additions & 0 deletions README.md
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
28 changes: 28 additions & 0 deletions composer.json
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"}
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
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>
12 changes: 12 additions & 0 deletions src/Handlers/NativeHandler.php
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
{

}
112 changes: 112 additions & 0 deletions src/Handlers/PdoHandler.php
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;
}
}
Loading

0 comments on commit 5206ef2

Please sign in to comment.