Skip to content

Commit

Permalink
refactor(router): convert route attributes to plain objects (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackshadev authored Dec 7, 2024
1 parent be20015 commit 88bd85d
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 131 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
])
->name('*.php')
->notName('*.cache.php')
->notPath('Tempest/Router/src/Route.php') // phpcs doesn't yet support property hooks in interfaces
->ignoreDotFiles(true)
->ignoreVCS(true);

Expand Down
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Connect implements Route
final readonly class Connect implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::CONNECT;
$this->middleware = $middleware;
}
}
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Delete implements Route
final readonly class Delete implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::DELETE;
$this->middleware = $middleware;
}
}
2 changes: 1 addition & 1 deletion src/Tempest/Router/src/GenericRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function toUri(array|string $action, ...$params): string
/** @var Route|null $routeAttribute */
$routeAttribute = $controllerMethod->getAttribute(Route::class);

$uri = $routeAttribute->uri();
$uri = $routeAttribute->uri;
} catch (ReflectionException) {
if (is_array($action)) {
throw new InvalidRouteException($action[0], $action[1]);
Expand Down
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Get implements Route
final readonly class Get implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::GET;
$this->middleware = $middleware;
}
}
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Head implements Route
final readonly class Head implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::HEAD;
$this->middleware = $middleware;
}
}
36 changes: 0 additions & 36 deletions src/Tempest/Router/src/IsRoute.php

This file was deleted.

10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Options implements Route
final readonly class Options implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::OPTIONS;
$this->middleware = $middleware;
}
}
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Patch implements Route
final readonly class Patch implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::PATCH;
$this->middleware = $middleware;
}
}
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Post implements Route
final readonly class Post implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::POST;
$this->middleware = $middleware;
}
}
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Put implements Route
final readonly class Put implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::PUT;
$this->middleware = $middleware;
}
}
8 changes: 4 additions & 4 deletions src/Tempest/Router/src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

interface Route
{
public function method(): Method;
public Method $method { get; }

public function uri(): string;
public string $uri { get; }

/** @return class-string<HttpMiddleware>[] */
public function middleware(): array;
/** @var HttpMiddleware[] */
public array $middleware { get; }
}
23 changes: 4 additions & 19 deletions src/Tempest/Router/src/Routing/Construction/DiscoveredRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ final class DiscoveredRoute implements Route
public static function fromRoute(Route $route, MethodReflector $methodReflector): self
{
return new self(
$route->uri(),
$route->method(),
self::getRouteParams($route->uri()),
$route->middleware(),
$route->uri,
$route->method,
self::getRouteParams($route->uri),
$route->middleware,
$methodReflector,
);
}
Expand Down Expand Up @@ -63,19 +63,4 @@ public function split(): array
array_filter($parts, static fn (string $part) => $part !== ''),
);
}

public function method(): Method
{
return $this->method;
}

public function uri(): string
{
return $this->uri;
}

public function middleware(): array
{
return $this->middleware;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ final class DuplicateRouteException extends InvalidArgumentException
{
public function __construct(Route $route)
{
parent::__construct("Route '{$route->uri()}' already exists.");
parent::__construct("Route '{$route->uri}' already exists.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Tempest\Router\Routing\Construction;

use Tempest\Container\Singleton;
use Tempest\Router\Route;
use Tempest\Router\RouteConfig;

/**
Expand Down
10 changes: 4 additions & 6 deletions src/Tempest/Router/src/Trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Tempest\Http\Method;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Trace implements Route
final readonly class Trace implements Route
{
use IsRoute;
public Method $method;

/**
* @param class-string<HttpMiddleware>[] $middleware
*/
public function __construct(
string $uri,
array $middleware = [],
public string $uri,
public array $middleware = [],
) {
$this->uri = $uri;
$this->method = Method::TRACE;
$this->middleware = $middleware;
}
}
16 changes: 1 addition & 15 deletions src/Tempest/Router/tests/FakeRouteBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public function __construct(
public Method $method = Method::GET,
public string $uri = '/',
public array $middleware = [],
) {
$this->handler = new MethodReflector(new ReflectionMethod($this, 'handler'));
}
Expand All @@ -42,21 +43,6 @@ public function asDiscoveredRoute(): DiscoveredRoute
return DiscoveredRoute::fromRoute($this, $this->handler);
}

public function method(): Method
{
return $this->method;
}

public function uri(): string
{
return $this->uri;
}

public function middleware(): array
{
return [];
}

public function handler(): void
{
}
Expand Down

0 comments on commit 88bd85d

Please sign in to comment.