Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed Feb 5, 2021
1 parent 0ca9b0c commit 6e6b4cd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
67 changes: 54 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,43 @@
[![type-coverage](https://shepherd.dev/github/antidot-framework/react-framework/coverage.svg)](https://shepherd.dev/github/antidot-framework/react-framework)
[![Build Status](https://scrutinizer-ci.com/g/antidot-framework/session-middleware/badges/build.png?b=1.x.x)](https://scrutinizer-ci.com/g/antidot-framework/session-middleware/build-status/1.x.x)

PSR-15 middleware that allows having [Aura Session](https://github.com/auraphp/Aura.Session) inside our request attributes.
PSR-15 middleware that allows having session inside the request attributes.

## Install

Intall using composer
Install using composer, by default it uses an implementation of [Aura Session](https://github.com/auraphp/Aura.Session).

```bash
composer require antidot-fw/session
```

Add it to your pipeline
## Config

Using [Antidot Framework Starter](), it will work after adding the middleware to the pipeline. Also, you can use it in any
PSR-15 compatible middleware pipeline using PSR-11 container.

```php
<?php

declare(strict_types=1);
$sessionFactory = new AuraSessionFactory();
$sessionMiddleware = new SessionMiddleware($sessionFactory($container));
```

Add it to your pipeline

```php
<?php

use Antidot\Application\Http\Application;
use Antidot\Application\Http\Middleware\ErrorMiddleware;
use Antidot\Application\Http\Middleware\RouteDispatcherMiddleware;
use Antidot\Application\Http\Middleware\RouteNotFoundMiddleware;
use Antidot\Logger\Application\Http\Middleware\ExceptionLoggerMiddleware;
use Antidot\Logger\Application\Http\Middleware\RequestLoggerMiddleware;
use Antidot\Session\Application\Http\Middleware\SessionMiddleware;
...

return static function (Application $app) : void {
$app->pipe(ErrorMiddleware::class);
$app->pipe(ExceptionLoggerMiddleware::class);
$app->pipe(...);
$app->pipe(SessionMiddleware::class); // added here
$app->pipe(RequestLoggerMiddleware::class);
$app->pipe(RouteDispatcherMiddleware::class);
$app->pipe(RouteNotFoundMiddleware::class);
...
};

```
Expand Down Expand Up @@ -65,7 +70,7 @@ class SomeHandler implements RequestHandlerInterface

public function handle(ServerRequestInterface $request): ResponseInterface
{
/** @var \Aura\Session\Segment $session */
/** @var \Antidot\Session\Application\Http\SessionSegment $session */
$session = $request->getAttribute('session');

$session->set('foo', 'bar');
Expand All @@ -77,5 +82,41 @@ class SomeHandler implements RequestHandlerInterface
...
}
}
```

## Adding custom session implementation

To create a wrapper for your own custom session, you need to implement both `Antidot\Session\Application\Http\SessionSegment`
and `Antidot\Session\Application\Http\SessionSegmentFactory` clases, take a look to the
`Antidot\Session\Infrastructure\AuraSessionSegment` and `Antidot\Session\Infrastructure\AuraSessionSegmentFactory`.

```php
<?php

declare(strict_types=1);

namespace Antidot\Session\Application\Http;

interface SessionSegment
{
public function get(string $identity);
public function getFlash(string $identity);
public function set(string $identity, $value): void;
public function setFlash(string $identity, $value): void;
}
```

```php
<?php

declare(strict_types=1);

namespace Antidot\Session\Application\Http;

use Psr\Http\Message\ServerRequestInterface;

interface SessionSegmentFactory
{
public function __invoke(ServerRequestInterface $request): SessionSegment;
}
```
5 changes: 4 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"src"
]
},
"logs": {
"text": "infection.log"
},
"mutators": {
"@default": true
}
}
}

0 comments on commit 6e6b4cd

Please sign in to comment.