Skip to content

Commit 07f92b9

Browse files
committed
refactor: add property for SessionConfig and use it
1 parent f493f7f commit 07f92b9

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

phpstan-baseline.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,6 @@ parameters:
295295
count: 1
296296
path: system/Session/Handlers/RedisHandler.php
297297

298-
-
299-
message: "#^Property CodeIgniter\\\\Session\\\\Session\\:\\:\\$sessionExpiration \\(int\\) in isset\\(\\) is not nullable\\.$#"
300-
count: 1
301-
path: system/Session/Session.php
302-
303298
-
304299
message: "#^Negated boolean expression is always false\\.$#"
305300
count: 1

system/Session/Session.php

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ class Session implements SessionInterface
4343
* The storage driver to use: files, database, redis, memcached
4444
*
4545
* @var string
46+
*
47+
* @deprecated Use $this->config->driver.
4648
*/
4749
protected $sessionDriverName;
4850

4951
/**
5052
* The session cookie name, must contain only [0-9a-z_-] characters.
5153
*
5254
* @var string
55+
*
56+
* @deprecated Use $this->config->cookieName.
5357
*/
5458
protected $sessionCookieName = 'ci_session';
5559

@@ -58,11 +62,13 @@ class Session implements SessionInterface
5862
* Setting it to 0 (zero) means expire when the browser is closed.
5963
*
6064
* @var int
65+
*
66+
* @deprecated Use $this->config->expiration.
6167
*/
6268
protected $sessionExpiration = 7200;
6369

6470
/**
65-
* The location to save sessions to, driver dependent..
71+
* The location to save sessions to, driver dependent.
6672
*
6773
* For the 'files' driver, it's a path to a writable directory.
6874
* WARNING: Only absolute paths are supported!
@@ -74,6 +80,8 @@ class Session implements SessionInterface
7480
* IMPORTANT: You are REQUIRED to set a valid save path!
7581
*
7682
* @var string
83+
*
84+
* @deprecated Use $this->config->savePath.
7785
*/
7886
protected $sessionSavePath;
7987

@@ -84,13 +92,17 @@ class Session implements SessionInterface
8492
* your session table's PRIMARY KEY when changing this setting.
8593
*
8694
* @var bool
95+
*
96+
* @deprecated Use $this->config->matchIP.
8797
*/
8898
protected $sessionMatchIP = false;
8999

90100
/**
91101
* How many seconds between CI regenerating the session ID.
92102
*
93103
* @var int
104+
*
105+
* @deprecated Use $this->config->timeToUpdate.
94106
*/
95107
protected $sessionTimeToUpdate = 300;
96108

@@ -100,6 +112,8 @@ class Session implements SessionInterface
100112
* will be later deleted by the garbage collector.
101113
*
102114
* @var bool
115+
*
116+
* @deprecated Use $this->config->regenerateDestroy.
103117
*/
104118
protected $sessionRegenerateDestroy = false;
105119

@@ -156,6 +170,11 @@ class Session implements SessionInterface
156170
*/
157171
protected $sidRegexp;
158172

