Skip to content

Commit 227e606

Browse files
committed
options
1 parent 7eca70c commit 227e606

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ $adapter = new RedisAdapter([
5252
'port' => 6379, // int
5353
'timeout' => 0, // int
5454
'password' => null, // null or string
55-
], 0, 86400, 'sess');
55+
'database' => 0,
56+
'ttl' => 86400,
57+
'prefix' => 'sess'
58+
]);
5659

5760
Session::createImmutable($adapter)
5861
->start();
@@ -67,7 +70,7 @@ use InitPHP\Sessions\Adapters\PDOAdapter;
6770

6871
$pdo = new \PDO('mysql:host=localhost;dbname=test', 'root', '');
6972

70-
$adapter = new PDOAdapter($pdo, 'app_sessions');
73+
$adapter = new PDOAdapter(['pdo' => $pdo, 'table' => 'app_sessions']);
7174

7275
Session::createImmutable($adapter)
7376
->start();
@@ -113,7 +116,7 @@ require_once "vendor/autoload.php";
113116
use InitPHP\Sessions\Session;
114117
use InitPHP\Sessions\Adapters\CookieAdapter;
115118

116-
$adapter = new CookieAdapter('sessDataCookieName', 'topSecretAppKey', 86400);
119+
$adapter = new CookieAdapter(['name' => 'sessDataCookieName', 'key' => 'topSecretAppKey', 'ttl' => 86400]);
117120

118121
Session::createImmutable($adapter)
119122
->start();
@@ -126,7 +129,7 @@ require_once "vendor/autoload.php";
126129
use InitPHP\Sessions\Session;
127130
use InitPHP\Sessions\Adapters\MongoDBAdapter;
128131

129-
$adapter = new MongoDBAdapter('mongodb://127.0.0.1:27017', 'sessDbName.sessCollectionName');
132+
$adapter = new MongoDBAdapter(['dsn' => 'mongodb://127.0.0.1:27017', 'collation' => 'sessDbName.sessCollectionName']);
130133

131134
Session::createImmutable($adapter)
132135
->start();

src/Adapters/CookieAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class CookieAdapter extends AbstractAdapter implements AdapterInterface
3636
/** @var \InitPHP\Encryption\HandlerInterface */
3737
private $encrypt;
3838

39-
public function __construct(string $cookieName, string $key, ?int $ttl = null)
39+
public function __construct(array $options)
4040
{
41-
$this->name = $cookieName;
41+
$this->name = $options['name'];
4242

4343
if (!class_exists("\\InitPHP\\Encryption\\Encrypt")) {
4444
throw new SessionNotSupportedAdapter('This adapter depends on the InitPHP Encryption library to work. Run the command : "composer require initphp/encryption"');
@@ -47,11 +47,11 @@ public function __construct(string $cookieName, string $key, ?int $ttl = null)
4747
$this->encrypt = \InitPHP\Encryption\Encrypt::use(\InitPHP\Encryption\OpenSSL::class, [
4848
'algo' => 'SHA256',
4949
'cipher' => 'AES-256-CTR',
50-
'key' => $key,
50+
'key' => $options['key'],
5151
'blocksize' => 16,
5252
]);
5353

54-
$this->ttl = $ttl ?? 86400;
54+
$this->ttl = $options['ttl'] ?? 86400;
5555

5656
$this->_decode();
5757
}

src/Adapters/MongoDBAdapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MongoDBAdapter extends \InitPHP\Sessions\AbstractAdapter implements \InitP
2626

2727
private string $collection;
2828

29-
public function __construct(string $dsn, string $collection)
29+
public function __construct(array $options)
3030
{
3131
if (
3232
!extension_loaded("MongoDB")
@@ -37,11 +37,11 @@ public function __construct(string $dsn, string $collection)
3737
throw new SessionNotSupportedAdapter();
3838
}
3939
try {
40-
$this->manager = new \MongoDB\Driver\Manager($dsn);
40+
$this->manager = new \MongoDB\Driver\Manager($options['dsn']);
4141
}catch (\Exception $e) {
4242
throw new SessionException("MongoDB connection failed." . $e->getMessage(), (int)$e->getCode());
4343
}
44-
$this->collection = $collection;
44+
$this->collection = $options['collection'];
4545
}
4646

4747
/**

src/Adapters/PDOAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class PDOAdapter extends AbstractAdapter implements AdapterInterface
3737

3838
protected bool $withIPAddress;
3939

40-
public function __construct(\PDO $pdo, string $table, bool $withIPAddress = false)
40+
public function __construct(array $options)
4141
{
42-
$this->pdo = $pdo;
43-
$this->table = $table;
44-
$this->withIPAddress = $withIPAddress;
42+
$this->pdo = $options['pdo'];
43+
$this->table = $options['table'];
44+
$this->withIPAddress = $options['withIPAddress'] ?? false;
4545
}
4646

4747
/**

src/Adapters/RedisAdapter.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,29 @@ class RedisAdapter extends \InitPHP\Sessions\AbstractAdapter implements \InitPHP
3030
private ?string $prefix;
3131

3232
/**
33-
* @param \Redis|array $redis
34-
* @param int $database
35-
* @param int $ttl
36-
* @param string|null $prefix
33+
* @param array $options
34+
* @throws SessionException
3735
*/
38-
public function __construct($redis, int $database = 0, int $ttl = 864000, ?string $prefix = null)
36+
public function __construct(array $options)
3937
{
4038
if (!extension_loaded('redis')) {
4139
throw new SessionNotSupportedAdapter();
4240
}
4341

44-
$this->ttl = $ttl;
45-
$this->database = $database;
46-
$this->prefix = $prefix;
42+
$this->ttl = $options['ttl'] ?? 864000;
43+
$this->database = $options['database'] ?? 0;
44+
$this->prefix = $options['prefix'] ?? null;
4745

48-
if ($redis instanceof \Redis) {
49-
$this->redis = $redis;
50-
} elseif (is_array($redis)) {
46+
if ($options['redis'] instanceof \Redis) {
47+
$this->redis = $options['redis'];
48+
} elseif (is_array($options['redis'])) {
5149
try {
5250
$this->redis = new \Redis();
5351

54-
if(!$this->redis->connect($redis['host'], $redis['port'], ($redis['timeout'] ?? 0))){
52+
if(!$this->redis->connect($options['redis']['host'], $options['redis']['port'], ($options['redis']['timeout'] ?? 0))){
5553
throw new \Exception('Redis Cache connection failed.');
5654
}
57-
$password = $redis['password'] ?? null;
55+
$password = $options['redis']['password'] ?? null;
5856
if($password !== null && !$this->redis->auth($password)){
5957
throw new \Exception('Redis Cache authentication failed.');
6058
}

0 commit comments

Comments
 (0)