diff --git a/README.md b/README.md index c6a056e..d686ba7 100755 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/Router.php b/src/Router.php index d814154..3e0380e 100755 --- a/src/Router.php +++ b/src/Router.php @@ -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(); @@ -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"; @@ -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 { @@ -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); } } } diff --git a/src/Traits/RouterAnnotationTrait.php b/src/Traits/RouterAnnotationTrait.php index 96c1c17..cb22800 100755 --- a/src/Traits/RouterAnnotationTrait.php +++ b/src/Traits/RouterAnnotationTrait.php @@ -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, @@ -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 { diff --git a/tests/RouterAnnotationTraitTest.php b/tests/RouterAnnotationTraitTest.php index 378b6b7..9557a41 100644 --- a/tests/RouterAnnotationTraitTest.php +++ b/tests/RouterAnnotationTraitTest.php @@ -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]); @@ -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 @@ -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']); diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 1297cab..9264ce8 100755 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -23,64 +23,64 @@ 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( @@ -88,13 +88,13 @@ protected function setRouteResourceEnvironment( 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'] ); @@ -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'] ); @@ -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')); } } diff --git a/tests/Stub/Controllers/AnnotatedController.php b/tests/Stub/Controllers/AnnotatedController.php index 9d037ca..cb46779 100644 --- a/tests/Stub/Controllers/AnnotatedController.php +++ b/tests/Stub/Controllers/AnnotatedController.php @@ -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() diff --git a/tests/Stub/Controllers/MainController.php b/tests/Stub/Controllers/MainController.php index e84475b..ac76be9 100755 --- a/tests/Stub/Controllers/MainController.php +++ b/tests/Stub/Controllers/MainController.php @@ -17,68 +17,68 @@ class MainController { public function actionIndex() { - Rudra::config()->set(["actionIndex" => "actionIndex"]); + Rudra::config()->set(['actionIndex' => 'actionIndex']); } public function actionGet() { - Rudra::config()->set(["actionGet" => "GET"]); + Rudra::config()->set(['actionGet' => 'GET']); } public function actionPost() { - Rudra::config()->set(["actionPost" => "POST"]); + Rudra::config()->set(['actionPost' => 'POST']); } public function actionPut() { - Rudra::config()->set(["actionPut" => "PUT"]); + Rudra::config()->set(['actionPut' => 'PUT']); } public function actionPatch() { - Rudra::config()->set(["actionPatch" => "PATCH"]); + Rudra::config()->set(['actionPatch' => 'PATCH']); } public function actionDelete() { - Rudra::config()->set(["actionDelete" => "DELETE"]); + Rudra::config()->set(['actionDelete' => 'DELETE']); } public function actionAny() { - Rudra::config()->set(["actionAny" => "ANY"]); + Rudra::config()->set(['actionAny' => 'ANY']); } public function index() { - Rudra::config()->set(["index" => "index"]); + Rudra::config()->set(['index' => 'index']); } public function read($params = null) { - Rudra::config()->set(["read" => "read"]); + Rudra::config()->set(['read' => 'read']); } public function create() { - Rudra::config()->set(["create" => "create"]); + Rudra::config()->set(['create' => 'create']); } public function update($params) { - Rudra::config()->set(["update" => "update"]); + Rudra::config()->set(['update' => 'update']); } public function delete($params) { - Rudra::config()->set(["delete" => "delete"]); + Rudra::config()->set(['delete' => 'delete']); } public function actionRegexGet() { - Rudra::config()->set(["regex" => "regex"]); + Rudra::config()->set(['regex' => 'regex']); } public function shipInit() diff --git a/tests/Stub/Middleware/Middleware.php b/tests/Stub/Middleware/Middleware.php index 03201f1..31d8426 100755 --- a/tests/Stub/Middleware/Middleware.php +++ b/tests/Stub/Middleware/Middleware.php @@ -17,6 +17,6 @@ class Middleware { public function __invoke() { - Rudra::config()->set(["middleware" => Middleware::class]); + Rudra::config()->set(['middleware' => Middleware::class]); } }