fix: remove Config\App from constructors in Session and Session Handlers

This commit is contained in:
kenjis 2023-02-14 20:04:09 +09:00
parent 49be3f1d23
commit 75fbf5650f
No known key found for this signature in database
GPG Key ID: BD254878922AF198
12 changed files with 60 additions and 78 deletions

View File

@ -638,6 +638,8 @@ class Services extends BaseService
* 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)
{
@ -645,18 +647,16 @@ class Services extends BaseService
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;
$driverName = $config->driver;
if ($driverName === DatabaseHandler::class) {
$DBGroup = $sessionConfig->DBGroup ?? config(Database::class)->defaultGroup;
$DBGroup = $config->DBGroup ?? config(Database::class)->defaultGroup;
$db = Database::connect($DBGroup);
$driver = $db->getPlatform();

View File

@ -11,7 +11,6 @@
namespace CodeIgniter\Session\Handlers;
use Config\App as AppConfig;
use Config\Cookie as CookieConfig;
use Config\Session as SessionConfig;
use Psr\Log\LoggerAwareTrait;
@ -105,15 +104,12 @@ abstract class BaseHandler implements SessionHandlerInterface
*/
protected $ipAddress;
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $config, string $ipAddress)
{
/** @var SessionConfig|null $session */
$session = config('Session');
// Store Session configurations
$this->cookieName = $session->cookieName;
$this->matchIP = $session->matchIP;
$this->savePath = $session->savePath;
$this->cookieName = $config->cookieName;
$this->matchIP = $config->matchIP;
$this->savePath = $config->savePath;
/** @var CookieConfig $cookie */
$cookie = config('Cookie');

View File

@ -14,7 +14,6 @@ namespace CodeIgniter\Session\Handlers;
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;
@ -69,17 +68,14 @@ class DatabaseHandler extends BaseHandler
/**
* @throws SessionException
*/
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);
/** @var SessionConfig|null $session */
$session = config('Session');
// Store Session configurations
$this->DBGroup = $session->DBGroup ?? config(Database::class)->defaultGroup;
$this->DBGroup = $config->DBGroup ?? config(Database::class)->defaultGroup;
// Add sessionCookieName for multiple session cookies.
$this->idPrefix = $session->cookieName . ':';
$this->idPrefix = $config->cookieName . ':';
$this->table = $this->savePath;
if (empty($this->table)) {

View File

@ -13,7 +13,7 @@ namespace CodeIgniter\Session\Handlers;
use CodeIgniter\I18n\Time;
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Session as SessionConfig;
use ReturnTypeWillChange;
/**
@ -63,7 +63,7 @@ class FileHandler extends BaseHandler
*/
protected $sessionIDRegex = '';
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);

View File

@ -13,7 +13,6 @@ namespace CodeIgniter\Session\Handlers;
use CodeIgniter\I18n\Time;
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Session as SessionConfig;
use Memcached;
use ReturnTypeWillChange;
@ -54,21 +53,18 @@ class MemcachedHandler extends BaseHandler
/**
* @throws SessionException
*/
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);
/** @var SessionConfig $session */
$session = config('Session');
$this->sessionExpiration = $session->expiration;
$this->sessionExpiration = $config->expiration;
if (empty($this->savePath)) {
throw SessionException::forEmptySavepath();
}
// Add sessionCookieName for multiple session cookies.
$this->keyPrefix .= $session->cookieName . ':';
$this->keyPrefix .= $config->cookieName . ':';
if ($this->matchIP === true) {
$this->keyPrefix .= $this->ipAddress . ':';

View File

@ -13,7 +13,6 @@ namespace CodeIgniter\Session\Handlers;
use CodeIgniter\I18n\Time;
use CodeIgniter\Session\Exceptions\SessionException;
use Config\App as AppConfig;
use Config\Session as SessionConfig;
use Redis;
use RedisException;
@ -67,19 +66,16 @@ class RedisHandler extends BaseHandler
*
* @throws SessionException
*/
public function __construct(AppConfig $config, string $ipAddress)
public function __construct(SessionConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);
/** @var SessionConfig|null $session */
$session = config('Session');
// Store Session configurations
$this->sessionExpiration = empty($session->expiration)
$this->sessionExpiration = empty($config->expiration)
? (int) ini_get('session.gc_maxlifetime')
: (int) $session->expiration;
: (int) $config->expiration;
// Add sessionCookieName for multiple session cookies.
$this->keyPrefix .= $session->cookieName . ':';
$this->keyPrefix .= $config->cookieName . ':';
$this->setSavePath();

View File

@ -62,7 +62,7 @@ class Session implements SessionInterface
protected $sessionExpiration = 7200;
/**
* The location to save sessions to, driver dependent..
* The location to save sessions to, driver dependent.
*
* For the 'files' driver, it's a path to a writable directory.
* WARNING: Only absolute paths are supported!
@ -161,13 +161,10 @@ class Session implements SessionInterface
*
* Extract configuration settings and save them here.
*/
public function __construct(SessionHandlerInterface $driver, App $config)
public function __construct(SessionHandlerInterface $driver, SessionConfig $session)
{
$this->driver = $driver;
/** @var SessionConfig|null $session */
$session = config('Session');
// Store Session configurations
$this->sessionDriverName = $session->driver;
$this->sessionCookieName = $session->cookieName ?? $this->sessionCookieName;

View File

@ -333,7 +333,7 @@ abstract class CIUnitTestCase extends TestCase
{
$_SESSION = [];
$config = config('App');
$config = config('Session');
$session = new MockSession(new ArrayHandler($config, '0.0.0.0'), $config);
Services::injectMock('session', $session);

View File

@ -34,6 +34,7 @@ use Config\Logger;
use Config\Modules;
use Config\Routing;
use Config\Services;
use Config\Session as SessionConfig;
use Kint;
use RuntimeException;
use stdClass;
@ -517,20 +518,20 @@ final class CommonFunctionsTest extends CIUnitTestCase
protected function injectSessionMock()
{
$appConfig = new App();
$sessionConfig = new SessionConfig();
$defaults = [
'sessionDriver' => FileHandler::class,
'sessionCookieName' => 'ci_session',
'sessionExpiration' => 7200,
'sessionSavePath' => '',
'sessionMatchIP' => false,
'sessionTimeToUpdate' => 300,
'sessionRegenerateDestroy' => false,
'driver' => FileHandler::class,
'cookieName' => 'ci_session',
'expiration' => 7200,
'savePath' => '',
'matchIP' => false,
'timeToUpdate' => 300,
'regenerateDestroy' => false,
];
foreach ($defaults as $key => $config) {
$appConfig->{$key} = $config;
$sessionConfig->{$key} = $config;
}
$cookie = new Cookie();
@ -546,7 +547,7 @@ final class CommonFunctionsTest extends CIUnitTestCase
}
Factories::injectMock('config', 'Cookie', $cookie);
$session = new MockSession(new FileHandler($appConfig, '127.0.0.1'), $appConfig);
$session = new MockSession(new FileHandler($sessionConfig, '127.0.0.1'), $sessionConfig);
$session->setLogger(new TestLogger(new Logger()));
BaseService::injectMock('session', $session);
}

View File

@ -25,10 +25,10 @@ use CodeIgniter\Test\Mock\MockAppConfig;
use CodeIgniter\Test\Mock\MockSecurity;
use CodeIgniter\Test\Mock\MockSession;
use CodeIgniter\Test\TestLogger;
use Config\App as AppConfig;
use Config\Cookie;
use Config\Logger as LoggerConfig;
use Config\Security as SecurityConfig;
use Config\Session as SessionConfig;
/**
* @runTestsInSeparateProcesses
@ -69,20 +69,20 @@ final class SecurityCSRFSessionRandomizeTokenTest extends CIUnitTestCase
private function createSession($options = []): Session
{
$defaults = [
'sessionDriver' => FileHandler::class,
'sessionCookieName' => 'ci_session',
'sessionExpiration' => 7200,
'sessionSavePath' => '',
'sessionMatchIP' => false,
'sessionTimeToUpdate' => 300,
'sessionRegenerateDestroy' => false,
'driver' => FileHandler::class,
'cookieName' => 'ci_session',
'expiration' => 7200,
'savePath' => '',
'matchIP' => false,
'timeToUpdate' => 300,
'regenerateDestroy' => false,
];
$config = array_merge($defaults, $options);
$appConfig = new AppConfig();
$sessionConfig = new SessionConfig();
foreach ($config as $key => $c) {
$appConfig->{$key} = $c;
$sessionConfig->{$key} = $c;
}
$cookie = new Cookie();
@ -98,7 +98,7 @@ final class SecurityCSRFSessionRandomizeTokenTest extends CIUnitTestCase
}
Factories::injectMock('config', 'Cookie', $cookie);
$session = new MockSession(new ArrayHandler($appConfig, '127.0.0.1'), $appConfig);
$session = new MockSession(new ArrayHandler($sessionConfig, '127.0.0.1'), $sessionConfig);
$session->setLogger(new TestLogger(new LoggerConfig()));
return $session;

View File

@ -24,10 +24,10 @@ use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockAppConfig;
use CodeIgniter\Test\Mock\MockSession;
use CodeIgniter\Test\TestLogger;
use Config\App as AppConfig;
use Config\Cookie;
use Config\Logger as LoggerConfig;
use Config\Security as SecurityConfig;
use Config\Session as SessionConfig;
/**
* @runTestsInSeparateProcesses
@ -62,20 +62,20 @@ final class SecurityCSRFSessionTest extends CIUnitTestCase
private function createSession($options = []): Session
{
$defaults = [
'sessionDriver' => FileHandler::class,
'sessionCookieName' => 'ci_session',
'sessionExpiration' => 7200,
'sessionSavePath' => '',
'sessionMatchIP' => false,
'sessionTimeToUpdate' => 300,
'sessionRegenerateDestroy' => false,
'driver' => FileHandler::class,
'cookieName' => 'ci_session',
'expiration' => 7200,
'savePath' => '',
'matchIP' => false,
'timeToUpdate' => 300,
'regenerateDestroy' => false,
];
$config = array_merge($defaults, $options);
$appConfig = new AppConfig();
$sessionConfig = new SessionConfig();
foreach ($config as $key => $c) {
$appConfig->{$key} = $c;
$sessionConfig->{$key} = $c;
}
$cookie = new Cookie();
@ -91,7 +91,7 @@ final class SecurityCSRFSessionTest extends CIUnitTestCase
}
Factories::injectMock('config', 'Cookie', $cookie);
$session = new MockSession(new ArrayHandler($appConfig, '127.0.0.1'), $appConfig);
$session = new MockSession(new ArrayHandler($sessionConfig, '127.0.0.1'), $sessionConfig);
$session->setLogger(new TestLogger(new LoggerConfig()));
return $session;

View File

@ -62,7 +62,7 @@ final class SessionTest extends CIUnitTestCase
}
Factories::injectMock('config', 'Session', $sessionConfig);
$session = new MockSession(new FileHandler($appConfig, '127.0.0.1'), $appConfig);
$session = new MockSession(new FileHandler($sessionConfig, '127.0.0.1'), $sessionConfig);
$session->setLogger(new TestLogger(new LoggerConfig()));
return $session;