Skip to content

Commit 94bd348

Browse files
authored
Merge pull request #6989 from kenjis/refactor-add-session-config
refactor: add Config\Session
2 parents 945ea26 + c67f708 commit 94bd348

23 files changed

Lines changed: 380 additions & 176 deletions

File tree

app/Config/App.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ class App extends BaseConfig
149149
* - `CodeIgniter\Session\Handlers\RedisHandler`
150150
*
151151
* @var string
152+
*
153+
* @deprecated use Config\Session::$driver instead.
152154
*/
153155
public $sessionDriver = FileHandler::class;
154156

@@ -158,6 +160,8 @@ class App extends BaseConfig
158160
* --------------------------------------------------------------------------
159161
*
160162
* The session cookie name, must contain only [0-9a-z_-] characters
163+
*
164+
* @deprecated use Config\Session::$cookieName instead.
161165
*/
162166
public string $sessionCookieName = 'ci_session';
163167

@@ -168,6 +172,8 @@ class App extends BaseConfig
168172
*
169173
* The number of SECONDS you want the session to last.
170174
* Setting to 0 (zero) means expire when the browser is closed.
175+
*
176+
* @deprecated use Config\Session::$expiration instead.
171177
*/
172178
public int $sessionExpiration = 7200;
173179

@@ -185,6 +191,8 @@ class App extends BaseConfig
185191
* Please read up the manual for the format with other session drivers.
186192
*
187193
* IMPORTANT: You are REQUIRED to set a valid save path!
194+
*
195+
* @deprecated use Config\Session::$savePath instead.
188196
*/
189197
public string $sessionSavePath = WRITEPATH . 'session';
190198

@@ -197,6 +205,8 @@ class App extends BaseConfig
197205
*
198206
* WARNING: If you're using the database driver, don't forget to update
199207
* your session table's PRIMARY KEY when changing this setting.
208+
*
209+
* @deprecated use Config\Session::$matchIP instead.
200210
*/
201211
public bool $sessionMatchIP = false;
202212

@@ -206,6 +216,8 @@ class App extends BaseConfig
206216
* --------------------------------------------------------------------------
207217
*
208218
* How many seconds between CI regenerating the session ID.
219+
*
220+
* @deprecated use Config\Session::$timeToUpdate instead.
209221
*/
210222
public int $sessionTimeToUpdate = 300;
211223

@@ -217,9 +229,22 @@ class App extends BaseConfig
217229
* Whether to destroy session data associated with the old session ID
218230
* when auto-regenerating the session ID. When set to FALSE, the data
219231
* will be later deleted by the garbage collector.
232+
*
233+
* @deprecated use Config\Session::$regenerateDestroy instead.
220234
*/
221235
public bool $sessionRegenerateDestroy = false;
222236

237+
/**
238+
* --------------------------------------------------------------------------
239+
* Session Database Group
240+
* --------------------------------------------------------------------------
241+
*
242+
* DB Group for the database session.
243+
*
244+
* @deprecated use Config\Session::$DBGroup instead.
245+
*/
246+
public ?string $sessionDBGroup = null;
247+
223248
/**
224249
* --------------------------------------------------------------------------
225250
* Cookie Prefix

app/Config/Session.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
use CodeIgniter\Session\Handlers\FileHandler;
7+
use SessionHandlerInterface;
8+
9+
class Session extends BaseConfig
10+
{
11+
/**
12+
* --------------------------------------------------------------------------
13+
* Session Driver
14+
* --------------------------------------------------------------------------
15+
*
16+
* The session storage driver to use:
17+
* - `CodeIgniter\Session\Handlers\FileHandler`
18+
* - `CodeIgniter\Session\Handlers\DatabaseHandler`
19+
* - `CodeIgniter\Session\Handlers\MemcachedHandler`
20+
* - `CodeIgniter\Session\Handlers\RedisHandler`
21+
*
22+
* @phpstan-var class-string<SessionHandlerInterface>
23+
*/
24+
public string $driver = FileHandler::class;
25+
26+
/**
27+
* --------------------------------------------------------------------------
28+
* Session Cookie Name
29+
* --------------------------------------------------------------------------
30+
*
31+
* The session cookie name, must contain only [0-9a-z_-] characters
32+
*/
33+
public string $cookieName = 'ci_session';
34+
35+
/**
36+
* --------------------------------------------------------------------------
37+
* Session Expiration
38+
* --------------------------------------------------------------------------
39+
*
40+
* The number of SECONDS you want the session to last.
41+
* Setting to 0 (zero) means expire when the browser is closed.
42+
*/
43+
public int $expiration = 7200;
44+
45+
/**
46+
* --------------------------------------------------------------------------
47+
* Session Save Path
48+
* --------------------------------------------------------------------------
49+
*
50+
* The location to save sessions to and is driver dependent.
51+
*
52+
* For the 'files' driver, it's a path to a writable directory.
53+
* WARNING: Only absolute paths are supported!
54+
*
55+
* For the 'database' driver, it's a table name.
56+
* Please read up the manual for the format with other session drivers.
57+
*
58+
* IMPORTANT: You are REQUIRED to set a valid save path!
59+
*/
60+
public string $savePath = WRITEPATH . 'session';
61+
62+
/**
63+
* --------------------------------------------------------------------------
64+
* Session Match IP
65+
* --------------------------------------------------------------------------
66+
*
67+
* Whether to match the user's IP address when reading the session data.
68+
*
69+
* WARNING: If you're using the database driver, don't forget to update
70+
* your session table's PRIMARY KEY when changing this setting.
71+
*/
72+
public bool $matchIP = false;
73+
74+
/**
75+
* --------------------------------------------------------------------------
76+
* Session Time to Update
77+
* --------------------------------------------------------------------------
78+
*
79+
* How many seconds between CI regenerating the session ID.
80+
*/
81+
public int $timeToUpdate = 300;
82+
83+
/**
84+
* --------------------------------------------------------------------------
85+
* Session Regenerate Destroy
86+
* --------------------------------------------------------------------------
87+
*
88+
* Whether to destroy session data associated with the old session ID
89+
* when auto-regenerating the session ID. When set to FALSE, the data
90+
* will be later deleted by the garbage collector.
91+
*/
92+
public bool $regenerateDestroy = false;
93+
94+
/**
95+
* --------------------------------------------------------------------------
96+
* Session Database Group
97+
* --------------------------------------------------------------------------
98+
*
99+
* DB Group for the database session.
100+
*/
101+
public ?string $DBGroup = null;
102+
}

