Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  update the doc
  replace travis by github ci
  add psalm
  add exception messages where necessary
  CS
  update deps
  update to phpunit 8
  remove unused dependency
  remove useless script
  rename Session::all() to Session::values()
  rename __toString to toString
  rename has() to contains()
  require php 7.4
  • Loading branch information
Baptouuuu committed Mar 1, 2020
2 parents ead2fa7 + e410bba commit 60ef562
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 115 deletions.
3 changes: 0 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/fixtures export-ignore
/tests export-ignore
65 changes: 65 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: CI

on: [push]

jobs:
phpunit:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
php-version: ['7.4']
name: 'PHPUnit - PHP/${{ matrix.php-version }} - OS/${{ matrix.os }}'
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Setup PHP
uses: shivammathur/setup-php@v1
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: xdebug
ini-values: xdebug.max_nesting_level=2048
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: PHPUnit
run: vendor/bin/phpunit --coverage-clover=coverage.clover
- uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
psalm:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4']
name: 'Psalm - PHP/${{ matrix.php-version }}'
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Setup PHP
uses: shivammathur/setup-php@v1
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: Psalm
run: vendor/bin/psalm --shepherd
6 changes: 0 additions & 6 deletions .scrutinizer.yml

This file was deleted.

12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Http session

| `master` | `develop` |
|----------|-----------|
| [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Innmind/HttpSession/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Innmind/HttpSession/?branch=master) | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Innmind/HttpSession/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/Innmind/HttpSession/?branch=develop) |
| [![Code Coverage](https://scrutinizer-ci.com/g/Innmind/HttpSession/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Innmind/HttpSession/?branch=master) | [![Code Coverage](https://scrutinizer-ci.com/g/Innmind/HttpSession/badges/coverage.png?b=develop)](https://scrutinizer-ci.com/g/Innmind/HttpSession/?branch=develop) |
| [![Build Status](https://scrutinizer-ci.com/g/Innmind/HttpSession/badges/build.png?b=master)](https://scrutinizer-ci.com/g/Innmind/HttpSession/build-status/master) | [![Build Status](https://scrutinizer-ci.com/g/Innmind/HttpSession/badges/build.png?b=develop)](https://scrutinizer-ci.com/g/Innmind/HttpSession/build-status/develop) |
[![Build Status](https://github.com/Innmind/HttpSession/workflows/CI/badge.svg)](https://github.com/Innmind/HttpSession/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/Innmind/HttpSession/branch/develop/graph/badge.svg)](https://codecov.io/gh/Innmind/HttpSession)
[![Type Coverage](https://shepherd.dev/github/Innmind/HttpSession/coverage.svg)](https://shepherd.dev/github/Innmind/HttpSession)

Library to manage session for http requests.

Expand All @@ -22,10 +20,9 @@ composer require innmind/http-session
use Innmind\HttpSession\Manager\Native;
use Innmind\Http\{
Message\Response\Response,
Message\StatusCode\StatusCode,
Headers\Headers,
Message\StatusCode,
Headers,
Header\SetCookie,
Header\CookieValue,
Header\CookieParameter\HttpOnly,
Header\CookieParameter\Domain,
Header\Parameter\Parameter,
Expand All @@ -43,14 +40,12 @@ $response = new Response(
$code->associatedReasonPhrase(),
$request->protocolVersion(),
Headers::of(
new SetCookie(
new CookieValue(
new Parameter((string) $session->name(), (string) $session->id()),
new HttpOnly,
new Domain($request->url()->authority()->host())
)
)
)
SetCookie::of(
new Parameter($session->name()->toString(), $session->id()->toString()),
new HttpOnly,
new Domain($request->url()->authority()->host()),
),
),
);
// send the response
```
Expand Down
13 changes: 5 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"issues": "http://github.com/Innmind/HttpSession/issues"
},
"require": {
"php": "~7.2",
"innmind/immutable": "^2.8",
"innmind/http": "^3.7"
"php": "~7.4",
"innmind/immutable": "~3.5",
"innmind/http": "~4.2"
},
"autoload": {
"psr-4": {
Expand All @@ -31,10 +31,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "~7.0",
"giorgiosironi/eris": "^0.10.0"
},
"scripts": {
"test": "vendor/bin/phpunit --colors=always"
"phpunit/phpunit": "~8.0",
"vimeo/psalm": "^3.9"
}
}
16 changes: 16 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
2 changes: 1 addition & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Manager
{
public function start(ServerRequest $request): Session;
public function get(ServerRequest $request): Session;
public function has(ServerRequest $request): bool;
public function contains(ServerRequest $request): bool;
public function save(ServerRequest $request): void;
public function close(ServerRequest $request): void;
}
66 changes: 38 additions & 28 deletions src/Manager/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
Message\ServerRequest,
Header\Cookie,
};
use Innmind\Url\PathInterface;
use Innmind\Url\Path;
use Innmind\Immutable\Map;
use function Innmind\Immutable\first;

final class Native implements Manager
{
private $session;
private $request;
private ?Session $session = null;
private ?ServerRequest $request = null;

public function __construct(PathInterface $save = null)
public function __construct(Path $save = null)
{
if ($save instanceof PathInterface) {
\session_save_path((string) $save);
if ($save instanceof Path) {
\session_save_path($save->toString());
}
}

Expand All @@ -45,46 +46,58 @@ public function start(ServerRequest $request): Session
throw new FailedToStartSession;
}

/** @var Map<string, mixed> */
$values = Map::of('string', 'mixed');

/**
* @var string $key
* @var mixed $value
*/
foreach ($_SESSION as $key => $value) {
$values = ($values)($key, $value);
}

$session = new Session(
new Id(\session_id()),
new Name(\session_name()),
Map::of(
'string',
'mixed',
array_keys($_SESSION),
array_values($_SESSION)
)
$values,
);
$this->request = $request;
$this->session = $session;

return $session;
}

/**
* @psalm-suppress InvalidNullableReturnType Because request and session are always set together
*/
public function get(ServerRequest $request): Session
{
if (!$this->has($request)) {
throw new LogicException;
if (!$this->contains($request)) {
throw new LogicException('No session started');
}

/** @psalm-suppress NullableReturnStatement Because request and session are always set together */
return $this->session;
}

public function has(ServerRequest $request): bool
public function contains(ServerRequest $request): bool
{
return $this->request === $request;
}

public function save(ServerRequest $request): void
{
if (!$this->has($request)) {
throw new LogicException;
if (!$this->contains($request)) {
throw new LogicException('No session started');
}

/** @psalm-suppress PossiblyNullReference */
$this
->session
->all()
->values()
->foreach(static function(string $key, $value): void {
/** @psalm-suppress MixedAssignment */
$_SESSION[$key] = $value;
});

Expand All @@ -99,8 +112,8 @@ public function save(ServerRequest $request): void

public function close(ServerRequest $request): void
{
if (!$this->has($request)) {
throw new LogicException;
if (!$this->contains($request)) {
throw new LogicException('No session started');
}

if (\session_destroy() === false) {
Expand All @@ -113,7 +126,7 @@ public function close(ServerRequest $request): void

private function configureSessionId(ServerRequest $request): void
{
if (!$request->headers()->has('Cookie')) {
if (!$request->headers()->contains('Cookie')) {
return;
}

Expand All @@ -124,20 +137,17 @@ private function configureSessionId(ServerRequest $request): void
}

$sessionName = \session_name();
$parameters = $request
->headers()
->get('Cookie')
->values()
->current()
$parameters = first($cookie->values())
->parameters()
->filter(static function(string $name) use ($sessionName): bool {
return $name === $sessionName;
});
})
->values();

if ($parameters->size() !== 1) {
return;
}

\session_id($parameters->current()->value());
\session_id($parameters->first()->value());
}
}
28 changes: 14 additions & 14 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
Id,
Name,
};
use Innmind\Immutable\MapInterface;
use Innmind\Immutable\Map;
use function Innmind\Immutable\assertMap;

final class Session
{
private $id;
private $name;
private $values;
private Id $id;
private Name $name;
/** @var Map<string, mixed> */
private Map $values;

public function __construct(Id $id, Name $name, MapInterface $values)
/**
* @param Map<string, mixed> $values
*/
public function __construct(Id $id, Name $name, Map $values)
{
if (
(string) $values->keyType() !== 'string' ||
(string) $values->valueType() !== 'mixed'
) {
throw new \TypeError('Argument 3 must be of type MapInterface<string, mixed>');
}
assertMap('string', 'mixed', $values, 3);

$this->id = $id;
$this->name = $name;
Expand All @@ -47,7 +47,7 @@ public function get(string $key)
return $this->values->get($key);
}

public function has(string $key): bool
public function contains(string $key): bool
{
return $this->values->contains($key);
}
Expand All @@ -65,9 +65,9 @@ public function set(string $key, $value): void
*
* It should not be used by users
*
* @return MapInterface<string, mixed>
* @return Map<string, mixed>
*/
public function all(): MapInterface
public function values(): Map
{
return $this->values;
}
Expand Down

0 comments on commit 60ef562

Please sign in to comment.