Skip to content
Merged
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
26 changes: 26 additions & 0 deletions src/http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,32 @@ public function getScriptUrl(): string
return parent::getScriptUrl();
}

/**
* Retrieves the server name for the current request, supporting PSR-7 and Yii2 fallback.
*
* Returns the server name as determined by the PSR-7 adapter if present.
*
* If no adapter is set, falls back to the parent implementation.
*
* This method enables seamless access to the server name in both PSR-7 and Yii2 environments, supporting
* interoperability with modern HTTP stacks and legacy workflows.
*
* @return string|null Server name for the current request, or null if not available.
*
* Usage example:
* ```php
* $serverName = $request->getServerName();
* ```
*/
public function getServerName(): string|null
{
if ($this->adapter !== null) {
return $this->getServerParam('SERVER_NAME');
}

return parent::getServerName();
}

/**
* Retrieves a server parameter value as a string or `null`.
*
Expand Down
7 changes: 6 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

abstract class TestCase extends \PHPUnit\Framework\TestCase
{
/**
* A secret key used for cookie validation in tests.
*/
protected const COOKIE_VALIDATION_KEY = 'wefJDF8sfdsfSDefwqdxj9oq';

/**
* @phpstan-var array<mixed, mixed>
*/
Expand Down Expand Up @@ -185,7 +190,7 @@ protected function webApplication(array $config = []): void
],
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'cookieValidationKey' => self::COOKIE_VALIDATION_KEY,
'isConsoleRequest' => false,
'scriptFile' => __DIR__ . '/index.php',
'scriptUrl' => '/index.php',
Expand Down
10 changes: 1 addition & 9 deletions tests/adapter/ResponseAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ public function testFormatCookieWithDateTimeImmutableExpire(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
],
],
],
Expand Down Expand Up @@ -798,7 +797,6 @@ public function testFormatCookieWithExpireAtCurrentTime(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
],
],
],
Expand Down Expand Up @@ -908,7 +906,7 @@ public function testFormatCookieWithExpireSetToOne(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
'cookieValidationKey' => self::COOKIE_VALIDATION_KEY,
],
],
],
Expand Down Expand Up @@ -1078,7 +1076,6 @@ public function testFormatCookieWithStringExpire(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
],
],
],
Expand Down Expand Up @@ -1154,7 +1151,6 @@ public function testFormatCookieWithStringExpireOne(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
],
],
],
Expand Down Expand Up @@ -1212,7 +1208,6 @@ public function testFormatCookieWithValidationDisabled(): void
'components' => [
'request' => [
'enableCookieValidation' => false,
'cookieValidationKey' => 'some-key',
],
],
],
Expand Down Expand Up @@ -1323,7 +1318,6 @@ public function testFormatCookieWithValidationEnabled(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
],
],
],
Expand Down Expand Up @@ -1383,7 +1377,6 @@ public function testFormatCookieWithValidationEnabledFutureExpiration(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
],
],
],
Expand Down Expand Up @@ -1573,7 +1566,6 @@ public function testHashCookieValueIncludingName(): void
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'test-validation-key-32-characters',
],
],
],
Expand Down
Loading
Loading