env

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@
2424
# If you have trouble with `.`, you could also use `_`.
2525
# app_baseURL = ''
2626
# app.forceGlobalSecureRequests = false
27-
28-
# app.sessionDriver = 'CodeIgniter\Session\Handlers\FileHandler'
29-
# app.sessionCookieName = 'ci_session'
30-
# app.sessionExpiration = 7200
31-
# app.sessionSavePath = null
32-
# app.sessionMatchIP = false
33-
# app.sessionTimeToUpdate = 300
34-
# app.sessionRegenerateDestroy = false
35-
3627
# app.CSPEnabled = false
3728

3829
#--------------------------------------------------------------------
@@ -127,6 +118,18 @@
127118
# security.redirect = false
128119
# security.samesite = 'Lax'
129120

121+
#--------------------------------------------------------------------
122+
# SESSION
123+
#--------------------------------------------------------------------
124+
125+
# session.driver = 'CodeIgniter\Session\Handlers\FileHandler'
126+
# session.cookieName = 'ci_session'
127+
# session.expiration = 7200
128+
# session.savePath = null
129+
# session.matchIP = false
130+
# session.timeToUpdate = 300
131+
# session.regenerateDestroy = false
132+
130133
#--------------------------------------------------------------------
131134
# LOGGER
132135
#--------------------------------------------------------------------

system/Commands/Generators/MigrationGenerator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use CodeIgniter\CLI\BaseCommand;
1515
use CodeIgniter\CLI\CLI;
1616
use CodeIgniter\CLI\GeneratorTrait;
17+
use Config\App as AppConfig;
18+
use Config\Session as SessionConfig;
1719

1820
/**
1921
* Generates a skeleton migration file.
@@ -105,7 +107,14 @@ protected function prepare(string $class): string
105107
$data['table'] = is_string($table) ? $table : 'ci_sessions';
106108
$data['DBGroup'] = is_string($DBGroup) ? $DBGroup : 'default';
107109
$data['DBDriver'] = config('Database')->{$data['DBGroup']}['DBDriver'];
108-
$data['matchIP'] = config('App')->sessionMatchIP;
110+
111+
/** @var AppConfig $config */
112+
$config = config('App');
113+
/** @var SessionConfig|null $session */
114+
$session = config('Session');
115+
116+
$data['matchIP'] = ($session instanceof SessionConfig)
117+
? $session->matchIP : $config->sessionMatchIP;
109118
}
110119

111120
return $this->parseTemplate($class, [], [], $data);

system/Session/Handlers/BaseHandler.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Config\App as AppConfig;
1515
use Config\Cookie as CookieConfig;
16+
use Config\Session as SessionConfig;
1617
use Psr\Log\LoggerAwareTrait;
1718
use SessionHandlerInterface;
1819

@@ -106,6 +107,21 @@ abstract class BaseHandler implements SessionHandlerInterface
106107

