Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add Config\Session #6989

Merged
merged 14 commits into from
Dec 20, 2022
25 changes: 25 additions & 0 deletions app/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class App extends BaseConfig
* - `CodeIgniter\Session\Handlers\RedisHandler`
*
* @var string
*
* @deprecated use Config\Session::$driver instead.
*/
public $sessionDriver = FileHandler::class;

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

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

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

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

Expand All @@ -206,6 +216,8 @@ class App extends BaseConfig
* --------------------------------------------------------------------------
*
* How many seconds between CI regenerating the session ID.
*
* @deprecated use Config\Session::$timeToUpdate instead.
*/
public int $sessionTimeToUpdate = 300;

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

/**
* --------------------------------------------------------------------------
* Session Database Group
* --------------------------------------------------------------------------
*
* DB Group for the database session.
*
* @deprecated use Config\Session::$DBGroup instead.
*/
public ?string $sessionDBGroup = null;

/**
* --------------------------------------------------------------------------
* Cookie Prefix
Expand Down
102 changes: 102 additions & 0 deletions app/Config/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Session\Handlers\FileHandler;
use SessionHandlerInterface;

class Session extends BaseConfig
{
/**
* --------------------------------------------------------------------------
* Session Driver
* --------------------------------------------------------------------------
*
* The session storage driver to use:
* - `CodeIgniter\Session\Handlers\FileHandler`
* - `CodeIgniter\Session\Handlers\DatabaseHandler`
* - `CodeIgniter\Session\Handlers\MemcachedHandler`
* - `CodeIgniter\Session\Handlers\RedisHandler`
*
* @phpstan-var class-string<SessionHandlerInterface>
*/
public string $driver = FileHandler::class;

/**
* --------------------------------------------------------------------------
* Session Cookie Name
* --------------------------------------------------------------------------
*
* The session cookie name, must contain only [0-9a-z_-] characters
*/
public string $cookieName = 'ci_session';

/**
* --------------------------------------------------------------------------
* Session Expiration
* --------------------------------------------------------------------------
*
* The number of SECONDS you want the session to last.
* Setting to 0 (zero) means expire when the browser is closed.
*/
public int $expiration = 7200;

/**
* --------------------------------------------------------------------------
* Session Save Path
* --------------------------------------------------------------------------
*
* The location to save sessions to and is driver dependent.
*
* For the 'files' driver, it's a path to a writable directory.
* WARNING: Only absolute paths are supported!
*
* For the 'database' driver, it's a table name.
* Please read up the manual for the format with other session drivers.
*
* IMPORTANT: You are REQUIRED to set a valid save path!
*/
public string $savePath = WRITEPATH . 'session';

/**
* --------------------------------------------------------------------------
* Session Match IP
* --------------------------------------------------------------------------
*
* Whether to match the user's IP address when reading the session data.
*
* WARNING: If you're using the database driver, don't forget to update
* your session table's PRIMARY KEY when changing this setting.
*/
public bool $matchIP = false;

/**
* --------------------------------------------------------------------------
* Session Time to Update
* --------------------------------------------------------------------------
*
* How many seconds between CI regenerating the session ID.
*/
public int $timeToUpdate = 300;

/**
* --------------------------------------------------------------------------
* Session Regenerate Destroy
* --------------------------------------------------------------------------
*
* Whether to destroy session data associated with the old session ID
* when auto-regenerating the session ID. When set to FALSE, the data
* will be later deleted by the garbage collector.
*/
public bool $regenerateDestroy = false;

/**
* --------------------------------------------------------------------------
* Session Database Group
* --------------------------------------------------------------------------
*
* DB Group for the database session.
*/
public ?string $DBGroup = null;
}
21 changes: 12 additions & 9 deletions env
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@
# If you have trouble with `.`, you could also use `_`.
# app_baseURL = ''
# app.forceGlobalSecureRequests = false

# app.sessionDriver = 'CodeIgniter\Session\Handlers\FileHandler'
# app.sessionCookieName = 'ci_session'
# app.sessionExpiration = 7200
# app.sessionSavePath = null
# app.sessionMatchIP = false
# app.sessionTimeToUpdate = 300
# app.sessionRegenerateDestroy = false

# app.CSPEnabled = false

#--------------------------------------------------------------------
Expand Down Expand Up @@ -127,6 +118,18 @@
# security.redirect = false
# security.samesite = 'Lax'

#--------------------------------------------------------------------
# SESSION
#--------------------------------------------------------------------

# session.driver = 'CodeIgniter\Session\Handlers\FileHandler'
# session.cookieName = 'ci_session'
# session.expiration = 7200
# session.savePath = null
# session.matchIP = false
# session.timeToUpdate = 300
# session.regenerateDestroy = false

