Skip to content

Commit

Permalink
Release 3.0.0 for Slim 3
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjhv committed May 13, 2016
1 parent 9224481 commit 1afe1da
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# slim-session

Simple middleware for Slim Framework 2, that allows managing PHP built-in
Simple middleware for Slim Framework 3, that allows managing PHP built-in
sessions and includes a `Helper` class to help you with the `$_SESSION`
superglobal.

**For the middleware version for Slim Framework 2, please check out the `slim-2`
branch in this repository.**

## Installation

Include this line in your `composer.json`:

```
"bryanjhv/slim-session": "~2.1"
"bryanjhv/slim-session": "~3.0"
```

## Usage

The namespace for the middleware is the same as normal, so:

```php
$app = new \Slim\Slim;
$app->add(new \Slim\Middleware\Session(
$app = new \Slim\App;
$app->add(new \Slim\Middleware\Session([
'name' => 'dummy_session',
'autorefresh' => true,
'lifetime' => '1 hour'
));
]));
```

### Supported options
Expand All @@ -37,20 +38,22 @@ $app->add(new \Slim\Middleware\Session(

## Session helper

This package also ships a `Helper` class ~~and registers it to `$app->session`
so you can do~~ which you need to register or instance if you want to use it:
This package also ships with a `Helper` class which you need to register in the
container or instance as an object if you want to use it:

```php
$app->container->singleton('session', function () {
$container = $app->getContainer();

$container['session'] = function ($c) {
return new \SlimSession\Helper;
});
};
```

This will provide you `$app->session`, so you can simply do:

```php
$app->get('/', function () use ($app) {
$session = new \SlimSession\Helper; // or $app->session if registered
$app->get('/', function () {
$session = new \SlimSession\Helper; // or $this->session if registered

// Get a variable
$key = $session->get('key', 'default');
Expand All @@ -67,13 +70,13 @@ $app->get('/', function () use ($app) {
$session::destroy();

// Get current session id
$id = $app->session::id();
$id = $this->session::id();
});
```

## TODO

Tests (still)!
Tests (still, still)!

## License

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bryanjhv/slim-session",
"description": "Session middleware and helper for Slim framework 2.",
"description": "Session middleware and helper for Slim framework 3.",
"type": "library",
"keywords": [
"slim",
Expand All @@ -22,7 +22,7 @@
"source": "https://github.com/bryanjhv/slim-session.git"
},
"require": {
"slim/slim": "~2.0"
"slim/slim": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
39 changes: 25 additions & 14 deletions src/Slim/Middleware/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Slim\Middleware;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

/**
* Session middleware
*
Expand All @@ -18,7 +21,7 @@
* @package Slim\Middleware
* @author Bryan Horna
*/
class Session extends \Slim\Middleware
class Session
{
/**
* @var array
Expand All @@ -30,18 +33,19 @@ class Session extends \Slim\Middleware
*
* @param array $settings
*/
public function __construct($settings = array())
public function __construct($settings = [])
{
$defaults = array(
'lifetime' => '20 minutes',
'path' => '/',
'domain' => null,
'secure' => false,
'httponly' => false,
'name' => 'slim_session',
'autorefresh' => false
);
$defaults = [
'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();
}
Expand All @@ -53,12 +57,19 @@ public function __construct($settings = array())
}

/**
* Call
* Called when middleware needs to be executed.
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function call()
public function __invoke(Request $request, Response $response, callable $next)
{
$this->startSession();
$this->next->call();

return $next($request, $response);
}

/**
Expand Down

0 comments on commit 1afe1da

Please sign in to comment.