107108
public function __construct(AppConfig $config, string $ipAddress)
108109
{
110+
/** @var SessionConfig|null $session */
111+
$session = config('Session');
112+
113+
// Store Session configurations
114+
if ($session instanceof SessionConfig) {
115+
$this->cookieName = $session->cookieName;
116+
$this->matchIP = $session->matchIP;
117+
$this->savePath = $session->savePath;
118+
} else {
119+
// `Config/Session.php` is absence
120+
$this->cookieName = $config->sessionCookieName;
121+
$this->matchIP = $config->sessionMatchIP;
122+
$this->savePath = $config->sessionSavePath;
123+
}
124+
109125
/** @var CookieConfig|null $cookie */
110126
$cookie = config('Cookie');
111127

@@ -123,10 +139,7 @@ public function __construct(AppConfig $config, string $ipAddress)
123139
$this->cookieSecure = $config->cookieSecure;
124140
}
125141

126-
$this->cookieName = $config->sessionCookieName;
127-
$this->matchIP = $config->sessionMatchIP;
128-
$this->savePath = $config->sessionSavePath;
129-
$this->ipAddress = $ipAddress;
142+
$this->ipAddress = $ipAddress;
130143
}
131144

132145
/**

system/Session/Handlers/DatabaseHandler.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Session\Exceptions\SessionException;
1717
use Config\App as AppConfig;
1818
use Config\Database;
19+
use Config\Session as SessionConfig;
1920
use ReturnTypeWillChange;
2021

2122
/**
@@ -66,16 +67,24 @@ class DatabaseHandler extends BaseHandler
6667
public function __construct(AppConfig $config, string $ipAddress)
6768
{
6869
parent::__construct($config, $ipAddress);
69-
$this->table = $config->sessionSavePath;
7070

71+
/** @var SessionConfig|null $session */
72+
$session = config('Session');
73+
74+
// Store Session configurations
75+
if ($session instanceof SessionConfig) {
76+
$this->DBGroup = $session->DBGroup ?? config(Database::class)->defaultGroup;
77+
} else {
78+
// `Config/Session.php` is absence
79+
$this->DBGroup = $config->sessionDBGroup ?? config(Database::class)->defaultGroup;
80+
}
81+
82+
$this->table = $this->savePath;
7183
if (empty($this->table)) {
7284
throw SessionException::forMissingDatabaseTable();
7385
}
7486

75-
$this->DBGroup = $config->sessionDBGroup ?? config(Database::class)->defaultGroup;
76-
77-
$this->db = Database::connect($this->DBGroup);
78-
87+
$this->db = Database::connect($this->DBGroup);
7988
$this->platform = $this->db->getPlatform();
8089
}
8190

system/Session/Handlers/FileHandler.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public function __construct(AppConfig $config, string $ipAddress)
6767
{
6868
parent::__construct($config, $ipAddress);
6969

70-
if (! empty($config->sessionSavePath)) {
71-
$this->savePath = rtrim($config->sessionSavePath, '/\\');
72-
ini_set('session.save_path', $config->sessionSavePath);
70+
if (! empty($this->savePath)) {
71+
$this->savePath = rtrim($this->savePath, '/\\');
72+
ini_set('session.save_path', $this->savePath);
7373
} else {
7474
$sessionPath = rtrim(ini_get('session.save_path'), '/\\');
7575

@@ -80,8 +80,6 @@ public function __construct(AppConfig $config, string $ipAddress)
8080
$this->savePath = $sessionPath;
8181
}
8282

83-
$this->matchIP = $config->sessionMatchIP;
84-
8583
$this->configureSessionIDRegex();
8684
}
8785

system/Session/Handlers/MemcachedHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\I18n\Time;
1515
use CodeIgniter\Session\Exceptions\SessionException;
1616
use Config\App as AppConfig;
17+
use Config\Session as SessionConfig;
1718
use Memcached;
1819
use ReturnTypeWillChange;
1920

@@ -57,6 +58,12 @@ public function __construct(AppConfig $config, string $ipAddress)
5758
{
5859
parent::__construct($config, $ipAddress);
5960

61+
/** @var SessionConfig|null $session */
62+
$session = config('Session');
63+
64+
$this->sessionExpiration = ($session instanceof SessionConfig)
65+
? $session->expiration : $config->sessionExpiration;
66+
6067
if (empty($this->savePath)) {
6168
throw SessionException::forEmptySavepath();
6269
}
@@ -68,8 +75,6 @@ public function __construct(AppConfig $config, string $ipAddress)
6875
if (! empty($this->keyPrefix)) {
6976
ini_set('memcached.sess_prefix', $this->keyPrefix);
7077
}
71-
72-
$this->sessionExpiration = $config->sessionExpiration;
7378
}
7479

7580
/**

0 commit comments

Comments
 (0)