Skip to content

Commit

Permalink
Session: configuration options are normalized in setOptions() instead…
Browse files Browse the repository at this point in the history
… of configure() [Closes #121]
  • Loading branch information
dg committed Mar 16, 2017
1 parent a6b4751 commit ef81204
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Http/Session.php
Expand Up @@ -352,11 +352,19 @@ public function clean()
*/
public function setOptions(array $options)
{
$normalized = [];
foreach ($options as $key => $value) {
if (!strncmp($key, 'session.', 8)) { // back compatibility
$key = substr($key, 8);
}
$key = strtolower(preg_replace('#(.)(?=[A-Z])#', '$1_', $key)); // camelCase -> snake_case
$normalized[$key] = $value;
}
if (self::$started) {
$this->configure($options);
$this->configure($normalized);
}
$this->options = $options + $this->options;
if (!empty($options['auto_start'])) {
$this->options = $normalized + $this->options;
if (!empty($normalized['auto_start'])) {
$this->start();
}
return $this;
Expand All @@ -383,11 +391,6 @@ private function configure(array $config)
$special = ['cache_expire' => 1, 'cache_limiter' => 1, 'save_path' => 1, 'name' => 1];

foreach ($config as $key => $value) {
if (!strncmp($key, 'session.', 8)) { // back compatibility
$key = substr($key, 8);
}
$key = strtolower(preg_replace('#(.)(?=[A-Z])#', '$1_', $key));

if ($value === NULL || ini_get("session.$key") == $value) { // intentionally ==
continue;

Expand Down
60 changes: 60 additions & 0 deletions tests/Http/Session.setOptions.phpt
@@ -0,0 +1,60 @@
<?php

/**
* Test: Nette\Http\Session setOptions.
*/

use Nette\Http\Session;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$factory = new Nette\Http\RequestFactory;
$session = new Nette\Http\Session($factory->createHttpRequest(), new Nette\Http\Response);

Assert::same([
'referer_check' => '',
'use_cookies' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_lifetime' => 0,
'cookie_path' => '/',
'cookie_domain' => '',
'cookie_secure' => FALSE,
'cookie_httponly' => TRUE,
'gc_maxlifetime' => 10800,
], $session->getOptions());

$session->setOptions([
'cookieDomain' => '.domain.com',
]);
Assert::same([
'cookie_domain' => '.domain.com',
'referer_check' => '',
'use_cookies' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_lifetime' => 0,
'cookie_path' => '/',
'cookie_secure' => FALSE,
'cookie_httponly' => TRUE,
'gc_maxlifetime' => 10800,
], $session->getOptions());

$session->setOptions([
'session.cookie_domain' => '.domain.org',
]);
Assert::same([
'cookie_domain' => '.domain.org',
'referer_check' => '',
'use_cookies' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_lifetime' => 0,
'cookie_path' => '/',
'cookie_secure' => FALSE,
'cookie_httponly' => TRUE,
'gc_maxlifetime' => 10800,
], $session->getOptions());

0 comments on commit ef81204

Please sign in to comment.