#--------------------------------------------------------------------
# LOGGER
#--------------------------------------------------------------------
Expand Down
11 changes: 10 additions & 1 deletion system/Commands/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\GeneratorTrait;
use Config\App as AppConfig;
use Config\Session as SessionConfig;

/**
* Generates a skeleton migration file.
Expand Down Expand Up @@ -105,7 +107,14 @@ protected function prepare(string $class): string
$data['table'] = is_string($table) ? $table : 'ci_sessions';
$data['DBGroup'] = is_string($DBGroup) ? $DBGroup : 'default';
$data['DBDriver'] = config('Database')->{$data['DBGroup']}['DBDriver'];
$data['matchIP'] = config('App')->sessionMatchIP;

/** @var AppConfig $config */
$config = config('App');
/** @var SessionConfig|null $session */
$session = config('Session');

$data['matchIP'] = ($session instanceof SessionConfig)
? $session->matchIP : $config->sessionMatchIP;
}

return $this->parseTemplate($class, [], [], $data);
Expand Down
21 changes: 17 additions & 4 deletions system/Session/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Config\App as AppConfig;
use Config\Cookie as CookieConfig;
use Config\Session as SessionConfig;
use Psr\Log\LoggerAwareTrait;
use SessionHandlerInterface;

Expand Down Expand Up @@ -106,6 +107,21 @@ abstract class BaseHandler implements SessionHandlerInterface

public function __construct(AppConfig $config, string $ipAddress)
{
/** @var SessionConfig|null $session */
$session = config('Session');

// Store Session configurations
if ($session instanceof SessionConfig) {
$this->cookieName = $session->cookieName;
$this->matchIP = $session->matchIP;
$this->savePath = $session->savePath;
} else {
// `Config/Session.php` is absence
$this->cookieName = $config->sessionCookieName;
$this->matchIP = $config->sessionMatchIP;
$this->savePath = $config->sessionSavePath;
}

/** @var CookieConfig|null $cookie */
$cookie = config('Cookie');

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

$this->cookieName = $config->sessionCookieName;
$this->matchIP = $config->sessionMatchIP;
$this->savePath = $config->sessionSavePath;
$this->ipAddress = $ipAddress;
$this->ipAddress = $ipAddress;
}

/**
Expand Down
19 changes: 14 additions & 5 deletions system/Session/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Database;
use Config\Session as SessionConfig;
use ReturnTypeWillChange;

/**
Expand Down Expand Up @@ -66,16 +67,24 @@ class DatabaseHandler extends BaseHandler
public function __construct(AppConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);
$this->table = $config->sessionSavePath;

/** @var SessionConfig|null $session */
$session = config('Session');

// Store Session configurations
if ($session instanceof SessionConfig) {
$this->DBGroup = $session->DBGroup ?? config(Database::class)->defaultGroup;
} else {
// `Config/Session.php` is absence
$this->DBGroup = $config->sessionDBGroup ?? config(Database::class)->defaultGroup;
}

$this->table = $this->savePath;
if (empty($this->table)) {
throw SessionException::forMissingDatabaseTable();
}

$this->DBGroup = $config->sessionDBGroup ?? config(Database::class)->defaultGroup;

$this->db = Database::connect($this->DBGroup);

$this->db = Database::connect($this->DBGroup);
$this->platform = $this->db->getPlatform();
}

Expand Down
8 changes: 3 additions & 5 deletions system/Session/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function __construct(AppConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);

if (! empty($config->sessionSavePath)) {
$this->savePath = rtrim($config->sessionSavePath, '/\\');
ini_set('session.save_path', $config->sessionSavePath);
if (! empty($this->savePath)) {
MGatner marked this conversation as resolved.
Show resolved Hide resolved
$this->savePath = rtrim($this->savePath, '/\\');
ini_set('session.save_path', $this->savePath);
} else {
$sessionPath = rtrim(ini_get('session.save_path'), '/\\');

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

$this->matchIP = $config->sessionMatchIP;

$this->configureSessionIDRegex();
}

Expand Down
9 changes: 7 additions & 2 deletions system/Session/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\I18n\Time;
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Session as SessionConfig;
use Memcached;
use ReturnTypeWillChange;

Expand Down Expand Up @@ -57,6 +58,12 @@ public function __construct(AppConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);

/** @var SessionConfig|null $session */
$session = config('Session');

$this->sessionExpiration = ($session instanceof SessionConfig)
? $session->expiration : $config->sessionExpiration;

if (empty($this->savePath)) {
throw SessionException::forEmptySavepath();
}
Expand All @@ -68,8 +75,6 @@ public function __construct(AppConfig $config, string $ipAddress)
if (! empty($this->keyPrefix)) {
ini_set('memcached.sess_prefix', $this->keyPrefix);
}

$this->sessionExpiration = $config->sessionExpiration;
}

/**
Expand Down
Loading