Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 39526df

Browse filesBrowse files
author
Drak
committed
[HttpFoundation] Refactor away options property.
It does not make sense to try and store session ini directives since they can be changes outside of the class as they are part of the global state. Coding stan
1 parent 21221f7 commit 39526df
Copy full SHA for 39526df

File tree

Expand file treeCollapse file tree

2 files changed

+39
-30
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+39
-30
lines changed

‎src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function start()
7676
*/
7777
public function regenerate($destroy = false)
7878
{
79-
if ($this->options['auto_start'] && !$this->started) {
79+
if (!$this->started) {
8080
$this->start();
8181
}
8282

@@ -124,6 +124,22 @@ public function clear()
124124
$this->loadSession($this->sessionData);
125125
}
126126

127+
/**
128+
* {@inheritdoc}
129+
*/
130+
public function getBag($name)
131+
{
132+
if (!isset($this->bags[$name])) {
133+
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
134+
}
135+
136+
if (!$this->started) {
137+
$this->start();
138+
}
139+
140+
return $this->bags[$name];
141+
}
142+
127143
/**
128144
* Generates a session ID.
129145
*

‎src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorage.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorage.php
+22-29Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ class SessionStorage implements SessionStorageInterface
3030
*/
3131
protected $bags;
3232

33-
/**
34-
* @var array
35-
*/
36-
protected $options = array();
37-
3833
/**
3934
* @var boolean
4035
*/
@@ -93,6 +88,11 @@ class SessionStorage implements SessionStorageInterface
9388
*/
9489
public function __construct(array $options = array(), $handler = null)
9590
{
91+
// sensible defaults
92+
ini_set('session.auto_start', 0); // by default we prefer to explicitly start the session using the class.
93+
ini_set('session.cache_limiter', ''); // disable by default because it's managed by HeaderBag (if used)
94+
ini_set('session.use_cookies', 1);
95+
9696
$this->setOptions($options);
9797
$this->setSaveHandler($handler);
9898
}
@@ -116,7 +116,15 @@ public function start()
116116
return true;
117117
}
118118

119-
if ($this->options['use_cookies'] && headers_sent()) {
119+
// catch condition where session was started automatically by PHP
120+
if (!$this->started && !$this->closed && $this->saveHandler->isActive()
121+
&& $this->saveHandler->isSessionHandlerInterface()) {
122+
$this->loadSession();
123+
124+
return true;
125+
}
126+
127+
if (ini_get('session.use_cookies') && headers_sent()) {
120128
throw new \RuntimeException('Failed to start the session because headers have already been sent.');
121129
}
122130

@@ -131,9 +139,6 @@ public function start()
131139
$this->saveHandler->setActive(false);
132140
}
133141

134-
$this->started = true;
135-
$this->closed = false;
136-
137142
return true;
138143
}
139144

@@ -205,8 +210,10 @@ public function getBag($name)
205210
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
206211
}
207212

208-
if ($this->options['auto_start'] && !$this->started) {
213+
if (ini_get('session.auto_start') && !$this->started) {
209214
$this->start();
215+
} else if ($this->saveHandler->isActive() && !$this->started) {
216+
$this->loadSession();
210217
}
211218

212219
return $this->bags[$name];
@@ -218,30 +225,13 @@ public function getBag($name)
218225
* For convenience we omit 'session.' from the beginning of the keys.
219226
* Explicitly ignores other ini keys.
220227
*
221-
* session_get_cookie_params() overrides values.
222-
*
223-
* @param array $options
228+
* @param array $options Session ini directives array(key => value).
224229
*
225230
* @see http://php.net/session.configuration
226231
*/
227232
public function setOptions(array $options)
228233
{
229-
$this->options = $options;
230-
231-
// set defaults for certain values
232-
$defaults = array(
233-
'cache_limiter' => '', // disable by default because it's managed by HeaderBag (if used)
234-
'auto_start' => false,
235-
'use_cookies' => true,
236-
);
237-
238-
foreach ($defaults as $key => $value) {
239-
if (!isset($this->options[$key])) {
240-
$this->options[$key] = $value;
241-
}
242-
}
243-
244-
foreach ($this->options as $key => $value) {
234+
foreach ($options as $key => $value) {
245235
if (in_array($key, array(
246236
'auto_start', 'cache_limiter', 'cookie_domain', 'cookie_httponly',
247237
'cookie_lifetime', 'cookie_path', 'cookie_secure',
@@ -322,5 +312,8 @@ protected function loadSession(array &$session = null)
322312
$session[$key] = isset($session[$key]) ? $session[$key] : array();
323313
$bag->initialize($session[$key]);
324314
}
315+
316+
$this->started = true;
317+
$this->closed = false;
325318
}
326319
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.