diff --git a/src/adapter/ServerRequestAdapter.php b/src/adapter/ServerRequestAdapter.php index 6c4a81c8..91a059c8 100644 --- a/src/adapter/ServerRequestAdapter.php +++ b/src/adapter/ServerRequestAdapter.php @@ -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; @@ -48,13 +46,6 @@ */ final class ServerRequestAdapter { - /** - * Query parameters resolved from the PSR-7 ServerRequestInterface. - * - * @phpstan-var array - */ - private array $queryParams = []; - /** * Creates a new instance of the {@see ServerRequestAdapter} class. * @@ -244,10 +235,6 @@ public function getParsedBody(): array|object|null */ public function getQueryParams(): array { - if ($this->queryParams !== []) { - return $this->queryParams; - } - return $this->psrRequest->getQueryParams(); } @@ -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 - * - * Usage example: - * ```php - * [$route, $params] = $adapter->resolve($request); - * ``` - */ - public function resolve(Request $request): array - { - /** @phpstan-var array{0: string, 1: array}|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. * diff --git a/src/http/Request.php b/src/http/Request.php index 6f1c143e..8077adb2 100644 --- a/src/http/Request.php +++ b/src/http/Request.php @@ -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; @@ -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 + * @phpstan-return array Query parameters as an associative array. * * Usage example: * ```php @@ -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(); } @@ -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 * * 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}|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();