Skip to content

Remove Config\App session items #7000

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,6 @@ parameters:
count: 1
path: system/Session/Handlers/RedisHandler.php

-
message: "#^Property CodeIgniter\\\\Session\\\\Session\\:\\:\\$sessionExpiration \\(int\\) in isset\\(\\) is not nullable\\.$#"
count: 1
path: system/Session/Session.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 1
Expand Down
8 changes: 2 additions & 6 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 */
/** @var SessionConfig $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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a pretty major change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and no. Almost no one writes their own session handlers, so in this particular case, the impact would be minimal. Although, I haven't thought of any other possible problems yet.

{
/** @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