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
50 changes: 0 additions & 50 deletions src/adapter/ServerRequestAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\web\{Cookie, HeaderCollection};
use yii\web\NotFoundHttpException;
use yii2\extensions\psrbridge\exception\Message;
use yii2\extensions\psrbridge\http\Request;

use function implode;
use function in_array;
Expand Down Expand Up @@ -48,13 +46,6 @@
*/
final class ServerRequestAdapter
{
/**
* Query parameters resolved from the PSR-7 ServerRequestInterface.
*
* @phpstan-var array<array-key, mixed>
*/
private array $queryParams = [];

/**
* Creates a new instance of the {@see ServerRequestAdapter} class.
*
Expand Down Expand Up @@ -244,10 +235,6 @@ public function getParsedBody(): array|object|null
*/
public function getQueryParams(): array
{
if ($this->queryParams !== []) {
return $this->queryParams;
}

return $this->psrRequest->getQueryParams();
}

Expand Down Expand Up @@ -379,43 +366,6 @@ public function getUrl(): string
return $url;
}

/**
* Resolves the route and parameters from the given {@see Request} instance using Yii2 UrlManager.
*
* Parses the request using Yii2 UrlManager and returns the resolved route and parameters.
*
* If the route is found, combine the parsed parameters with the query parameters from the PSR-7
* ServerRequestAdapter.
*
* @param Request $request Request instance to resolve.
*
* @throws NotFoundHttpException if the route cannot be resolved by UrlManager.
*
* @return array Array containing the resolved route and combined parameters.
*
* @phpstan-return array<array-key, mixed>
*
* Usage example:
* ```php
* [$route, $params] = $adapter->resolve($request);
* ```
*/
public function resolve(Request $request): array
{
/** @phpstan-var array{0: string, 1: array<string, mixed>}|false $result*/
$result = Yii::$app->getUrlManager()->parseRequest($request);

if ($result !== false) {
[$route, $params] = $result;

$this->queryParams = $params + $this->psrRequest->getQueryParams();

return [$route, $this->queryParams];
}

throw new NotFoundHttpException(Yii::t('yii', Message::PAGE_NOT_FOUND->getMessage()));
}

/**
* Extracts cookies from the PSR-7 ServerRequestInterface without validation.
*
Expand Down
50 changes: 35 additions & 15 deletions src/http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace yii2\extensions\psrbridge\http;

use Psr\Http\Message\{ServerRequestInterface, UploadedFileInterface};
use Yii;
use yii\base\InvalidConfigException;
use yii\web\{CookieCollection, HeaderCollection, NotFoundHttpException, UploadedFile};
use yii2\extensions\psrbridge\adapter\ServerRequestAdapter;
Expand Down Expand Up @@ -342,13 +343,14 @@ public function getPsr7Request(): ServerRequestInterface
/**
* Retrieves query parameters from the current request, supporting PSR-7 and Yii2 fallback.
*
* Returns the query parameters as an array from the PSR-7 adapter if present.
* Returns the query parameters as an associative array from the PSR-7 adapter if present. If the parent
* implementation returns a non-empty array, it is used instead to maintain compatibility with Yii2 Request
* component logic.
*
* If no adapter is set, falls back to the parent implementation.
*
* @return array Query parameters for the current request.
* This method enables seamless access to query parameters in both PSR-7 and Yii2 environments, supporting
* interoperability with modern HTTP stacks and legacy workflows.
*
* @phpstan-return array<mixed, mixed>
* @phpstan-return array<array-key, mixed> Query parameters as an associative array.
*
* Usage example:
* ```php
Expand All @@ -358,6 +360,12 @@ public function getPsr7Request(): ServerRequestInterface
public function getQueryParams(): array
{
if ($this->adapter !== null) {
$parentParams = parent::getQueryParams();

if ($parentParams !== []) {
return $parentParams;
}

return $this->adapter->getQueryParams();
}

Expand Down Expand Up @@ -520,29 +528,41 @@ public function reset(): void
}

/**
* Resolves the request parameters using PSR-7 adapter or Yii2 fallback.
*
* Returns an array of resolved request parameters by delegating to the PSR-7 ServerRequestAdapter if present, or
* falling back to the parent Yii2 implementation when no adapter is set.
* Resolves the current request into a route and parameters, supporting PSR-7 and Yii2 fallback.
*
* This method enables seamless interoperability between PSR-7 compatible HTTP stacks and legacy Yii2 workflows,
* ensuring consistent parameter resolution in both environments.
* Parses the request using the Yii2 UrlManager and merges parameters with those from the PSR-7 adapter if present.
* - If the PSR-7 adapter is set, this method delegates parsing to the UrlManager, merges the resulting parameters
* with the query parameters from the adapter, and updates the request's query parameters accordingly.
* - If no adapter is present, it falls back to the parent implementation.
*
* @throws NotFoundHttpException if the route cannot be resolved by UrlManager.
* @throws NotFoundHttpException if the route is not found or undefined.
*
* @return array Array of resolved request parameters for the current request.
* @return array An array containing the resolved route and parameters.
*
* @phpstan-return array<array-key, mixed>
*
* Usage example:
* ```php
* $params = $request->resolve();
* [$route, $params] = $request->resolve();
* ```
*/
public function resolve(): array
{
if ($this->adapter !== null) {
return $this->adapter->resolve($this);
/** @phpstan-var array{0: string, 1: array<string, mixed>}|false $result*/
$result = Yii::$app->getUrlManager()->parseRequest($this);

if ($result !== false) {
[$route, $params] = $result;

$mergedParams = $params + $this->adapter->getQueryParams();

$this->setQueryParams($mergedParams);

return [$route, $mergedParams];
}

throw new NotFoundHttpException(Yii::t('yii', Message::PAGE_NOT_FOUND->getMessage()));
}

return parent::resolve();
Expand Down
Loading