Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ use Rudra\Router\Attributes\Routing;
use Rudra\Router\Attributes\Middleware;

#[Routing(url: 'admin/:id', method: 'GET')]
#[Middleware(name: "Auth", params: ["admin"])]
#[Middleware(name: 'Auth', params: ['admin'])]
public function show(int $id): void
{
// Only accessible for authenticated admins
Expand Down Expand Up @@ -364,8 +364,8 @@ The router automatically recognizes this as a PUT request.

## Error Handling

- `RouterException("Not Found", 404)` — route not found or parameters do not match
- `RouterException("Service Unavailable", 503)` — controller method does not exist
- `RouterException('Not Found', 404)` — route not found or parameters do not match
- `RouterException('Service Unavailable', 503)` — controller method does not exist
- `MiddlewareException` — error in the middleware chain

## License
Expand Down
8 changes: 4 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function directCall(array $route, ?array $params = null): void
$action = $route['action'];

if (!method_exists($controller, $action)) {
throw new RouterException("Service Unavailable", 503);
throw new RouterException('Service Unavailable', 503);
}

$controller->shipInit();
Expand Down Expand Up @@ -207,7 +207,7 @@ public function directCall(array $route, ?array $params = null): void
private function callActionThroughReflection(?array $params, string $action, object $controller): void
{
if ($params && in_array('', $params, true)) {
throw new RouterException("Not Found", 404);
throw new RouterException('Not Found', 404);
}

$cacheKey = get_class($controller) . "::$action";
Expand Down Expand Up @@ -242,7 +242,7 @@ private function callActionThroughReflection(?array $params, string $action, obj
private function callActionThroughException(?array $params, string $action, object $controller): void
{
if (isset($params) && in_array('', $params)) {
throw new RouterException("Not Found", 404);
throw new RouterException('Not Found', 404);
}

try {
Expand Down Expand Up @@ -307,7 +307,7 @@ public function handleMiddleware(array $chain): void

throw new MiddlewareException('Invalid middleware format');
} catch (\Throwable $e) {
throw new MiddlewareException("Failed to process middleware: " . json_encode($current), 0, $e);
throw new MiddlewareException('Failed to process middleware: ' . json_encode($current), 0, $e);
}
}
}
14 changes: 7 additions & 7 deletions src/Traits/RouterAnnotationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public function annotationCollector(array $controllers, bool $getter = false, bo

$middleware = [];

if (isset($annotation["Middleware"])) {
$middleware['before'] = $this->handleAnnotationMiddleware($annotation["Middleware"]);
if (isset($annotation['Middleware'])) {
$middleware['before'] = $this->handleAnnotationMiddleware($annotation['Middleware']);
}

if (isset($annotation["AfterMiddleware"])) {
$middleware['after'] = $this->handleAnnotationMiddleware($annotation["AfterMiddleware"]);
if (isset($annotation['AfterMiddleware'])) {
$middleware['after'] = $this->handleAnnotationMiddleware($annotation['AfterMiddleware']);
}

if (isset($annotation["Routing"])) {
foreach ($annotation["Routing"] as $route) {
if (isset($annotation['Routing'])) {
foreach ($annotation['Routing'] as $route) {
$route += [
'controller' => $controller,
'action' => $action,
Expand Down Expand Up @@ -117,7 +117,7 @@ protected function expandOptionalSegments(string $url): array

/**
* Processes middleware annotations into a valid middleware format.
* #[Middleware(name: "Auth", params: "admin")] to: ['Auth', 'admin']
* #[Middleware(name: 'Auth', params: 'admin')] to: ['Auth', 'admin']
*/
protected function handleAnnotationMiddleware(array $annotation): array
{
Expand Down
8 changes: 4 additions & 4 deletions tests/RouterAnnotationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected function setUp(): void
{
parent::setUp();

$_SERVER["REQUEST_URI"] = "test/123";
$_SERVER["REQUEST_METHOD"] = "GET";
$_SERVER['REQUEST_URI'] = 'test/123';
$_SERVER['REQUEST_METHOD'] = 'GET';

Rudra::binding([RudraInterface::class => Rudra::run()]);
Rudra::set([Annotation::class, Annotation::class]);
Expand All @@ -36,7 +36,7 @@ protected function setUp(): void
protected function tearDown(): void
{
parent::tearDown();
unset($_SERVER["REQUEST_URI"], $_SERVER["REQUEST_METHOD"]);
unset($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
}

private function getRouter(): Router
Expand Down Expand Up @@ -112,7 +112,7 @@ public function testAnnotationCollectorWithRegistrationMode(): void
public function testAnnotationCollectorWithInvalidController(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Remove the InvalidController controller from the routes.php file.");
$this->expectExceptionMessage('Remove the InvalidController controller from the routes.php file.');

$router = $this->getRouter();
$router->annotationCollector(['InvalidController']);
Expand Down
86 changes: 43 additions & 43 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,78 +23,78 @@ protected function setContainer()
{
R::$rudra = null;
Rudra::binding()->set([RudraInterface::class => Rudra::run()]);
Rudra::config()->set(["environment" => "test"]);
Rudra::config()->set(['environment' => 'test']);
}

protected function setRouteEnvironment(string $requestUri, string $requestMethod, string $pattern, string $controller = MainController::class): void
{
$_SERVER["REQUEST_URI"] = $requestUri;
$_SERVER["REQUEST_METHOD"] = $requestMethod;
$_SERVER['REQUEST_URI'] = $requestUri;
$_SERVER['REQUEST_METHOD'] = $requestMethod;
$method = strtolower($requestMethod);
$action = "action" . ucfirst($method);
$action = 'action' . ucfirst($method);
$this->setContainer();

\Rudra\Router\RouterFacade::$method($pattern, [$controller, "action" . ucfirst($method)]);
\Rudra\Router\RouterFacade::$method($pattern, [$controller, 'action' . ucfirst($method)]);
$this->assertEquals($requestMethod, Rudra::config()->get($action));
}

public function testGetWithFullQualifiedNamespace()
{
$this->setRouteEnvironment(
"test/page",
"GET",
"/test/page",
'test/page',
'GET',
'/test/page',
MainController::class
);
}

public function testGet(): void
{
$this->setRouteEnvironment("test/page?id=98", "GET", "/test/page");
$this->setRouteEnvironment('test/page?id=98', 'GET', '/test/page');
}

public function testPost(): void
{
$this->setRouteEnvironment("test/page?some=123", "POST", "/test/page");
$this->setRouteEnvironment('test/page?some=123', 'POST', '/test/page');
}

public function testPut(): void
{
$this->setRouteEnvironment("test/page", 'PUT', "/test/page");
$this->setRouteEnvironment('test/page', 'PUT', '/test/page');
}

public function testPatch(): void
{
$this->setRouteEnvironment("test/page", 'PATCH', "/test/page");
$this->setRouteEnvironment('test/page', 'PATCH', '/test/page');
}

public function testDelete(): void
{
$this->setRouteEnvironment("test/page", 'DELETE', "/test/page");
$this->setRouteEnvironment('test/page', 'DELETE', '/test/page');
}

public function testAny(): void
{
$_SERVER["REQUEST_URI"] = "test/page";
$_SERVER["REQUEST_METHOD"] = "PATCH";
$_SERVER['REQUEST_URI'] = 'test/page';
$_SERVER['REQUEST_METHOD'] = 'PATCH';
$this->setContainer();

Router::any('/test/page',[MainController::class, 'actionAny']);
$this->assertEquals("ANY", Rudra::config()->get("actionAny"));
$this->assertEquals('ANY', Rudra::config()->get('actionAny'));
}

protected function setRouteResourceEnvironment(
string $requestMethod,
string $action,
string $requestUri
): void {
$_SERVER["REQUEST_URI"] = $requestUri;
$_SERVER["REQUEST_METHOD"] = $requestMethod;
$_SERVER['REQUEST_URI'] = $requestUri;
$_SERVER['REQUEST_METHOD'] = $requestMethod;
$this->setContainer();

Router::resource(
"api/users",
"api/user",
'api/users',
'api/user',
MainController::class,
['index', 'read', 'create', 'update', 'delete']
);
Expand All @@ -105,26 +105,26 @@ protected function setRouteResourceEnvironment(
public function testResource(): void
{
// Collection routes (plural URL, no :id)
$this->setRouteResourceEnvironment("GET", "index", "api/users");
$this->setRouteResourceEnvironment("POST", "create", "api/users");
$this->setRouteResourceEnvironment('GET', 'index', 'api/users');
$this->setRouteResourceEnvironment('POST', 'create', 'api/users');

// Single-item routes (singular URL + /:id)
$this->setRouteResourceEnvironment("GET", "read", "api/user/123");
$this->setRouteResourceEnvironment("PUT", "update", "api/user/123");
$this->setRouteResourceEnvironment("PATCH", "update", "api/user/123");
$this->setRouteResourceEnvironment("DELETE", "delete", "api/user/123");
$this->setRouteResourceEnvironment('GET', 'read', 'api/user/123');
$this->setRouteResourceEnvironment('PUT', 'update', 'api/user/123');
$this->setRouteResourceEnvironment('PATCH', 'update', 'api/user/123');
$this->setRouteResourceEnvironment('DELETE', 'delete', 'api/user/123');
}

protected function setRouteResourcePostEnvironment(string $spoofedMethod, string $action): void
{
$_SERVER["REQUEST_URI"] = "api/user/123";
$_SERVER["REQUEST_METHOD"] = "POST";
$_POST["_method"] = $spoofedMethod;
$_SERVER['REQUEST_URI'] = 'api/user/123';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['_method'] = $spoofedMethod;
$this->setContainer();

Router::resource(
"api/users",
"api/user",
'api/users',
'api/user',
MainController::class,
['index', 'read', 'create', 'update', 'delete']
);
Expand All @@ -134,31 +134,31 @@ protected function setRouteResourcePostEnvironment(string $spoofedMethod, string

public function testResourcePost(): void
{
$this->setRouteResourcePostEnvironment("PUT", "update");
$this->setRouteResourcePostEnvironment("PATCH", "update");
$this->setRouteResourcePostEnvironment("DELETE", "delete");
$this->setRouteResourcePostEnvironment('PUT', 'update');
$this->setRouteResourcePostEnvironment('PATCH', 'update');
$this->setRouteResourcePostEnvironment('DELETE', 'delete');
}

public function testClosure()
{
$_SERVER["REQUEST_URI"] = "test/page";
$_SERVER["REQUEST_METHOD"] = "GET";
$_SERVER['REQUEST_URI'] = 'test/page';
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->setContainer();

Router::get("test/page", function () {
Rudra::config()->set(["closure" => "closure"]);
Router::get('test/page', function () {
Rudra::config()->set(['closure' => 'closure']);
});

$this->assertEquals("closure", Rudra::config()->get("closure"));
$this->assertEquals('closure', Rudra::config()->get('closure'));
}

public function testRegex(): void
{
$_SERVER["REQUEST_URI"] = "test/12";
$_SERVER["REQUEST_METHOD"] = "GET";
$_SERVER['REQUEST_URI'] = 'test/12';
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->setContainer();

Router::get("test/:[\d]{1,3}", [MainController::class, 'actionRegexGet']);
$this->assertEquals('regex', Rudra::config()->get("regex"));
Router::get('test/:[\d]{1,3}', [MainController::class, 'actionRegexGet']);
$this->assertEquals('regex', Rudra::config()->get('regex'));
}
}
18 changes: 9 additions & 9 deletions tests/Stub/Controllers/AnnotatedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@

class AnnotatedController
{
#[Routing(url: "annotated/index", method: "GET")]
#[Middleware(name: "AuthMiddleware", params: ["admin"])]
#[Routing(url: 'annotated/index', method: 'GET')]
#[Middleware(name: 'AuthMiddleware', params: ['admin'])]
public function actionIndex(): void
{
Rudra::config()->set(["annotatedIndex" => "annotatedIndex"]);
Rudra::config()->set(['annotatedIndex' => 'annotatedIndex']);
}

#[Routing(url: "annotated/edit/:id", method: "POST")]
#[Middleware(name: "AuthMiddleware")]
#[AfterMiddleware(name: "LogMiddleware", params: ["edit"])]
#[Routing(url: 'annotated/edit/:id', method: 'POST')]
#[Middleware(name: 'AuthMiddleware')]
#[AfterMiddleware(name: 'LogMiddleware', params: ['edit'])]
public function actionEdit(string $id): void
{
Rudra::config()->set(["annotatedEdit" => "annotatedEdit"]);
Rudra::config()->set(['annotatedEdit' => 'annotatedEdit']);
}

#[Routing(url: "annotated/view/:id", method: "GET")]
#[Routing(url: 'annotated/view/:id', method: 'GET')]
public function actionView(string $id): void
{
Rudra::config()->set(["annotatedView" => "annotatedView"]);
Rudra::config()->set(['annotatedView' => 'annotatedView']);
}

public function shipInit()
Expand Down
Loading
Loading