This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
generated from yiisoft/package-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
24 changed files
with
500 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestModel\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(flags: Attribute::TARGET_PARAMETER)] | ||
final class Body implements HandlerParameterAttributeInterface | ||
{ | ||
public function getType(): string | ||
{ | ||
return self::REQUEST_BODY; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestModel\Attribute; | ||
|
||
/** | ||
* Represents action handler parameter [attribute](https://www.php.net/manual/en/language.attributes.php). | ||
*/ | ||
interface HandlerParameterAttributeInterface | ||
{ | ||
public const ROUTE_PARAM = 'route_param'; | ||
public const REQUEST_BODY = 'request_body'; | ||
public const REQUEST_ATTRIBUTE = 'request_attribute'; | ||
public const UPLOADED_FILES = 'uploaded_files'; | ||
public const QUERY_PARAM = 'query_param'; | ||
|
||
public function getName(): ?string; | ||
|
||
public function getType(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestModel\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(flags: Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class Query implements HandlerParameterAttributeInterface | ||
{ | ||
public function __construct(private string $name) | ||
{ | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::QUERY_PARAM; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestModel\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(flags: Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class Request implements HandlerParameterAttributeInterface | ||
{ | ||
public function __construct(private string $name) | ||
{ | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::REQUEST_ATTRIBUTE; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestModel\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(flags: Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class Route implements HandlerParameterAttributeInterface | ||
{ | ||
public function __construct(private string $name) | ||
{ | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::ROUTE_PARAM; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestModel\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(flags: Attribute::TARGET_PARAMETER)] | ||
final class UploadedFiles implements HandlerParameterAttributeInterface | ||
{ | ||
public function getType(): string | ||
{ | ||
return self::UPLOADED_FILES; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestModel; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Yiisoft\RequestModel\Attribute\HandlerParameterAttributeInterface; | ||
use Yiisoft\Router\CurrentRoute; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class HandlerParametersResolver | ||
{ | ||
public function __construct(private RequestModelFactory $factory, private CurrentRoute $currentRoute) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \ReflectionException | ||
*/ | ||
public function resolve(array $parameters, ServerRequestInterface $request): array | ||
{ | ||
return array_merge( | ||
$this->getAttributeParams($parameters, $request), | ||
$this->factory->createInstances($request, $parameters) | ||
); | ||
} | ||
|
||
/** | ||
* @param \ReflectionParameter[] $parameters | ||
* @param ServerRequestInterface $request | ||
* | ||
* @return array | ||
*/ | ||
private function getAttributeParams(array $parameters, ServerRequestInterface $request): array | ||
{ | ||
$actionParameters = []; | ||
foreach ($parameters as $parameter) { | ||
$attributes = $parameter->getAttributes( | ||
HandlerParameterAttributeInterface::class, | ||
\ReflectionAttribute::IS_INSTANCEOF | ||
); | ||
foreach ($attributes as $attribute) { | ||
/** @var HandlerParameterAttributeInterface $attributeInstance */ | ||
$attributeInstance = $attribute->newInstance(); | ||
|
||
$actionParameters[$parameter->getName()] = match ($attributeInstance->getType()) { | ||
HandlerParameterAttributeInterface::ROUTE_PARAM => $this | ||
->currentRoute | ||
->getArgument($attributeInstance->getName()), | ||
HandlerParameterAttributeInterface::REQUEST_BODY => $request->getParsedBody(), | ||
HandlerParameterAttributeInterface::REQUEST_ATTRIBUTE => $request->getAttribute( | ||
$attributeInstance->getName() | ||
), | ||
HandlerParameterAttributeInterface::QUERY_PARAM => $request | ||
->getQueryParams()[$attributeInstance->getName()] ?? null, | ||
HandlerParameterAttributeInterface::UPLOADED_FILES => $request->getUploadedFiles() | ||
}; | ||
} | ||
} | ||
return $actionParameters; | ||
} | ||
} |
Oops, something went wrong.