173+
/**
174+
* Session Config
175+
*/
176+
protected SessionConfig $config;
177+
159178
/**
160179
* Constructor.
161180
*
@@ -165,14 +184,16 @@ public function __construct(SessionHandlerInterface $driver, SessionConfig $sess
165184
{
166185
$this->driver = $driver;
167186

187+
$this->config = $session;
188+
168189
// Store Session configurations
169190
$this->sessionDriverName = $session->driver;
170-
$this->sessionCookieName = $session->cookieName ?? $this->sessionCookieName;
171-
$this->sessionExpiration = $session->expiration ?? $this->sessionExpiration;
191+
$this->sessionCookieName = $session->cookieName;
192+
$this->sessionExpiration = $session->expiration;
172193
$this->sessionSavePath = $session->savePath;
173-
$this->sessionMatchIP = $session->matchIP ?? $this->sessionMatchIP;
174-
$this->sessionTimeToUpdate = $session->timeToUpdate ?? $this->sessionTimeToUpdate;
175-
$this->sessionRegenerateDestroy = $session->regenerateDestroy ?? $this->sessionRegenerateDestroy;
194+
$this->sessionMatchIP = $session->matchIP;
195+
$this->sessionTimeToUpdate = $session->timeToUpdate;
196+
$this->sessionRegenerateDestroy = $session->regenerateDestroy;
176197

177198
/** @var App $config */
178199
$config = config('App');
@@ -186,8 +207,8 @@ public function __construct(SessionHandlerInterface $driver, SessionConfig $sess
186207
/** @var CookieConfig|null $cookie */
187208
$cookie = config('Cookie');
188209

189-
$this->cookie = (new Cookie($this->sessionCookieName, '', [
190-
'expires' => $this->sessionExpiration === 0 ? 0 : Time::now()->getTimestamp() + $this->sessionExpiration,
210+
$this->cookie = (new Cookie($this->config->cookieName, '', [
211+
'expires' => $this->config->expiration === 0 ? 0 : Time::now()->getTimestamp() + $this->config->expiration,
191212
'path' => $cookie->path ?? $config->cookiePath,
192213
'domain' => $cookie->domain ?? $config->cookieDomain,
193214
'secure' => $cookie->secure ?? $config->cookieSecure,
@@ -230,32 +251,32 @@ public function start()
230251
$this->setSaveHandler();
231252

232253
// Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
233-
if (isset($_COOKIE[$this->sessionCookieName])
234-
&& (! is_string($_COOKIE[$this->sessionCookieName]) || ! preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[$this->sessionCookieName]))
254+
if (isset($_COOKIE[$this->config->cookieName])
255+
&& (! is_string($_COOKIE[$this->config->cookieName]) || ! preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[$this->config->cookieName]))
235256
) {
236-
unset($_COOKIE[$this->sessionCookieName]);
257+
unset($_COOKIE[$this->config->cookieName]);
237258
}
238259

239260
$this->startSession();
240261

241262
// Is session ID auto-regeneration configured? (ignoring ajax requests)
242263
if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
243-
&& ($regenerateTime = $this->sessionTimeToUpdate) > 0
264+
&& ($regenerateTime = $this->config->timeToUpdate) > 0
244265
) {
245266
if (! isset($_SESSION['__ci_last_regenerate'])) {
246267
$_SESSION['__ci_last_regenerate'] = Time::now()->getTimestamp();
247268
} elseif ($_SESSION['__ci_last_regenerate'] < (Time::now()->getTimestamp() - $regenerateTime)) {
248-
$this->regenerate((bool) $this->sessionRegenerateDestroy);
269+
$this->regenerate((bool) $this->config->regenerateDestroy);
249270
}
250271
}
251272
// Another work-around ... PHP doesn't seem to send the session cookie
252273
// unless it is being currently created or regenerated
253-
elseif (isset($_COOKIE[$this->sessionCookieName]) && $_COOKIE[$this->sessionCookieName] === session_id()) {
274+
elseif (isset($_COOKIE[$this->config->cookieName]) && $_COOKIE[$this->config->cookieName] === session_id()) {
254275
$this->setCookie();
255276
}
256277

257278
$this->initVars();
258-
$this->logger->info("Session: Class initialized using '" . $this->sessionDriverName . "' driver.");
279+
$this->logger->info("Session: Class initialized using '" . $this->config->driver . "' driver.");
259280

260281
return $this;
261282
}
@@ -270,7 +291,7 @@ public function start()
270291
public function stop()
271292
{
272293
setcookie(
273-
$this->sessionCookieName,
294+
$this->config->cookieName,
274295
session_id(),
275296
['expires' => 1, 'path' => $this->cookie->getPath(), 'domain' => $this->cookie->getDomain(), 'secure' => $this->cookie->isSecure(), 'httponly' => true]
276297
);
@@ -285,16 +306,12 @@ public function stop()
285306
*/
286307
protected function configure()
287308
{
288-
if (empty($this->sessionCookieName)) {
289-
$this->sessionCookieName = ini_get('session.name');
290-
} else {
291-
ini_set('session.name', $this->sessionCookieName);
292-
}
309+
ini_set('session.name', $this->config->cookieName);
293310

294311
$sameSite = $this->cookie->getSameSite() ?: ucfirst(Cookie::SAMESITE_LAX);
295312

296313
$params = [
297-
'lifetime' => $this->sessionExpiration,
314+
'lifetime' => $this->config->expiration,
298315
'path' => $this->cookie->getPath(),
299316
'domain' => $this->cookie->getDomain(),
300317
'secure' => $this->cookie->isSecure(),
@@ -305,14 +322,12 @@ protected function configure()
305322
ini_set('session.cookie_samesite', $sameSite);
306323
session_set_cookie_params($params);
307324

308-
if (! isset($this->sessionExpiration)) {
309-
$this->sessionExpiration = (int) ini_get('session.gc_maxlifetime');
310-
} elseif ($this->sessionExpiration > 0) {
311-
ini_set('session.gc_maxlifetime', (string) $this->sessionExpiration);
325+
if ($this->config->expiration > 0) {
326+
ini_set('session.gc_maxlifetime', (string) $this->config->expiration);
312327
}
313328

314-
if (! empty($this->sessionSavePath)) {
315-
ini_set('session.save_path', $this->sessionSavePath);
329+
if (! empty($this->config->savePath)) {
330+
ini_set('session.save_path', $this->config->savePath);
316331
}
317332

318333
// Security is king
@@ -419,12 +434,12 @@ private function removeOldSessionCookie(): void
419434
$response = Services::response();
420435
$cookieStoreInResponse = $response->getCookieStore();
421436

422-
if (! $cookieStoreInResponse->has($this->sessionCookieName)) {
437+
if (! $cookieStoreInResponse->has($this->config->cookieName)) {
423438
return;
424439
}
425440

426441
// CookieStore is immutable.
427-
$newCookieStore = $cookieStoreInResponse->remove($this->sessionCookieName);
442+
$newCookieStore = $cookieStoreInResponse->remove($this->config->cookieName);
428443

429444
// But clear() method clears cookies in the object (not immutable).
430445
$cookieStoreInResponse->clear();
@@ -924,7 +939,7 @@ protected function startSession()
924939
*/
925940
protected function setCookie()
926941
{
927-
$expiration = $this->sessionExpiration === 0 ? 0 : Time::now()->getTimestamp() + $this->sessionExpiration;
942+
$expiration = $this->config->expiration === 0 ? 0 : Time::now()->getTimestamp() + $this->config->expiration;
928943
$this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
929944

930945
$response = Services::response();

system/Test/Mock/MockSession.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function startSession()
5757
*/
5858
protected function setCookie()
5959
{
60-
$expiration = $this->sessionExpiration === 0 ? 0 : Time::now()->getTimestamp() + $this->sessionExpiration;
60+
$expiration = $this->config->expiration === 0 ? 0 : Time::now()->getTimestamp() + $this->config->expiration;
6161
$this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
6262

6363
$this->cookies[] = $this->cookie;

0 commit comments

Comments
 (0)