Skip to content

Commit 167f439

Browse files
committed
The first commit
0 parents  commit 167f439

24 files changed

+1575
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.php diff=php
2+
3+
/Tests export-ignore
4+
/phpunit.* export-ignore
5+
/*.yml export-ignore

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build
2+
vendor
3+
.DS_Store
4+
.idea
5+
composer.lock
6+
phpunit.phar
7+
*.yml
8+
!.travis.yml

.travis.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
language: php
2+
3+
sudo: false
4+
5+
php:
6+
- 7.1
7+
- 7.2
8+
9+
services:
10+
- memcached
11+
- redis-server
12+
13+
env:
14+
- REDIS_SERVER_HOST=127.0.0.1 REDIS_SERVER_PORT=6379 MEMCACHED_POOL=[["127.0.0.1",11211]]
15+
16+
matrix:
17+
fast_finish: true
18+
19+
before_script:
20+
- composer selfupdate
21+
- composer update -o --no-interaction --prefer-stable
22+
- echo "extension=memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
23+
- echo "extension=redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
24+
25+
script:
26+
- vendor/bin/phpunit
27+
28+
after_success:
29+
- vendor/bin/coveralls

Handler/FilesHandler.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Koded package.
5+
*
6+
* (c) Mihail Binev <mihail@kodeart.com>
7+
*
8+
* Please view the LICENSE distributed with this source code
9+
* for the full copyright and license information.
10+
*
11+
*/
12+
13+
namespace Koded\Session\Handler;
14+
15+
use Koded\Session\SessionConfiguration;
16+
use SessionHandler;
17+
18+
19+
final class FilesHandler extends SessionHandler
20+
{
21+
22+
public function __construct(SessionConfiguration $settings)
23+
{
24+
ini_set('session.save_handler', 'files');
25+
ini_set('session.save_path', $settings->get('save_path', session_save_path()));
26+
ini_set('session.serialize_handler', $settings->get('serialize_handler', 'php'));
27+
}
28+
}

Handler/MemcachedHandler.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Koded package.
5+
*
6+
* (c) Mihail Binev <mihail@kodeart.com>
7+
*
8+
* Please view the LICENSE distributed with this source code
9+
* for the full copyright and license information.
10+
*
11+
*/
12+
13+
namespace Koded\Session\Handler;
14+
15+
use Koded\Caching\Client\MemcachedClient;
16+
use Koded\Caching\Configuration\MemcachedConfiguration;
17+
use Koded\Session\SessionConfiguration;
18+
use Memcached;
19+
use SessionHandlerInterface;
20+
21+
22+
final class MemcachedHandler implements SessionHandlerInterface
23+
{
24+
25+
/**
26+
* @var int
27+
*/
28+
protected $ttl;
29+
30+
/**
31+
* @var Memcached
32+
*/
33+
private $client;
34+
35+
public function __construct(SessionConfiguration $settings)
36+
{
37+
$this->ttl = (int)$settings->get('gc_maxlifetime', ini_get('session.gc_maxlifetime'));
38+
$this->client = (new MemcachedClient(new Memcached, $this->configuration($settings)))->client();
39+
}
40+
41+
public function close(): bool
42+
{
43+
return true;
44+
}
45+
46+
public function destroy($sessionId): bool
47+
{
48+
return $this->client->delete($sessionId);
49+
}
50+
51+
public function open($savePath, $sessionId): bool
52+
{
53+
return true;
54+
}
55+
56+
public function read($sessionId): string
57+
{
58+
return $this->client->get($sessionId) ?: '';
59+
}
60+
61+
public function write($sessionId, $sessionData): bool
62+
{
63+
return $this->client->set($sessionId, $sessionData, $this->ttl + time());
64+
}
65+
66+
/**
67+
* @codeCoverageIgnore
68+
*/
69+
public function gc($maxLifetime): bool
70+
{
71+
return true;
72+
}
73+
74+
private function configuration(SessionConfiguration $settings): MemcachedConfiguration
75+
{
76+
return new MemcachedConfiguration([
77+
'id' => $settings->get('id', $settings->get('name', ini_get('session.name'))),
78+
'servers' => $settings->get('servers', []),
79+
'options' => $settings->get('options', []) + [
80+
Memcached::OPT_PREFIX_KEY => $settings->prefix ?? 'session.'
81+
]
82+
]);
83+
}
84+
}

