Skip to content

Commit 8997102

Browse files
mabardg
authored andcommitted
Session: added read_and_close configuration (#156)
1 parent ab9c35e commit 8997102

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Bridges/HttpDI/SessionExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getConfigSchema(): Nette\Schema\Schema
3939
'autoStart' => Expect::anyOf('smart', true, false)->default('smart'),
4040
'expiration' => Expect::string()->dynamic(),
4141
'handler' => Expect::string()->dynamic(),
42+
'readAndClose' => Expect::bool(),
4243
])->otherItems('mixed');
4344
}
4445

@@ -76,6 +77,9 @@ public function loadConfiguration()
7677

7778
$options = (array) $config;
7879
unset($options['expiration'], $options['handler'], $options['autoStart'], $options['debugger']);
80+
if ($options['readAndClose'] === null) {
81+
unset($options['readAndClose']);
82+
}
7983
if (!empty($options)) {
8084
$session->addSetup('setOptions', [$options]);
8185
}

src/Http/Session.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class Session
5353
/** @var \SessionHandlerInterface */
5454
private $handler;
5555

56+
/** @var bool */
57+
private $readAndClose = false;
58+
5659

5760
public function __construct(IRequest $request, IResponse $response)
5861
{
@@ -90,7 +93,7 @@ public function start(): void
9093

9194
try {
9295
// session_start returns false on failure only sometimes
93-
Nette\Utils\Callback::invokeSafe('session_start', [], function (string $message) use (&$e): void {
96+
Nette\Utils\Callback::invokeSafe('session_start', [['read_and_close' => $this->readAndClose]], function (string $message) use (&$e): void {
9497
$e = new Nette\InvalidStateException($message);
9598
});
9699
} catch (\Exception $e) {
@@ -329,6 +332,13 @@ public function setOptions(array $options)
329332
$key = strtolower(preg_replace('#(.)(?=[A-Z])#', '$1_', $key)); // camelCase -> snake_case
330333
$normalized[$key] = $value;
331334
}
335+
if (!empty($normalized['read_and_close'])) {
336+
if (session_status() === PHP_SESSION_ACTIVE) {
337+
throw new Nette\InvalidStateException('Cannot configure "read_and_close" for already started session.');
338+
}
339+
$this->readAndClose = (bool) $normalized['read_and_close'];
340+
unset($normalized['read_and_close']);
341+
}
332342
if (session_status() === PHP_SESSION_ACTIVE) {
333343
$this->configure($normalized);
334344
}

0 commit comments

Comments
 (0)