Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjhv committed Jun 26, 2015
1 parent 85a6fb9 commit 9ca6da9
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ $app->get('/', function () use ($app) {
});
```

## TODO

Tests!

## License

MIT
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "bryanjhv/slim-session",
"description": "Session middleware and helper for Slim framework 2.",
"version": "1.0.0",
"type": "library",
"keywords": [
"slim",
"session",
"middleware",
"helper"
],
"homepage": "https://github.com/bryanjhv/slim-session",
"license": "MIT",
"authors": [
{
"name": "Bryan Horna",
"email": "[email protected]",
"homepage": "http://bryanjhv.me",
"role": "developer"
}
],
"support": {
"source": "https://github.com/bryanjhv/slim-session.git"
},
"require": {
"slim/slim": "~2.0"
},
"autoload": {
"psr-4": {
"": "src/"
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
113 changes: 113 additions & 0 deletions src/Slim/Middleware/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Slim\Middleware;

/**
* Session middleware
*
* This class is meant to provide a easy way to manage sessions with framework,
* using the PHP built-in (native) sessions but also allowing to manipulate the
* session variables via same app instance, by registering a container to the
* helper class that ships with this package. As a plus, you can set a lifetime
* for a session and it will be updated after each user activity or interaction
* like an 'autorefresh' feature.
*
* Keep in mind this relies on PHP native sessions, so for this to work you
* must have that enabled and correctly working.
*
* @package Slim\Middleware
* @author Bryan Horna
*/
class Session extends \Slim\Middleware
{
/**
* @var array
*/
protected $settings;

/**
* Constructor
*
* @param array $settings
*/
public function __construct($settings = array())
{
$defaults = array(
'lifetime' => '20 minutes',
'path' => '/',
'domain' => null,
'secure' => false,
'httponly' => false,
'name' => 'slim_session',
'autorefresh' => false
);
$settings = array_merge($defaults, $settings);
if (is_string($lifetime = $settings['lifetime'])) {
$settings['lifetime'] = strtotime($lifetime) - time();
}
$this->settings = $settings;

ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 1);
ini_set('session.gc_maxlifetime', 30 * 24 * 60 * 60);
}

/**
* Call
*/
public function call()
{
$this->registerHelper();
$this->startSession();
$this->next->call();
}

/**
* Register helper
*
* It registers a session helper singleton to $app->session, so you can use
* that to manage sessions or instantiate the helper class for yourself.
*/
protected function registerHelper()
{
$this->app->container->singleton('session', function () {
return new \SlimSession\Helper;
});
}

/**
* Start session
*/
protected function startSession()
{
if (session_id()) {
return;
}

$settings = $this->settings;
$name = $settings['name'];

session_set_cookie_params(
$settings['lifetime'],
$settings['path'],
$settings['domain'],
$settings['secure'],
$settings['httponly']
);
session_name($name);
session_cache_limiter(false);
session_start();

if ($settings['autorefresh'] && isset($_COOKIE[$name])) {
setcookie(
$name,
$_COOKIE[$name],
time() + $settings['lifetime'],
$settings['path'],
$settings['domain'],
$settings['secure'],
$settings['httponly']
);
}
}
}
158 changes: 158 additions & 0 deletions src/SlimSession/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace SlimSession;

/**
* Helper class
*
* This is a general-purpose class that allows to manage PHP built-in sessions
* and the session variables passed via $_SESSION superglobal.
*
* @package SlimSession
*/
class Helper
{
/**
* Get a session variable.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null)
{
return $this->exists($key)
? $_SESSION[$key]
: $default;
}

/**
* Set a session variable.
*
* @param string $key
* @param mixed $value
*/
public function set($key, $value)
{
$_SESSION[$key] = $value;
}

/**
* Delete a session variable.
*
* @param string $key
*/
public function delete($key)
{
if ($this->exists($key)) {
unset($_SESSION[$key]);
}
}

/**
* Clear all session variables.
*/
public function clear()
{
$_SESSION = array();
}

/**
* Check if a session variable is set.
*
* @param string $key
*
* @return bool
*/
protected function exists($key)
{
return array_key_exists($key, $_SESSION);
}

/**
* Get or regenerate current session ID.
*
* @param bool $new
*
* @return string
*/
public static function id($new = false)
{
if ($new && session_id()) {
session_regenerate_id(true);
}

return session_id() ?: '';
}

/**
* Destroy the session.
*/
public static function destroy()
{
if (self::id()) {
session_unset();
session_destroy();
session_write_close();

if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(
session_name(),
'',
time() - 4200,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly']
);
}
}
}

/**
* Magic method for get.
*
* @param string $key
*
* @return mixed
*/
public function __get($key)
{
return $this->get($key);
}

/**
* Magic method for set.
*
* @param string $key
* @param mixed $value
*/
public function __set($key, $value)
{
$this->set($key, $value);
}

/**
* Magic method for delete.
*
* @param string $key
*/
public function __unset($key)
{
$this->delete($key);
}

/**
* Magic method for exists.
*
* @param string $key
*
* @return bool
*/
public function __isset($key)
{
return $this->exists($key);
}
}

0 comments on commit 9ca6da9

Please sign in to comment.