Skip to content

Commit

Permalink
fix: remove Session config items in Config\App
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 22, 2022
1 parent 13d8246 commit 6e9fd1a
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 297 deletions.
109 changes: 0 additions & 109 deletions app/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Config;

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

class App extends BaseConfig
{
Expand Down Expand Up @@ -137,114 +136,6 @@ class App extends BaseConfig
*/
public bool $forceGlobalSecureRequests = false;

/**
* --------------------------------------------------------------------------
* Session Driver
* --------------------------------------------------------------------------
*
* The session storage driver to use:
* - `CodeIgniter\Session\Handlers\FileHandler`
* - `CodeIgniter\Session\Handlers\DatabaseHandler`
* - `CodeIgniter\Session\Handlers\MemcachedHandler`
* - `CodeIgniter\Session\Handlers\RedisHandler`
*
* @var string
*
* @deprecated use Config\Session::$driver instead.
*/
public $sessionDriver = FileHandler::class;

/**
* --------------------------------------------------------------------------
* Session Cookie Name
* --------------------------------------------------------------------------
*
* The session cookie name, must contain only [0-9a-z_-] characters
*
* @deprecated use Config\Session::$cookieName instead.
*/
public string $sessionCookieName = '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.
*
* @deprecated use Config\Session::$expiration instead.
*/
public int $sessionExpiration = 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!
*
* @deprecated use Config\Session::$savePath instead.
*/
public string $sessionSavePath = 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.
*
* @deprecated use Config\Session::$matchIP instead.
*/
public bool $sessionMatchIP = false;

/**
* --------------------------------------------------------------------------
* Session Time to Update
* --------------------------------------------------------------------------
*
* How many seconds between CI regenerating the session ID.
*
* @deprecated use Config\Session::$timeToUpdate instead.
*/
public int $sessionTimeToUpdate = 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.
*
* @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
6 changes: 1 addition & 5 deletions system/Commands/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\GeneratorTrait;
use Config\App as AppConfig;
use Config\Session as SessionConfig;

/**
Expand Down Expand Up @@ -108,13 +107,10 @@ protected function prepare(string $class): string
$data['DBGroup'] = is_string($DBGroup) ? $DBGroup : 'default';
$data['DBDriver'] = config('Database')->{$data['DBGroup']}['DBDriver'];

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

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

return $this->parseTemplate($class, [], [], $data);
Expand Down
17 changes: 8 additions & 9 deletions system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,25 +634,23 @@ public static function security(?App $config = null, bool $getShared = true)
* Return the session manager.
*
* @return Session
*
* @TODO replace the first parameter type `?App` with `?SessionConfig`
*/
public static function session(?App $config = null, bool $getShared = true)
{
if ($getShared) {
return static::getSharedInstance('session', $config);
}

$config ??= config('App');
assert($config instanceof App);

$logger = AppServices::logger();

/** @var SessionConfig|null $sessionConfig */
$sessionConfig = config('Session');
/** @var SessionConfig $config */
$config = config('Session');
assert($config instanceof SessionConfig, 'Missing "Config/Session.php".');

$driverName = $sessionConfig->driver ?? $config->sessionDriver;
$driverName = $config->driver;

if ($driverName === DatabaseHandler::class) {
$DBGroup = $sessionConfig->DBGroup ?? $config->sessionDBGroup ?? config(Database::class)->defaultGroup;
$DBGroup = $config->DBGroup ?? config(Database::class)->defaultGroup;
$db = Database::connect($DBGroup);

$driver = $db->getPlatform();
Expand All @@ -664,6 +662,7 @@ public static function session(?App $config = null, bool $getShared = true)
}
}

$logger = AppServices::logger();
$driver = new $driverName($config, AppServices::request()->getIPAddress());
$driver->setLogger($logger);

Expand Down
29 changes: 14 additions & 15 deletions system/Session/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,19 @@ abstract class BaseHandler implements SessionHandlerInterface
*/
protected $ipAddress;

public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $session, 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;
}
$this->cookieName = $session->cookieName;
$this->matchIP = $session->matchIP;
$this->savePath = $session->savePath;

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

