Skip to content

Commit fba6bed

Browse files
committed
update README + update Session::start
1 parent 5442ab5 commit fba6bed

2 files changed

Lines changed: 93 additions & 24 deletions

File tree

‎README.md‎

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,80 @@ composer require rancoud/session
1010
```
1111

1212
## How to use it?
13+
The most popular
1314
```php
15+
Session::set('key', 'value');
16+
$value = Session::get('key');
17+
```
18+
In read only
19+
```php
20+
Session::readOnly();
21+
Session::set('key', 'value');
22+
$value = Session::get('key');
23+
```
24+
With custom options
25+
```php
26+
Session::start(['read_and_close' => true]);
27+
Session::set('key', 'value');
28+
$value = Session::get('key');
29+
```
30+
With encryption on default php session
31+
```php
32+
Session::useDefaultEncryptionDriver('keyForEncryption');
33+
Session::set('key', 'value');
34+
$value = Session::get('key');
35+
```
36+
With File driver
37+
```php
38+
Session::useFileDriver();
39+
Session::set('key', 'value');
40+
$value = Session::get('key');
41+
```
42+
With Database driver (you have to install rancoud/database)
43+
```php
44+
$conf = new \Rancoud\Database\Configurator([
45+
'engine' => 'mysql',
46+
'host' => '127.0.0.1',
47+
'user' => 'root',
48+
'password' => '',
49+
'database' => 'test_database'
50+
]);
51+
$db = new \Rancoud\Database\Database($conf);
52+
53+
// for using a current connection
54+
Session::useCurrentDatabaseDriver($db);
55+
56+
// for creating a new connection
57+
//Session::useNewDatabaseDriver($conf);
1458

59+
Session::set('key', 'value');
60+
$value = Session::get('key');
1561
```
62+
With Redis driver (you have to install predis/predis)
63+
```php
64+
$params = [
65+
'scheme' => 'tcp',
66+
'host' => '127.0.0.1',
67+
'port' => 6379,
68+
];
69+
$redis = new Predis\Client($params);
1670

17-
## Session Constructor
18-
### Settings
19-
#### Mandatory
20-
| Parameter | Type | Description |
21-
| --- | --- | --- |
22-
| | | |
71+
// for using a current connection
72+
Session::useCurrentRedisDriver($redis);
2373

24-
#### Optionnals
25-
| Parameter | Type | Default value | Description |
26-
| --- | --- | --- | --- |
27-
| | | | |
74+
// for creating a new connection
75+
//Session::useNewRedisDriver($params);
76+
77+
Session::set('key', 'value');
78+
$value = Session::get('key');
79+
```
80+
With your own driver implementing SessionHandlerInterface
81+
```php
82+
$driver = new MyCustomDriver();
83+
Session::useCustomDriver($driver);
84+
Session::set('key', 'value');
85+
$value = Session::get('key');
86+
```
2887

2988
## Session Methods
3089
### General Commands
@@ -37,7 +96,7 @@ Extends SessionHandler
3796
## Database
3897
You need to install
3998
```php
40-
composer require rancoud/session
99+
composer require rancoud/database
41100
```
42101
```sql
43102
CREATE TABLE IF NOT EXISTS `sessions` (

‎src/Session.php‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
class Session
1515
{
16+
/** @var bool */
1617
protected static $hasStarted = false;
1718
/** @var SessionHandlerInterface */
1819
protected static $driver = null;
@@ -24,29 +25,35 @@ class Session
2425
protected static $cookieDomain = null;
2526
/** @var string */
2627
protected static $lifetime = 1440;
28+
/** @var bool */
29+
protected static $readOnly = false;
30+
protected static $options = [];
2731

2832
/**
29-
* @throws \Exception
33+
* @param array $options
34+
*
35+
* @throws Exception
3036
*/
31-
public static function start(): void
37+
public static function start(array $options = []): void
3238
{
3339
static::throwExceptionIfHasStarted();
3440

3541
static::configureDriver();
3642
static::setupSessionParameters();
37-
static::startSession();
38-
static::$driver->gc(static::$lifetime);
43+
static::startSession($options);
3944

4045
static::$hasStarted = true;
46+
if (array_key_exists('read_and_close', $options) && $options['read_and_close'] === true) {
47+
static::$hasStarted = false;
48+
}
4149
}
4250

4351
/**
4452
* @throws \Exception
4553
*/
4654
protected static function throwExceptionIfHasStarted(): void
4755
{
48-
$status = static::status();
49-
if ($status === PHP_SESSION_ACTIVE) {
56+
if (static::hasStarted()) {
5057
throw new Exception('Session already started');
5158
}
5259
}
@@ -432,6 +439,7 @@ protected static function setupIniSession(array $options = []): array
432439
$options['cookie_httponly'] = $options['cookie_httponly'] ?? '1';
433440
$options['use_only_cookies'] = $options['use_only_cookies'] ?? '1';
434441
$options['use_trans_sid'] = $options['use_trans_sid'] ?? '0';
442+
$options['read_and_close'] = $options['read_and_close'] ?? static::$readOnly;
435443

436444
return $options;
437445
}
@@ -468,12 +476,17 @@ public static function abort(): bool
468476
return session_abort();
469477
}
470478

479+
public static function readOnly(): void
480+
{
481+
static::$readOnly = true;
482+
}
483+
471484
/**
472-
* @return int
485+
* @return bool
473486
*/
474-
public static function status(): int
487+
public static function hasStarted(): bool
475488
{
476-
return session_status();
489+
return static::$hasStarted;
477490
}
478491

479492
/**
@@ -551,11 +564,8 @@ public static function remove($key): void
551564
*/
552565
protected static function startSessionIfNotHasStarted(): void
553566
{
554-
$status = static::status();
555-
if ($status === PHP_SESSION_NONE) {
567+
if (!static::hasStarted()) {
556568
static::start();
557-
} elseif ($status === PHP_SESSION_DISABLED) {
558-
throw new Exception('Session disabled');
559569
}
560570
}
561571

0 commit comments

Comments
 (0)