Handler/RedisHandler.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Koded package.
5+
*
6+
* (c) Mihail Binev <mihail@kodeart.com>
7+
*
8+
* Please view the LICENSE distributed with this source code
9+
* for the full copyright and license information.
10+
*
11+
*/
12+
13+
namespace Koded\Session\Handler;
14+
15+
use Koded\Caching\Client\RedisClient;
16+
use Koded\Caching\Configuration\RedisConfiguration;
17+
use Koded\Session\SessionConfiguration;
18+
use Redis;
19+
use SessionHandlerInterface;
20+
21+
22+
final class RedisHandler implements SessionHandlerInterface
23+
{
24+
25+
/**
26+
* @var int
27+
*/
28+
protected $ttl;
29+
30+
/**
31+
* @var Redis
32+
*/
33+
private $client;
34+
35+
public function __construct(SessionConfiguration $settings)
36+
{
37+
$this->ttl = (int)$settings->get('gc_maxlifetime', ini_get('session.gc_maxlifetime'));
38+
$this->client = (new RedisClient(new Redis, $this->configuration($settings)))->client();
39+
}
40+
41+
public function close(): bool
42+
{
43+
return true;
44+
}
45+
46+
public function destroy($sessionId): bool
47+
{
48+
return $this->client->del($sessionId) > 0;
49+
}
50+
51+
/**
52+
* @codeCoverageIgnore
53+
*/
54+
public function gc($maxLifetime): bool
55+
{
56+
return true;
57+
}
58+
59+
public function read($sessionId): string
60+
{
61+
return $this->client->get($sessionId) ?: '';
62+
}
63+
64+
public function write($sessionId, $sessionData): bool
65+
{
66+
if ($this->ttl > 0) {
67+
return $this->client->setex($sessionId, $this->ttl, $sessionData);
68+
}
69+
70+
return $this->client->set($sessionId, $sessionData, $this->ttl);
71+
}
72+
73+
public function open($savePath, $sessionId): bool
74+
{
75+
return true;
76+
}
77+
78+
private function configuration(SessionConfiguration $settings): RedisConfiguration
79+
{
80+
return new RedisConfiguration([
81+
'prefix' => (string)$settings->get('prefix', 'session:'),
82+
'host' => (string)$settings->get('host'),
83+
'port' => (int)$settings->get('port', 6379),
84+
'timeout' => (float)$settings->get('timeout', 0.0),
85+
'retry' => (int)$settings->get('retry', 0),
86+
'db' => (int)$settings->get('db', 0),
87+
]);
88+
}
89+
}

LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
BSD 3-Clause License
2+
3+
Koded - Session library
4+
Copyright (c) 2018, Mihail Binev
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice, this
11+
list of conditions and the following disclaimer.
12+
13+
* Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
* Neither the name of the copyright holder nor the names of its
18+
contributors may be used to endorse or promote products derived from
19+
this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

NotAuthenticatedMiddleware.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Koded package.
5+
*
6+
* (c) Mihail Binev <mihail@kodeart.com>
7+
*
8+
* Please view the LICENSE distributed with this source code
9+
* for the full copyright and license information.
10+
*
11+
*/
12+
13+
namespace Koded\Session;
14+
15+
use Koded\Http\{StatusCode, ServerResponse};
16+
use Koded\Stdlib\Interfaces\ConfigurationFactory;
17+
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
18+
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
19+
20+
21+
class NotAuthenticatedMiddleware implements MiddlewareInterface
22+
{
23+
24+
public const SESSION_AUTHENTICATED = 'authenticated';
25+
public const SESSION_REDIRECT_TO = 'login_uri';
26+
27+
private $redirectTo = '/';
28+
29+
public function __construct(ConfigurationFactory $configuration)
30+
{
31+
$this->redirectTo = $configuration->get(self::SESSION_REDIRECT_TO, '/');
32+
}
33+
34+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
35+
{
36+
if (true !== $_SESSION[self::SESSION_AUTHENTICATED]) {
37+
// Ajax requests should be handled in the browser
38+
if (strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') === 'XMLHTTPREQUEST') {
39+
return (new ServerResponse(json_encode([
40+
'location' => $this->redirectTo,
41+
'status' => StatusCode::UNAUTHORIZED
42+
], JSON_UNESCAPED_SLASHES), StatusCode::UNAUTHORIZED));
43+
}
44+
45+
return (new ServerResponse(null, StatusCode::TEMPORARY_REDIRECT))
46+
->withHeader('Location', $this->redirectTo);
47+
}
48+
49+
return $handler->handle($request);
50+
}
51+
}

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Koded Session
2+
=============
3+
4+
The library relies on the `php.ini` settings.
5+
Every session ini directive can be reset in the
6+
configuration object.
7+
8+
Refer to session php.ini directives:
9+
10+
11+
Usage
12+
-----
13+
14+
The session is started automatically by using one of the 2 methods:
15+
16+
### app configuration
17+
```php
18+
[
19+
'session' => [
20+
// your ini "session." overwrites (without "session." prefix)
21+
]
22+
]
23+
```
24+
25+
### or using a `SessionMiddleware`
26+
include this middleware class in your middleware stack
27+
28+
```php
29+
// your middleware stack
30+
$middleware = [
31+
SessionMiddleware::class
32+
];
33+
```
34+
35+
Session class and function
36+
--------------------------
37+
38+
```php
39+
session()->get('key');
40+
session()->set('key', 'value');
41+
// etc.
42+
```
43+
44+
The session class can be instantiated and used, but function `session()`
45+
is much preferred instead an instance of `Session` class.
46+
47+
Redis configuration
48+
-------------------
49+
50+
```php
51+
[
52+
'session' => [
53+
'save_handler' => 'redis'
54+
]
55+
]
56+
```

0 commit comments

Comments
 (0)