-
-
Notifications
You must be signed in to change notification settings - Fork 25
Add attributes support #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rustamwin
wants to merge
73
commits into
master
Choose a base branch
from
add-attributes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
a60d03b
Add attributes support
rustamwin b328c82
Apply fixes from StyleCI
StyleCIBot 3496866
Fix psalm annotation
rustamwin 69d9832
Add tests
rustamwin bc421e9
Merge branch 'master' into add-attributes
xepozz 875d7fd
Merge branch 'master' into add-attributes
xepozz 777ff54
Add attributes registrar
rustamwin 3959325
Apply fixes from StyleCI
StyleCIBot 1651071
Add tests
rustamwin 838bbc4
Fix tests & psalm issues
rustamwin 4e63476
Apply fixes from StyleCI
StyleCIBot a0fb7a2
Add test cases
rustamwin ed2f60f
Apply fixes from StyleCI
StyleCIBot c239d53
Improve Route
rustamwin e8367aa
Add test
rustamwin 2ab8dc2
Merge branch 'master' into add-attributes
xepozz 2a60945
Increase coverage
rustamwin 61eafb0
Merge remote-tracking branch 'origin/add-attributes' into add-attributes
rustamwin 01665f4
Merge branch 'master' into add-attributes
xepozz e7d7a43
Add routes resource & attributes (#220)
rustamwin ab43b76
Fix AttributeRoutesProvider
rustamwin 7a0e222
Allow attributes to use in classes
rustamwin 2c38b06
Fix psalm annotation
rustamwin 6a2bd4c
Add `RouteAttributeInterface` (#221)
vjik ab467d8
Adjust naming
rustamwin 61cbc83
Minor improvements
rustamwin d85e667
Minor
rustamwin 0ebcb5a
Move attribute collector it's own package
rustamwin 3ff6921
Cleanup
rustamwin a81c91a
Minor
rustamwin 5be62b4
Add changelog
rustamwin 63467dc
Apply Rector changes (CI)
rustamwin 91b5a87
Merge remote-tracking branch 'origin/master' into add-attributes
rustamwin 81a5858
Fixes
rustamwin b44cd31
Apply Rector changes (CI)
rustamwin d3bc8e4
Apply fixes from StyleCI
StyleCIBot f4b3020
Fix psalm issues
rustamwin 333edf7
Merge remote-tracking branch 'origin/master' into add-attributes
rustamwin 5a0221d
Apply PHP CS Fixer and Rector changes (CI)
rustamwin 26ea39e
Merge remote-tracking branch 'origin/master' into add-attributes
rustamwin 00bf155
Fix: BC
rustamwin 3326413
Add tests for default override and middleware behavior; enhance file …
rustamwin 72c9f66
Refactor: remove unused properties in Group; add return type to middl…
rustamwin 24f5323
Merge branch 'master' into add-attributes
samdark 302fae5
Refactor: extract host normalization logic to a shared private method…
rustamwin f38b6da
Apply PHP CS Fixer and Rector changes (CI)
rustamwin b7f262f
Apply PHP CS Fixer and Rector changes (CI)
samdark bb2a333
Update CHANGELOG: consolidate duplicate entries for #196
rustamwin 0b981f1
Refactor: improve PHPDoc for route attributes and update validation m…
rustamwin 4ec6f08
Apply PHP CS Fixer and Rector changes (CI)
rustamwin a9dffd4
Update CHANGELOG: add bug fix entry for `Group::hosts()` method consi…
rustamwin b0e9813
Add phpdoc
samdark 65e1e13
Mark `Group::create`, `RouteCollectorInterface`, `RouteCollector`, an…
rustamwin a8b9596
Update src/Route.php
samdark 58a88d5
Fix incorrectly backtick usage and wrong class name in @deperecated
samdark 1c16802
Add validation for single route case
samdark 35c4c40
Apply PHP CS Fixer and Rector changes (CI)
samdark 82c8fa1
Allow passing method as a single value instead of an array
samdark 1206c2a
Action is used more than name, swap order
samdark 652bdae
Add missing phpdoc
samdark 69c2c62
Add missing Group phpdoc
samdark 60bd8ee
Remove unclear statement
samdark 9626883
Use cast instead of check to get an array from $method
samdark e1ad7e8
Apply PHP CS Fixer and Rector changes (CI)
samdark 8b5cdbc
Compare with empty array instead of using `empty()`
samdark cc20fc2
Fix: suppress psalm warnings and enhance PHPDocs in Route and RouteCo…
rustamwin 12bd3fc
Apply PHP CS Fixer and Rector changes (CI)
rustamwin 860f963
Fix: suppress psalm warnings and improve PHPDocs in RouteCollection a…
rustamwin a03bfb4
Merge remote-tracking branch 'origin/add-attributes' into add-attributes
rustamwin f6abda7
Fix: suppress psalm warnings and enhance PHPDocs in RouteCollection
rustamwin a5b3a60
Fix: adjust psalm suppression placement in RouteCollection PHPDoc
rustamwin c080a1c
Kill mutation
rustamwin 2aa46f1
Refactor: mark `$container` as readonly in `RouterCollector` and upda…
rustamwin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ with an adapter package. Currently, the only adapter available is [FastRoute](ht | |
| - Ready to use middleware for route matching. | ||
| - Convenient `CurrentRoute` service that holds information about last matched route. | ||
| - Out of the box CORS middleware support. | ||
| - Declaring routes using PHP attributes. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be an example for using attributes and then reading these with |
||
|
|
||
| ## Requirements | ||
|
|
||
|
|
@@ -45,7 +46,7 @@ Additionally, you will need an adapter such as [FastRoute](https://github.com/yi | |
|
|
||
| ## Defining routes and URL matching | ||
|
|
||
| Common usage of the router looks like the following: | ||
| #### Common usage of the router looks like the following | ||
|
|
||
| ```php | ||
| use Yiisoft\Router\CurrentRoute; | ||
|
|
@@ -101,6 +102,36 @@ if (!$result->isSuccess()) { | |
| $response = $result->process($request, $notFoundHandler); | ||
| ``` | ||
|
|
||
| #### Using attributes is also supported | ||
|
|
||
| In controller: | ||
|
|
||
| ```php | ||
| use Yiisoft\Router\Attribute\Get; | ||
|
|
||
| final class SiteController | ||
| { | ||
| //... | ||
|
|
||
| #[Get('/')] | ||
| public function home(ServerRequestInterface $request): ResponseInterface | ||
| { | ||
| return $this->responseFactory->createResponse()->withBody( | ||
| $this->streamFactory->createStream('You are at homepage.') | ||
| ); | ||
| } | ||
|
|
||
| #[Get('/test/{id:\w+}')] | ||
| public function test(CurrentRoute $currentRoute): ResponseInterface | ||
| { | ||
| $id = $currentRoute->getArgument('id'); | ||
|
|
||
| return $this->responseFactory->createResponse()->withBody( | ||
| $this->streamFactory->createStream('You are at test with argument ' . $id) | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| > Note: Despite `UrlGeneratorInterface` and `UrlMatcherInterface` being common for all adapters available, certain | ||
| > features and, especially, pattern syntax may differ. To check usage and configuration details, please refer | ||
| > to specific adapter documentation. All examples in this document are for | ||
|
|
||
This file contains hidden or 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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Router\Attribute; | ||
|
|
||
| use Attribute; | ||
| use Stringable; | ||
| use Yiisoft\Http\Method; | ||
| use Yiisoft\Router\Route; | ||
|
|
||
| /** | ||
| * Route attribute that defines a DELETE HTTP method route. | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
| final class Delete implements RouteAttributeInterface | ||
| { | ||
| private readonly Route $route; | ||
|
|
||
| /** | ||
| * @param string $pattern Route pattern. | ||
| * @param string|null $name Route name. | ||
| * @param array[]|callable[]|string[] $middlewares Middlewares to be added to the route. | ||
| * @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names. | ||
| * @param string[] $hosts Hosts that the route should match. | ||
| * @param bool $override Marks route as override. When added, it will replace the existing route with the same name. | ||
| * @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. | ||
| * It is useful to avoid invoking one of the parent group middleware for | ||
| * a certain route. | ||
| * | ||
| * @psalm-param list<array|callable|string> $middlewares | ||
| */ | ||
| public function __construct( | ||
| string $pattern, | ||
| ?string $name = null, | ||
| array $middlewares = [], | ||
| array $defaults = [], | ||
| array $hosts = [], | ||
| bool $override = false, | ||
| array $disabledMiddlewares = [], | ||
| ) { | ||
| $this->route = new Route( | ||
| method: [Method::DELETE], | ||
| pattern: $pattern, | ||
| name: $name, | ||
| middlewares: $middlewares, | ||
| defaults: $defaults, | ||
| hosts: $hosts, | ||
| override: $override, | ||
| disabledMiddlewares: $disabledMiddlewares, | ||
| ); | ||
| } | ||
|
|
||
| public function getRoute(): Route | ||
| { | ||
| return $this->route; | ||
| } | ||
| } |
This file contains hidden or 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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Router\Attribute; | ||
|
|
||
| use Attribute; | ||
| use Stringable; | ||
| use Yiisoft\Http\Method; | ||
| use Yiisoft\Router\Route; | ||
|
|
||
| /** | ||
| * Route attribute that defines a GET HTTP method route. | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
| final class Get implements RouteAttributeInterface | ||
| { | ||
| private readonly Route $route; | ||
|
|
||
| /** | ||
| * @param string $pattern Route pattern. | ||
| * @param string|null $name Route name. | ||
| * @param array[]|callable[]|string[] $middlewares Middlewares to be added to the route. | ||
| * @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names. | ||
| * @param string[] $hosts Hosts that the route should match. | ||
| * @param bool $override Marks route as override. When added it will replace existing route with the same name. | ||
| * @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. | ||
| * It is useful to avoid invoking one of the parent group middleware for | ||
| * a certain route. | ||
| * | ||
| * @psalm-param list<array|callable|string> $middlewares | ||
| */ | ||
| public function __construct( | ||
| string $pattern, | ||
| ?string $name = null, | ||
| array $middlewares = [], | ||
| array $defaults = [], | ||
| array $hosts = [], | ||
| bool $override = false, | ||
| array $disabledMiddlewares = [], | ||
| ) { | ||
| $this->route = new Route( | ||
| method: [Method::GET], | ||
| pattern: $pattern, | ||
| name: $name, | ||
| middlewares: $middlewares, | ||
| defaults: $defaults, | ||
| hosts: $hosts, | ||
| override: $override, | ||
| disabledMiddlewares: $disabledMiddlewares, | ||
| ); | ||
| } | ||
|
|
||
| public function getRoute(): Route | ||
| { | ||
| return $this->route; | ||
| } | ||
| } |
This file contains hidden or 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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Router\Attribute; | ||
|
|
||
| use Attribute; | ||
| use Stringable; | ||
| use Yiisoft\Http\Method; | ||
| use Yiisoft\Router\Route; | ||
|
|
||
| /** | ||
| * Route attribute that defines a HEAD HTTP method route. | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
| final class Head implements RouteAttributeInterface | ||
| { | ||
| private readonly Route $route; | ||
|
|
||
| /** | ||
| * @param string $pattern Route pattern. | ||
| * @param string|null $name Route name. | ||
| * @param array[]|callable[]|string[] $middlewares Middlewares to be added to the route. | ||
| * @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names. | ||
| * @param string[] $hosts Hosts that the route should match. | ||
| * @param bool $override Marks route as override. When added, it will replace the existing route with the same name. | ||
| * @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. | ||
| * It is useful to avoid invoking one of the parent group middleware for | ||
| * a certain route. | ||
| * | ||
| * @psalm-param list<array|callable|string> $middlewares | ||
| */ | ||
| public function __construct( | ||
| string $pattern, | ||
| ?string $name = null, | ||
| array $middlewares = [], | ||
| array $defaults = [], | ||
| array $hosts = [], | ||
| bool $override = false, | ||
| array $disabledMiddlewares = [], | ||
| ) { | ||
| $this->route = new Route( | ||
| method: [Method::HEAD], | ||
| pattern: $pattern, | ||
| name: $name, | ||
| middlewares: $middlewares, | ||
| defaults: $defaults, | ||
| hosts: $hosts, | ||
| override: $override, | ||
| disabledMiddlewares: $disabledMiddlewares, | ||
| ); | ||
| } | ||
|
|
||
| public function getRoute(): Route | ||
| { | ||
| return $this->route; | ||
| } | ||
| } |
This file contains hidden or 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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Router\Attribute; | ||
|
|
||
| use Attribute; | ||
| use Stringable; | ||
| use Yiisoft\Http\Method; | ||
| use Yiisoft\Router\Route; | ||
|
|
||
| /** | ||
| * Route attribute that defines an OPTIONS HTTP method route. | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
| final class Options implements RouteAttributeInterface | ||
| { | ||
| private readonly Route $route; | ||
|
|
||
| /** | ||
| * @param string $pattern Route pattern. | ||
| * @param string|null $name Route name. | ||
| * @param array[]|callable[]|string[] $middlewares Middlewares to be added to the route. | ||
| * @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names. | ||
| * @param string[] $hosts Hosts that the route should match. | ||
| * @param bool $override Marks route as override. When added, it will replace the existing route with the same name. | ||
| * @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. | ||
| * It is useful to avoid invoking one of the parent group middleware for | ||
| * a certain route. | ||
| * | ||
| * @psalm-param list<array|callable|string> $middlewares | ||
| */ | ||
| public function __construct( | ||
| string $pattern, | ||
| ?string $name = null, | ||
| array $middlewares = [], | ||
| array $defaults = [], | ||
| array $hosts = [], | ||
| bool $override = false, | ||
| array $disabledMiddlewares = [], | ||
| ) { | ||
| $this->route = new Route( | ||
| method: [Method::OPTIONS], | ||
| pattern: $pattern, | ||
| name: $name, | ||
| middlewares: $middlewares, | ||
| defaults: $defaults, | ||
| hosts: $hosts, | ||
| override: $override, | ||
| disabledMiddlewares: $disabledMiddlewares, | ||
| ); | ||
| } | ||
|
|
||
| public function getRoute(): Route | ||
| { | ||
| return $this->route; | ||
| } | ||
| } |
This file contains hidden or 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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Router\Attribute; | ||
|
|
||
| use Attribute; | ||
| use Stringable; | ||
| use Yiisoft\Http\Method; | ||
| use Yiisoft\Router\Route; | ||
|
|
||
| /** | ||
| * Route attribute that defines a PATCH HTTP method route. | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
| final class Patch implements RouteAttributeInterface | ||
| { | ||
| private readonly Route $route; | ||
|
|
||
| /** | ||
| * @param string $pattern Route pattern. | ||
| * @param string|null $name Route name. | ||
| * @param array[]|callable[]|string[] $middlewares Middlewares to be added to the route. | ||
| * @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names. | ||
| * @param string[] $hosts Hosts that the route should match. | ||
| * @param bool $override Marks route as override. When added, it will replace the existing route with the same name. | ||
| * @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. | ||
| * It is useful to avoid invoking one of the parent group middleware for | ||
| * a certain route. | ||
| * | ||
| * @psalm-param list<array|callable|string> $middlewares | ||
| */ | ||
| public function __construct( | ||
| string $pattern, | ||
| ?string $name = null, | ||
| array $middlewares = [], | ||
| array $defaults = [], | ||
| array $hosts = [], | ||
| bool $override = false, | ||
| array $disabledMiddlewares = [], | ||
| ) { | ||
| $this->route = new Route( | ||
| method: [Method::PATCH], | ||
| pattern: $pattern, | ||
| name: $name, | ||
| middlewares: $middlewares, | ||
| defaults: $defaults, | ||
| hosts: $hosts, | ||
| override: $override, | ||
| disabledMiddlewares: $disabledMiddlewares, | ||
| ); | ||
| } | ||
|
|
||
| public function getRoute(): Route | ||
| { | ||
| return $this->route; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to mention the deprecation of
Routestatic constructors and describe these in the README.