rename BaseRequestContext class to RequestContext

This commit is contained in:
Spine
2025-04-03 05:31:38 +00:00
parent 6b8f1ca5c1
commit b34f838f79
15 changed files with 23 additions and 23 deletions

View File

@@ -7,7 +7,7 @@ abstract class Base {
protected static DB\Mysql $db;
protected static Cache $cache;
protected static BaseRequestContext $requestContext;
protected static RequestContext $requestContext;
protected static \Twig\Environment $twig;
public static function initialize(Cache $cache, DB\Mysql $db, \Twig\Environment $twig): void {
@@ -16,15 +16,15 @@ abstract class Base {
static::$twig = $twig;
}
public static function setRequestContext(BaseRequestContext $c): void {
public static function setRequestContext(RequestContext $c): void {
static::$requestContext = $c;
}
public static function staticRequestContext(): BaseRequestContext {
public static function staticRequestContext(): RequestContext {
return static::$requestContext;
}
public function requestContext(): BaseRequestContext {
public function requestContext(): RequestContext {
return static::$requestContext;
}

View File

@@ -2,7 +2,7 @@
namespace Gazelle;
class BaseRequestContext {
class RequestContext {
protected string $module; // previously known as global $Document
protected bool $isValid;
protected array $ua;

View File

@@ -27,7 +27,7 @@ if ($remove) {
define('BORIS', 1);
$_SERVER['HTTP_USER_AGENT'] = 'boris';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
Base::setRequestContext(new BaseRequestContext(
Base::setRequestContext(new RequestContext(
'boris', $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']
));

View File

@@ -4,6 +4,6 @@
require_once(__DIR__ . '/../lib/bootstrap.php');
Gazelle\Base::setRequestContext(
new Gazelle\BaseRequestContext('cli', '127.0.0.1', 'scheduler')
new Gazelle\RequestContext('cli', '127.0.0.1', 'scheduler')
);
(new Gazelle\TaskScheduler)->run();

View File

@@ -20,7 +20,7 @@ if (
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$context = new BaseRequestContext(
$context = new RequestContext(
$_SERVER['SCRIPT_NAME'],
$_SERVER['REMOTE_ADDR'],
$_SERVER['HTTP_USER_AGENT'] ?? '[no-useragent]',
@@ -130,7 +130,7 @@ if ($Viewer) {
}
if ($Viewer->ipaddr() != $context->remoteAddr() && !$Viewer->permitted('site_disable_ip_history')) {
if ($ipv4Man->isBanned($context->remoteAddr())) {
error('Your IP address has been banned.');
Error403::error('Your IP address has been banned.');
}
$ipv4Man->register($Viewer, $context->remoteAddr());
}

View File

@@ -28,7 +28,7 @@ $Twig = Gazelle\Util\Twig::factory(new Gazelle\Manager\User());
Gazelle\Base::initialize($Cache, Gazelle\DB::DB(), $Twig);
Gazelle\Base::setRequestContext(new Gazelle\BaseRequestContext(
Gazelle\Base::setRequestContext(new Gazelle\RequestContext(
scriptName: 'none',
remoteAddr: '127.0.0.1',
useragent: 'none',

View File

@@ -29,7 +29,7 @@ if (
) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$context = new Gazelle\BaseRequestContext(
$context = new Gazelle\RequestContext(
$_SERVER['SCRIPT_NAME'],
$_SERVER['REMOTE_ADDR'],
$_SERVER['HTTP_USER_AGENT'] ?? '[no-useragent]',

View File

@@ -46,7 +46,7 @@ if (!empty($_POST['username']) && !empty($_POST['password'])) {
exit;
}
$useragent = $_SERVER['HTTP_USER_AGENT'] ?? '[no-useragent]';
$context = new BaseRequestContext(
$context = new RequestContext(
$_SERVER['SCRIPT_NAME'],
$_SERVER['REMOTE_ADDR'],
$useragent,

View File

@@ -547,7 +547,7 @@ class ForumTest extends TestCase {
description: 'This is where it renders',
);
$paginator = (new Util\Paginator(TOPICS_PER_PAGE, 1))->setTotal(1);
Base::setRequestContext(new BaseRequestContext('/forum.php', '127.0.0.1', ''));
Base::setRequestContext(new RequestContext('/forum.php', '127.0.0.1', ''));
global $SessionID; // to render header()
$SessionID = 'phpunit';
Util\Twig::setViewer($admin);

View File

@@ -59,7 +59,7 @@ class FriendTest extends TestCase {
'ipaddr' => '127.0.0.1',
'useragent' => 'phpunit-browser',
]);
Base::setRequestContext(new BaseRequestContext('/friends.php', '127.0.0.1', ''));
Base::setRequestContext(new RequestContext('/friends.php', '127.0.0.1', ''));
global $SessionID;
$SessionID = $current['SessionID'];

View File

@@ -138,7 +138,7 @@ class LogTest extends TestCase {
$page = $siteLog->page($paginator->page(), $paginator->offset(), '');
$paginator->setTotal($siteLog->total(''));
Base::setRequestContext(new BaseRequestContext('/index.php', '127.0.0.1', ''));
Base::setRequestContext(new RequestContext('/index.php', '127.0.0.1', ''));
global $SessionID;
$SessionID = 'phpunit';
Util\Twig::setViewer($this->user);

View File

@@ -4,7 +4,7 @@ namespace Gazelle;
use PHPUnit\Framework\TestCase;
class BaseRequestContextTest extends TestCase {
class RequestContextTest extends TestCase {
protected User $user;
public function tearDown(): void {
@@ -13,8 +13,8 @@ class BaseRequestContextTest extends TestCase {
}
}
public function testBaseRequestContext(): void {
$context = new BaseRequestContext(
public function testRequestContext(): void {
$context = new RequestContext(
'/phpunit.php',
'224.0.0.1',
'Lidarr/3.5.8 (windows 98)',
@@ -38,7 +38,7 @@ class BaseRequestContextTest extends TestCase {
}
public function testBadRequest(): void {
$context = new BaseRequestContext('', '', '');
$context = new RequestContext('', '', '');
$this->assertFalse($context->isValid(), 'context-not-valid');
$this->assertEquals('', $context->browser(), 'context-invalid-browser');
}
@@ -46,7 +46,7 @@ class BaseRequestContextTest extends TestCase {
// Any object that derives from Base has access to the request context
public function testObject(): void {
Base::setRequestContext(
new BaseRequestContext(
new RequestContext(
'/phpunit.php',
'225.0.0.1',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.3',

View File

@@ -20,7 +20,7 @@ class UploadTest extends TestCase {
$this->assertStringContainsString($this->user->auth(), $upload->head(0), 'upload-head');
Base::setRequestContext(new BaseRequestContext('/upload.php', '127.0.0.1', ''));
Base::setRequestContext(new RequestContext('/upload.php', '127.0.0.1', ''));
global $SessionID;
$SessionID = '';
Util\Twig::setViewer($this->user);

View File

@@ -380,7 +380,7 @@ class UserTest extends TestCase {
public function testLogin(): void {
$_SERVER['REMOTE_ADDR'] = '127.0.0.255';
Base::setRequestContext(new BaseRequestContext(
Base::setRequestContext(new RequestContext(
'phpunit', $_SERVER['REMOTE_ADDR'], 'whatever'
));
$login = new Login();

View File

@@ -227,7 +227,7 @@ END;
}
public function testFunction(): void {
Base::setRequestContext(new BaseRequestContext('/index.php', '127.0.0.1', ''));
Base::setRequestContext(new RequestContext('/index.php', '127.0.0.1', ''));
Util\Twig::setViewer($this->user);
$this->assertStringStartsWith('<!DOCTYPE html>', self::twig('{{ header("page") }}')->render(), 'twig-function-header');