/** @var AppConfig $config */
$config = config('App');

if ($cookie instanceof CookieConfig) {
// Session cookies have no prefix.
$this->cookieDomain = $cookie->domain;
Expand All @@ -151,7 +144,13 @@ protected function destroyCookie(): bool
return setcookie(
$this->cookieName,
'',
['expires' => 1, 'path' => $this->cookiePath, 'domain' => $this->cookieDomain, 'secure' => $this->cookieSecure, 'httponly' => true]
[
'expires' => 1,
'path' => $this->cookiePath,
'domain' => $this->cookieDomain,
'secure' => $this->cookieSecure,
'httponly' => true,
]
);
}

Expand Down
21 changes: 5 additions & 16 deletions system/Session/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Database;
use Config\Session as SessionConfig;
use ReturnTypeWillChange;
Expand Down Expand Up @@ -69,24 +68,14 @@ class DatabaseHandler extends BaseHandler
/**
* @throws SessionException
*/
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $session, string $ipAddress)
{
parent::__construct($config, $ipAddress);

/** @var SessionConfig|null $session */
$session = config('Session');
parent::__construct($session, $ipAddress);

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

$this->table = $this->savePath;
if (empty($this->table)) {
Expand Down
6 changes: 3 additions & 3 deletions system/Session/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use CodeIgniter\I18n\Time;
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Session as SessionConfig;
use ReturnTypeWillChange;

/**
Expand Down Expand Up @@ -63,9 +63,9 @@ class FileHandler extends BaseHandler
*/
protected $sessionIDRegex = '';

public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $session, string $ipAddress)
{
parent::__construct($config, $ipAddress);
parent::__construct($session, $ipAddress);

if (! empty($this->savePath)) {
$this->savePath = rtrim($this->savePath, '/\\');
Expand Down
14 changes: 4 additions & 10 deletions system/Session/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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 @@ -54,23 +53,18 @@ class MemcachedHandler extends BaseHandler
/**
* @throws SessionException
*/
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $session, string $ipAddress)
{
parent::__construct($config, $ipAddress);
parent::__construct($session, $ipAddress);

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

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

if (empty($this->savePath)) {
throw SessionException::forEmptySavepath();
}

// Add sessionCookieName for multiple session cookies.
$this->keyPrefix .= ($session instanceof SessionConfig)
? $session->cookieName : $config->sessionCookieName . ':';
$this->keyPrefix .= $session->cookieName . ':';

if ($this->matchIP === true) {
$this->keyPrefix .= $this->ipAddress . ':';
Expand Down
27 changes: 7 additions & 20 deletions system/Session/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use CodeIgniter\I18n\Time;
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Session as SessionConfig;
use Redis;
use RedisException;
Expand Down Expand Up @@ -66,28 +65,16 @@ class RedisHandler extends BaseHandler
*
* @throws SessionException
*/
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $session, string $ipAddress)
{
parent::__construct($config, $ipAddress);

/** @var SessionConfig|null $session */
$session = config('Session');
parent::__construct($session, $ipAddress);

// Store Session configurations
if ($session instanceof SessionConfig) {
$this->sessionExpiration = empty($session->expiration)
? (int) ini_get('session.gc_maxlifetime')
: (int) $session->expiration;
// Add sessionCookieName for multiple session cookies.
$this->keyPrefix .= $session->cookieName . ':';
} else {
// `Config/Session.php` is absence
$this->sessionExpiration = empty($config->sessionExpiration)
? (int) ini_get('session.gc_maxlifetime')
: (int) $config->sessionExpiration;
// Add sessionCookieName for multiple session cookies.
$this->keyPrefix .= $config->sessionCookieName . ':';
}
// Add sessionCookieName for multiple session cookies.
$this->keyPrefix .= $session->cookieName . ':';
$this->sessionExpiration = empty($session->expiration)
? (int) ini_get('session.gc_maxlifetime')
: (int) $session->expiration;

$this->setSavePath();

Expand Down
Loading

0 comments on commit 6e9fd1a

Please sign in to comment.