Skip to content

Commit

Permalink
Add RouteAttributeInterface (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Oct 15, 2023
1 parent 2c38b06 commit 6a2bd4c
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 54 deletions.
14 changes: 11 additions & 3 deletions src/Attribute/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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 extends Route
final class Delete implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|\Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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
Expand All @@ -27,7 +30,7 @@ public function __construct(
bool $override = false,

Check warning on line 30 in src/Attribute/Delete.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::DELETE], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }

Check warning on line 30 in src/Attribute/Delete.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::DELETE], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }
array $disabledMiddlewares = []
) {
parent::__construct(
$this->route = new Route(
methods: [Method::DELETE],
pattern: $pattern,
name: $name,
Expand All @@ -38,4 +41,9 @@ public function __construct(
disabledMiddlewares: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
14 changes: 11 additions & 3 deletions src/Attribute/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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 extends Route
final class Get implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|\Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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
Expand All @@ -27,7 +30,7 @@ public function __construct(
bool $override = false,

Check warning on line 30 in src/Attribute/Get.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::GET], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }

Check warning on line 30 in src/Attribute/Get.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::GET], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }
array $disabledMiddlewares = []
) {
parent::__construct(
$this->route = new Route(
methods: [Method::GET],
pattern: $pattern,
name: $name,
Expand All @@ -38,4 +41,9 @@ public function __construct(
disabledMiddlewares: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
14 changes: 11 additions & 3 deletions src/Attribute/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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 extends Route
final class Head implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|\Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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
Expand All @@ -27,7 +30,7 @@ public function __construct(
bool $override = false,

Check warning on line 30 in src/Attribute/Head.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::HEAD], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }

Check warning on line 30 in src/Attribute/Head.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::HEAD], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }
array $disabledMiddlewares = []
) {
parent::__construct(
$this->route = new Route(
methods: [Method::HEAD],
pattern: $pattern,
name: $name,
Expand All @@ -38,4 +41,9 @@ public function __construct(
disabledMiddlewares: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
14 changes: 11 additions & 3 deletions src/Attribute/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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 extends Route
final class Options implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|\Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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
Expand All @@ -27,7 +30,7 @@ public function __construct(
bool $override = false,

Check warning on line 30 in src/Attribute/Options.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::OPTIONS], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }

Check warning on line 30 in src/Attribute/Options.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::OPTIONS], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }
array $disabledMiddlewares = []
) {
parent::__construct(
$this->route = new Route(
methods: [Method::OPTIONS],
pattern: $pattern,
name: $name,
Expand All @@ -38,4 +41,9 @@ public function __construct(
disabledMiddlewares: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
14 changes: 11 additions & 3 deletions src/Attribute/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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 extends Route
final class Patch implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|\Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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
Expand All @@ -27,7 +30,7 @@ public function __construct(
bool $override = false,

Check warning on line 30 in src/Attribute/Patch.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::PATCH], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }

Check warning on line 30 in src/Attribute/Patch.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::PATCH], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }
array $disabledMiddlewares = []
) {
parent::__construct(
$this->route = new Route(
methods: [Method::PATCH],
pattern: $pattern,
name: $name,
Expand All @@ -38,4 +41,9 @@ public function __construct(
disabledMiddlewares: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
14 changes: 11 additions & 3 deletions src/Attribute/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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 extends Route
final class Post implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|\Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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
Expand All @@ -27,7 +30,7 @@ public function __construct(
bool $override = false,

Check warning on line 30 in src/Attribute/Post.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::POST], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }

Check warning on line 30 in src/Attribute/Post.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::POST], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }
array $disabledMiddlewares = []
) {
parent::__construct(
$this->route = new Route(
methods: [Method::POST],
pattern: $pattern,
name: $name,
Expand All @@ -38,4 +41,9 @@ public function __construct(
disabledMiddlewares: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
14 changes: 11 additions & 3 deletions src/Attribute/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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 extends Route
final class Put implements RouteAttributeInterface
{
private Route $route;

/**
* @param array<string,scalar|\Stringable|null> $defaults Parameter default values indexed by parameter names.
* @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
Expand All @@ -27,7 +30,7 @@ public function __construct(
bool $override = false,

Check warning on line 30 in src/Attribute/Put.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::PUT], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }

Check warning on line 30 in src/Attribute/Put.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * 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 = []) + public function __construct(string $pattern, ?string $name = null, array $middlewares = [], array $defaults = [], array $hosts = [], bool $override = true, array $disabledMiddlewares = []) { $this->route = new Route(methods: [Method::PUT], pattern: $pattern, name: $name, middlewares: $middlewares, defaults: $defaults, hosts: $hosts, override: $override, disabledMiddlewares: $disabledMiddlewares); }
array $disabledMiddlewares = []
) {
parent::__construct(
$this->route = new Route(
methods: [Method::PUT],
pattern: $pattern,
name: $name,
Expand All @@ -38,4 +41,9 @@ public function __construct(
disabledMiddlewares: $disabledMiddlewares
);
}

public function getRoute(): Route
{
return $this->route;
}
}
49 changes: 49 additions & 0 deletions src/Attribute/Route.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\Router\Route as RouteObject;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Route implements RouteAttributeInterface
{
private RouteObject $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(

Check warning on line 23 in src/Attribute/Route.php

View check run for this annotation

Codecov / codecov/patch

src/Attribute/Route.php#L23

Added line #L23 was not covered by tests
array $methods,
string $pattern,
?string $name = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],

Check warning on line 29 in src/Attribute/Route.php

View check run for this annotation

Codecov / codecov/patch

src/Attribute/Route.php#L27-L29

Added lines #L27 - L29 were not covered by tests
bool $override = false,
array $disabledMiddlewares = []

Check warning on line 31 in src/Attribute/Route.php

View check run for this annotation

Codecov / codecov/patch

src/Attribute/Route.php#L31

Added line #L31 was not covered by tests
) {
$this->route = new RouteObject(
methods: $methods,
pattern: $pattern,
name: $name,
middlewares: $middlewares,
defaults: $defaults,
hosts: $hosts,
override: $override,
disabledMiddlewares: $disabledMiddlewares
);

Check warning on line 42 in src/Attribute/Route.php

View check run for this annotation

Codecov / codecov/patch

src/Attribute/Route.php#L33-L42

Added lines #L33 - L42 were not covered by tests
}

public function getRoute(): RouteObject

Check warning on line 45 in src/Attribute/Route.php

View check run for this annotation

Codecov / codecov/patch

src/Attribute/Route.php#L45

Added line #L45 was not covered by tests
{
return $this->route;

Check warning on line 47 in src/Attribute/Route.php

View check run for this annotation

Codecov / codecov/patch

src/Attribute/Route.php#L47

Added line #L47 was not covered by tests
}
}
12 changes: 12 additions & 0 deletions src/Attribute/RouteAttributeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Attribute;

use Yiisoft\Router\Route;

interface RouteAttributeInterface
{
public function getRoute(): Route;
}
18 changes: 10 additions & 8 deletions src/Provider/AttributeRoutesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,40 @@
namespace Yiisoft\Router\Provider;

use olvlvl\ComposerAttributeCollector\Attributes;
use ReflectionAttribute;
use ReflectionClass;
use Yiisoft\Router\Attribute\RouteAttributeInterface;
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;

/**
* An attribute provider provides routes that declared via PHP Attributes.
* Currently, uses `olvlvl/composer-attribute-collector`. {@link https://github.com/olvlvl/composer-attribute-collector}.
*
* @codeCoverageIgnore
*/
final class AttributeRoutesProvider implements RoutesProviderInterface
{
/**
* @var array<class-string, \ReflectionClass>
* @var array<class-string, ReflectionClass>
*/
private static array $reflectionsCache = [];

public function getRoutes(): array

Check warning on line 25 in src/Provider/AttributeRoutesProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/AttributeRoutesProvider.php#L25

Added line #L25 was not covered by tests
{
$routes = [];
$groupRoutes = [];
$routePredicate = Attributes::predicateForAttributeInstanceOf(Route::class);
$routePredicate = Attributes::predicateForAttributeInstanceOf(RouteAttributeInterface::class);
$targetMethods = Attributes::filterTargetMethods($routePredicate);
foreach ($targetMethods as $targetMethod) {

Check warning on line 31 in src/Provider/AttributeRoutesProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/AttributeRoutesProvider.php#L27-L31

Added lines #L27 - L31 were not covered by tests
/** @var Route $route */
$route = $targetMethod->attribute;
$targetMethodReflection = self::$reflectionsCache[$targetMethod->class] ??= new \ReflectionClass(
/** @var RouteAttributeInterface $routeAttribute */
$routeAttribute = $targetMethod->attribute;
$route = $routeAttribute->getRoute();
$targetMethodReflection = self::$reflectionsCache[$targetMethod->class] ??= new ReflectionClass(
$targetMethod->class
);

Check warning on line 37 in src/Provider/AttributeRoutesProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/AttributeRoutesProvider.php#L33-L37

Added lines #L33 - L37 were not covered by tests
/** @var Group[] $groupAttributes */
$groupAttributes = $targetMethodReflection->getAttributes(
Group::class,
\ReflectionAttribute::IS_INSTANCEOF
ReflectionAttribute::IS_INSTANCEOF
);
if (!empty($groupAttributes)) {
$groupRoutes[$targetMethod->class][] = $route->action([$targetMethod->class, $targetMethod->name]);

Check warning on line 44 in src/Provider/AttributeRoutesProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/AttributeRoutesProvider.php#L39-L44

Added lines #L39 - L44 were not covered by tests
Expand Down
Loading

0 comments on commit 6a2bd4c

Please sign in to comment.