Added abstract RouteHandler and documentation.
This commit is contained in:
parent
32254bf398
commit
0710597062
6 changed files with 50 additions and 5 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.2309.72158
|
0.2309.72221
|
||||||
|
|
|
@ -1,10 +1,18 @@
|
||||||
<?php
|
<?php
|
||||||
// IRouteHandler.php
|
// IRouteHandler.php
|
||||||
// Created: 2023-09-06
|
// Created: 2023-09-06
|
||||||
// Updated: 2023-09-06
|
// Updated: 2023-09-07
|
||||||
|
|
||||||
namespace Index\Routing;
|
namespace Index\Routing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the interface for IRouter::register().
|
||||||
|
*/
|
||||||
interface IRouteHandler {
|
interface IRouteHandler {
|
||||||
|
/**
|
||||||
|
* Registers routes on a given IRouter instance.
|
||||||
|
*
|
||||||
|
* @param IRouter $router Target router.
|
||||||
|
*/
|
||||||
public function registerRoutes(IRouter $router): void;
|
public function registerRoutes(IRouter $router): void;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,21 +9,41 @@ use Attribute;
|
||||||
use ReflectionObject;
|
use ReflectionObject;
|
||||||
use UnexpectedValueException;
|
use UnexpectedValueException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an attribute for marking methods in a class as routes.
|
||||||
|
*/
|
||||||
#[Attribute]
|
#[Attribute]
|
||||||
class Route {
|
class Route {
|
||||||
|
/** @internal */
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private string $method,
|
private string $method,
|
||||||
private string $path
|
private string $path
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the target method name.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getMethod(): string {
|
public function getMethod(): string {
|
||||||
return $this->method;
|
return $this->method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the target path.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getPath(): string {
|
public function getPath(): string {
|
||||||
return $this->path;
|
return $this->path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads attributes from methods in a IRouteHandler instance and registers them to a given IRouter instance.
|
||||||
|
*
|
||||||
|
* @param IRouter $router Router instance.
|
||||||
|
* @param IRouteHandler $handler Handler instance.
|
||||||
|
*/
|
||||||
public static function handleAttributes(IRouter $router, IRouteHandler $handler): void {
|
public static function handleAttributes(IRouter $router, IRouteHandler $handler): void {
|
||||||
$objectInfo = new ReflectionObject($handler);
|
$objectInfo = new ReflectionObject($handler);
|
||||||
$methodInfos = $objectInfo->getMethods();
|
$methodInfos = $objectInfo->getMethods();
|
||||||
|
|
14
src/Routing/RouteHandler.php
Normal file
14
src/Routing/RouteHandler.php
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
// RouteHandler.php
|
||||||
|
// Created: 2023-09-07
|
||||||
|
// Updated: 2023-09-07
|
||||||
|
|
||||||
|
namespace Index\Routing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an abstract class version of IRouteHandler that already includes the trait as well,
|
||||||
|
* letting you only have to use one use statement rather than two!
|
||||||
|
*/
|
||||||
|
abstract class RouteHandler implements IRouteHandler {
|
||||||
|
use RouteHandlerTrait;
|
||||||
|
}
|
|
@ -5,6 +5,10 @@
|
||||||
|
|
||||||
namespace Index\Routing;
|
namespace Index\Routing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an implementation of IRouteHandler::registerRoutes that uses the attributes.
|
||||||
|
* For more advanced use, everything can be use'd separately and Route::handleAttributes called manually.
|
||||||
|
*/
|
||||||
trait RouteHandlerTrait {
|
trait RouteHandlerTrait {
|
||||||
public function registerRoutes(IRouter $router): void {
|
public function registerRoutes(IRouter $router): void {
|
||||||
Route::handleAttributes($router, $this);
|
Route::handleAttributes($router, $this);
|
||||||
|
|
|
@ -8,6 +8,7 @@ declare(strict_types=1);
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Index\Routing\IRouteHandler;
|
use Index\Routing\IRouteHandler;
|
||||||
use Index\Routing\Route;
|
use Index\Routing\Route;
|
||||||
|
use Index\Routing\RouteHandler;
|
||||||
use Index\Routing\RouteHandlerTrait;
|
use Index\Routing\RouteHandlerTrait;
|
||||||
use Index\Routing\Router;
|
use Index\Routing\Router;
|
||||||
|
|
||||||
|
@ -145,9 +146,7 @@ final class RouterTest extends TestCase {
|
||||||
$this->assertEquals('avatar', $router->resolve('POST', '/avatar')->run());
|
$this->assertEquals('avatar', $router->resolve('POST', '/avatar')->run());
|
||||||
$this->assertEquals('static', $router->resolve('PUT', '/static')->run());
|
$this->assertEquals('static', $router->resolve('PUT', '/static')->run());
|
||||||
|
|
||||||
$badHandler = new class implements IRouteHandler {
|
$badHandler = new class extends RouteHandler {
|
||||||
use RouteHandlerTrait;
|
|
||||||
|
|
||||||
#[Route('GET', '/')]
|
#[Route('GET', '/')]
|
||||||
#[Route('POST', '/meow')]
|
#[Route('POST', '/meow')]
|
||||||
public function getPostBad() {
|
public function getPostBad() {
|
||||||
|
|
Loading…
Reference in a new issue