From 09f4457b68babd5e7f20658ab26c197b72e392e8 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Fri, 29 Jul 2022 17:38:33 -0300 Subject: [PATCH 1/3] Use current class name in the route action --- src/Reflector.php | 2 +- tests/ReflectorTest.php | 22 ++++++++++++++++++++++ tests/Support/AbstractClass.php | 27 +++++++++++++++++++++++++++ tests/Support/ChildClass.php | 25 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/Support/AbstractClass.php create mode 100644 tests/Support/ChildClass.php diff --git a/src/Reflector.php b/src/Reflector.php index 9385596..47bf5e7 100644 --- a/src/Reflector.php +++ b/src/Reflector.php @@ -103,7 +103,7 @@ public function getRoutes() : array 'path' => $route->getPath(), 'arguments' => $route->getArguments(), 'name' => $route->getName(), - 'action' => $method->class . '::' . $method->name, + 'action' => $this->reflection->name . '::' . $method->name, ]; } return $routes; diff --git a/tests/ReflectorTest.php b/tests/ReflectorTest.php index 42dc314..4b5f7d7 100644 --- a/tests/ReflectorTest.php +++ b/tests/ReflectorTest.php @@ -11,6 +11,7 @@ use Framework\Routing\Reflector; use PHPUnit\Framework\TestCase; +use Tests\Routing\Support\ChildClass; use Tests\Routing\Support\UsersRouteActionsPresenter; use Tests\Routing\Support\UsersRouteActionsResource; use Tests\Routing\Support\WithoutRouteActions; @@ -80,4 +81,25 @@ public function testWithOrigins() : void 'action' => UsersRouteActionsResource::class . '::delete', ], $reflector->getRoutes()); } + + public function testInChildClass() : void + { + $reflector = new Reflector(new ChildClass()); + self::assertContains([ + 'origins' => [], + 'methods' => ['GET'], + 'path' => '/hello', + 'arguments' => '*', + 'name' => null, + 'action' => ChildClass::class . '::hello', + ], $reflector->getRoutes()); + self::assertContains([ + 'origins' => [], + 'methods' => ['GET'], + 'path' => '/bye', + 'arguments' => '*', + 'name' => null, + 'action' => ChildClass::class . '::bye', + ], $reflector->getRoutes()); + } } diff --git a/tests/Support/AbstractClass.php b/tests/Support/AbstractClass.php new file mode 100644 index 0000000..1309a0b --- /dev/null +++ b/tests/Support/AbstractClass.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Tests\Routing\Support; + +use Framework\Routing\Attributes\Origin; +use Framework\Routing\Attributes\Route; + +/** + * Class AbstractClass. + * + * @package routing + */ +#[Origin('http://bar.xyz')] +abstract class AbstractClass +{ + #[Route('GET', '/hello')] + public function hello() : void + { + } +} diff --git a/tests/Support/ChildClass.php b/tests/Support/ChildClass.php new file mode 100644 index 0000000..a7d98ac --- /dev/null +++ b/tests/Support/ChildClass.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Tests\Routing\Support; + +use Framework\Routing\Attributes\Route; + +/** + * Class ChildClass. + * + * @package routing + */ +class ChildClass extends AbstractClass +{ + #[Route('GET', '/bye')] + public function bye() : void + { + } +} From 6b85c03ff4e9653f9eedfe9e1ddb349da34ba806 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Fri, 29 Jul 2022 18:05:12 -0300 Subject: [PATCH 2/3] Get origins from parent classes --- src/Reflector.php | 16 +++++++++++++--- tests/ReflectorTest.php | 10 +++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Reflector.php b/src/Reflector.php index 47bf5e7..584e655 100644 --- a/src/Reflector.php +++ b/src/Reflector.php @@ -61,12 +61,16 @@ protected function getMethodRoute(string $method) : ?Route } /** + * @template T of object + * + * @param ReflectionClass $reflection + * * @return array */ - protected function getObjectOrigins() : array + protected function getObjectOrigins(ReflectionClass $reflection) : array { $origins = []; - foreach ($this->reflection->getAttributes() as $attribute) { + foreach ($reflection->getAttributes() as $attribute) { if ($attribute->getName() === Origin::class) { /** * @var Origin $origin @@ -75,6 +79,12 @@ protected function getObjectOrigins() : array $origins[] = $origin->getOrigin(); } } + $parent = $reflection->getParentClass(); + if ($parent) { + $origins = [...$origins, ...$this->getObjectOrigins($parent)]; + } + $origins = \array_unique($origins); + \sort($origins); return $origins; } @@ -85,7 +95,7 @@ protected function getObjectOrigins() : array */ public function getRoutes() : array { - $origins = $this->getObjectOrigins(); + $origins = $this->getObjectOrigins($this->reflection); $routes = []; foreach ($this->reflection->getMethods() as $method) { if ( ! $method->isPublic()) { diff --git a/tests/ReflectorTest.php b/tests/ReflectorTest.php index 4b5f7d7..70eec11 100644 --- a/tests/ReflectorTest.php +++ b/tests/ReflectorTest.php @@ -71,8 +71,8 @@ public function testWithOrigins() : void ], $reflector->getRoutes()); self::assertContains([ 'origins' => [ - 'http://domain.com', 'http://api.domain.xyz', + 'http://domain.com', ], 'methods' => ['DELETE'], 'path' => '/users/{int}', @@ -86,7 +86,9 @@ public function testInChildClass() : void { $reflector = new Reflector(new ChildClass()); self::assertContains([ - 'origins' => [], + 'origins' => [ + 'http://bar.xyz', + ], 'methods' => ['GET'], 'path' => '/hello', 'arguments' => '*', @@ -94,7 +96,9 @@ public function testInChildClass() : void 'action' => ChildClass::class . '::hello', ], $reflector->getRoutes()); self::assertContains([ - 'origins' => [], + 'origins' => [ + 'http://bar.xyz', + ], 'methods' => ['GET'], 'path' => '/bye', 'arguments' => '*', From 11cb761cd9f42adf19a2eeddeb8a70fb5d57ba34 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Fri, 29 Jul 2022 18:07:33 -0300 Subject: [PATCH 3/3] Route attribute origin replace Origin attribute origins --- tests/ReflectorTest.php | 10 ++++++++++ tests/Support/AbstractClass.php | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/tests/ReflectorTest.php b/tests/ReflectorTest.php index 70eec11..f818f7c 100644 --- a/tests/ReflectorTest.php +++ b/tests/ReflectorTest.php @@ -95,6 +95,16 @@ public function testInChildClass() : void 'name' => null, 'action' => ChildClass::class . '::hello', ], $reflector->getRoutes()); + self::assertContains([ + 'origins' => [ + 'xxx', + ], + 'methods' => ['GET'], + 'path' => '/replace-origin', + 'arguments' => '*', + 'name' => null, + 'action' => ChildClass::class . '::replaceOrigin', + ], $reflector->getRoutes()); self::assertContains([ 'origins' => [ 'http://bar.xyz', diff --git a/tests/Support/AbstractClass.php b/tests/Support/AbstractClass.php index 1309a0b..e068c02 100644 --- a/tests/Support/AbstractClass.php +++ b/tests/Support/AbstractClass.php @@ -24,4 +24,9 @@ abstract class AbstractClass public function hello() : void { } + + #[Route('GET', '/replace-origin', origin: 'xxx')] + public function replaceOrigin() : void + { + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy