Skip to content
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

Add attributes support & improve MatchingResult #196

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a60d03b
Add attributes support
rustamwin Mar 28, 2023
b328c82
Apply fixes from StyleCI
StyleCIBot Mar 28, 2023
3496866
Fix psalm annotation
rustamwin Mar 28, 2023
69d9832
Add tests
rustamwin Mar 28, 2023
bc421e9
Merge branch 'master' into add-attributes
xepozz May 29, 2023
875d7fd
Merge branch 'master' into add-attributes
xepozz Jun 8, 2023
777ff54
Add attributes registrar
rustamwin Jun 12, 2023
3959325
Apply fixes from StyleCI
StyleCIBot Jun 12, 2023
1651071
Add tests
rustamwin Jun 12, 2023
838bbc4
Fix tests & psalm issues
rustamwin Jun 15, 2023
4e63476
Apply fixes from StyleCI
StyleCIBot Jun 15, 2023
a0fb7a2
Add test cases
rustamwin Jun 15, 2023
ed2f60f
Apply fixes from StyleCI
StyleCIBot Jun 15, 2023
c239d53
Improve Route
rustamwin Aug 19, 2023
e8367aa
Add test
rustamwin Aug 19, 2023
2ab8dc2
Merge branch 'master' into add-attributes
xepozz Aug 19, 2023
2a60945
Increase coverage
rustamwin Aug 19, 2023
61eafb0
Merge remote-tracking branch 'origin/add-attributes' into add-attributes
rustamwin Aug 19, 2023
01665f4
Merge branch 'master' into add-attributes
xepozz Oct 9, 2023
e7d7a43
Add routes resource & attributes (#220)
rustamwin Oct 10, 2023
ab43b76
Fix AttributeRoutesProvider
rustamwin Oct 11, 2023
7a0e222
Allow attributes to use in classes
rustamwin Oct 15, 2023
2c38b06
Fix psalm annotation
rustamwin Oct 15, 2023
6a2bd4c
Add `RouteAttributeInterface` (#221)
vjik Oct 15, 2023
ab467d8
Adjust naming
rustamwin Oct 16, 2023
61cbc83
Minor improvements
rustamwin Oct 16, 2023
d85e667
Minor
rustamwin Oct 16, 2023
0ebcb5a
Move attribute collector it's own package
rustamwin Oct 23, 2023
3ff6921
Cleanup
rustamwin Oct 23, 2023
a81c91a
Minor
rustamwin Oct 23, 2023
5be62b4
Add changelog
rustamwin Nov 2, 2023
63467dc
Apply Rector changes (CI)
rustamwin Nov 2, 2023
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
5 changes: 2 additions & 3 deletions .phpstorm.meta.php/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
'host',
'hosts',
'corsMiddleware',
'items',
'routes',
'middlewareDefinitions',
'hasDispatcher',
'hasCorsMiddleware'
);
}
}
7 changes: 3 additions & 4 deletions .phpstorm.meta.php/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
'methods',
'override',
'defaults',
'dispatcherWithMiddlewares',
'hasDispatcher',
'hasMiddlewares'
'hasMiddlewares',
'builtMiddlewareDefinitions'
);
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
- New #195: Add debug collector for `yiisoft/yii-debug` (@xepozz)
- Chg #207: Replace two `RouteCollectorInterface` methods `addRoute()` and `addGroup()` to single `addRoute()` (@vjik)
- Enh #202: Add support for `psr/http-message` version `^2.0` (@vjik)
- New #196: Add PHP Attributes support (@rustamwin)
- New #196: Add `RoutesProviderInterface` interface providing routes from various resources (@rustamwin)
- Enh #196: The `Group` and `Route` classes have been refactored to be DTO objects & dispatcher-independent. (@rustamwin)
- Enh #196: The `MatchingResult` class has been improved to be dispatcher-independent (@rustamwin)
- Chg #196: The implementation of `MatchingResult` from `MiddlewareInterface` has been removed, so
it is no longer middleware. (@rustamwin)

## 3.0.0 February 17, 2023

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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.

## Requirements

Expand Down
49 changes: 49 additions & 0 deletions src/Attribute/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Attribute;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Delete implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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.
*/
public function __construct(
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
bool $override = false,
array $disabledMiddlewares = []
) {
$this->route = new Route(
methods: [Method::DELETE],
pattern: $pattern,
name: $name,
middlewareDefinitions: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewareDefinitions: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
49 changes: 49 additions & 0 deletions src/Attribute/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Attribute;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Get implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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.
*/
public function __construct(
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
bool $override = false,
array $disabledMiddlewares = []
) {
$this->route = new Route(
methods: [Method::GET],
pattern: $pattern,
name: $name,
middlewareDefinitions: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewareDefinitions: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
49 changes: 49 additions & 0 deletions src/Attribute/Head.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Attribute;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Head implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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.
*/
public function __construct(
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
bool $override = false,
array $disabledMiddlewares = []
) {
$this->route = new Route(
methods: [Method::HEAD],
pattern: $pattern,
name: $name,
middlewareDefinitions: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewareDefinitions: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
49 changes: 49 additions & 0 deletions src/Attribute/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Attribute;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Options implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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.
*/
public function __construct(
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
bool $override = false,
array $disabledMiddlewares = []
) {
$this->route = new Route(
methods: [Method::OPTIONS],
pattern: $pattern,
name: $name,
middlewareDefinitions: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewareDefinitions: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
49 changes: 49 additions & 0 deletions src/Attribute/Patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Attribute;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Patch implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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.
*/
public function __construct(
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
bool $override = false,
array $disabledMiddlewares = []
) {
$this->route = new Route(
methods: [Method::PATCH],
pattern: $pattern,
name: $name,
middlewareDefinitions: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewareDefinitions: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
49 changes: 49 additions & 0 deletions src/Attribute/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Attribute;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Post implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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.
*/
public function __construct(
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
bool $override = false,
array $disabledMiddlewares = []
) {
$this->route = new Route(
methods: [Method::POST],
pattern: $pattern,
name: $name,
middlewareDefinitions: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewareDefinitions: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
49 changes: 49 additions & 0 deletions src/Attribute/Put.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Attribute;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Put implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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.
*/
public function __construct(
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
bool $override = false,
array $disabledMiddlewares = []
) {
$this->route = new Route(
methods: [Method::PUT],
pattern: $pattern,
name: $name,
middlewareDefinitions: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewareDefinitions: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
Loading