diff --git a/src/Reflector.php b/src/Reflector.php index 9385596..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()) { @@ -103,7 +113,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..f818f7c 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; @@ -70,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}', @@ -80,4 +81,39 @@ public function testWithOrigins() : void 'action' => UsersRouteActionsResource::class . '::delete', ], $reflector->getRoutes()); } + + public function testInChildClass() : void + { + $reflector = new Reflector(new ChildClass()); + self::assertContains([ + 'origins' => [ + 'http://bar.xyz', + ], + 'methods' => ['GET'], + 'path' => '/hello', + 'arguments' => '*', + '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', + ], + '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..e068c02 --- /dev/null +++ b/tests/Support/AbstractClass.php @@ -0,0 +1,32 @@ + + * + * 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 + { + } + + #[Route('GET', '/replace-origin', origin: 'xxx')] + public function replaceOrigin() : 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 + { + } +} 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