Skip to content

Commit c2a627f

Browse files
committed
Session: configuration options are normalized in setOptions() instead of configure() [Closes #121]
1 parent cc32169 commit c2a627f

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

src/Http/Session.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,19 @@ public function clean(): void
337337
*/
338338
public function setOptions(array $options)
339339
{
340+
$normalized = [];
341+
foreach ($options as $key => $value) {
342+
if (!strncmp($key, 'session.', 8)) { // back compatibility
343+
$key = substr($key, 8);
344+
}
345+
$key = strtolower(preg_replace('#(.)(?=[A-Z])#', '$1_', $key)); // camelCase -> snake_case
346+
$normalized[$key] = $value;
347+
}
340348
if (self::$started) {
341-
$this->configure($options);
349+
$this->configure($normalized);
342350
}
343-
$this->options = $options + $this->options;
344-
if (!empty($options['auto_start'])) {
351+
$this->options = $normalized + $this->options;
352+
if (!empty($normalized['auto_start'])) {
345353
$this->start();
346354
}
347355
return $this;
@@ -365,11 +373,6 @@ private function configure(array $config): void
365373
$special = ['cache_expire' => 1, 'cache_limiter' => 1, 'save_path' => 1, 'name' => 1];
366374

367375
foreach ($config as $key => $value) {
368-
if (!strncmp($key, 'session.', 8)) { // back compatibility
369-
$key = substr($key, 8);
370-
}
371-
$key = strtolower(preg_replace('#(.)(?=[A-Z])#', '$1_', $key));
372-
373376
if ($value === NULL || ini_get("session.$key") == $value) { // intentionally ==
374377
continue;
375378

tests/Http/Session.setOptions.phpt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Http\Session setOptions.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Http\Session;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
$factory = new Nette\Http\RequestFactory;
17+
$session = new Nette\Http\Session($factory->createHttpRequest(), new Nette\Http\Response);
18+
19+
Assert::same([
20+
'referer_check' => '',
21+
'use_cookies' => 1,
22+
'use_only_cookies' => 1,
23+
'use_trans_sid' => 0,
24+
'cookie_lifetime' => 0,
25+
'cookie_path' => '/',
26+
'cookie_domain' => '',
27+
'cookie_secure' => FALSE,
28+
'cookie_httponly' => TRUE,
29+
'gc_maxlifetime' => 10800,
30+
], $session->getOptions());
31+
32+
$session->setOptions([
33+
'cookieDomain' => '.domain.com',
34+
]);
35+
Assert::same([
36+
'cookie_domain' => '.domain.com',
37+
'referer_check' => '',
38+
'use_cookies' => 1,
39+
'use_only_cookies' => 1,
40+
'use_trans_sid' => 0,
41+
'cookie_lifetime' => 0,
42+
'cookie_path' => '/',
43+
'cookie_secure' => FALSE,
44+
'cookie_httponly' => TRUE,
45+
'gc_maxlifetime' => 10800,
46+
], $session->getOptions());
47+
48+
$session->setOptions([
49+
'session.cookie_domain' => '.domain.org',
50+
]);
51+
Assert::same([
52+
'cookie_domain' => '.domain.org',
53+
'referer_check' => '',
54+
'use_cookies' => 1,
55+
'use_only_cookies' => 1,
56+
'use_trans_sid' => 0,
57+
'cookie_lifetime' => 0,
58+
'cookie_path' => '/',
59+
'cookie_secure' => FALSE,
60+
'cookie_httponly' => TRUE,
61+
'gc_maxlifetime' => 10800,
62+
], $session->getOptions());

0 commit comments

Comments
 (0)