Skip to content

Commit

Permalink
Allow set many origins in Route and RouteNotFound attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Aug 1, 2022
1 parent a1cef70 commit aaad6cd
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 25 deletions.
17 changes: 10 additions & 7 deletions src/Attributes/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class Route
protected string $path;
protected string $arguments;
protected ?string $name;
protected ?string $origin;
/**
* @var array<string>
*/
protected array $origins;

/**
* Route constructor.
Expand All @@ -35,14 +38,14 @@ class Route
* @param string $path The Route path
* @param string $arguments The Route action arguments
* @param string|null $name The Route name
* @param string|null $origin The Route origin
* @param array<string>|string $origins The Route origins
*/
public function __construct(
array | string $methods,
string $path,
string $arguments = '*',
string $name = null,
string $origin = null,
array | string $origins = [],
) {
$methods = (array) $methods;
foreach ($methods as &$method) {
Expand All @@ -53,7 +56,7 @@ public function __construct(
$this->path = $path;
$this->arguments = $arguments;
$this->name = $name;
$this->origin = $origin;
$this->origins = (array) $origins;
}

/**
Expand Down Expand Up @@ -89,10 +92,10 @@ public function getName() : ?string
}

/**
* @return string|null
* @return array<string>
*/
public function getOrigin() : ?string
public function getOrigins() : array
{
return $this->origin;
return $this->origins;
}
}
17 changes: 10 additions & 7 deletions src/Attributes/RouteNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class RouteNotFound
{
protected ?string $origin;
/**
* @var array<string>
*/
protected array $origins;

/**
* RouteNotFound constructor.
*
* @param string|null $origin The Route Not Found origin
* @param array<string>|string $origins The Route Not Found origins
*/
public function __construct(string $origin = null)
public function __construct(array | string $origins = [])
{
$this->origin = $origin;
$this->origins = (array) $origins;
}

/**
* @return string|null
* @return array<string>
*/
public function getOrigin() : ?string
public function getOrigins() : array
{
return $this->origin;
return $this->origins;
}
}
8 changes: 2 additions & 6 deletions src/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ public function getRoutes() : array
continue;
}
foreach ($routes as $route) {
$origin = $route->getOrigin();
$origin = $origin === null ? $origins : [$origin];
$result[] = [
'origins' => $origin,
'origins' => $route->getOrigins() ?: $origins,
'methods' => $route->getMethods(),
'path' => $route->getPath(),
'arguments' => $route->getArguments(),
Expand Down Expand Up @@ -156,10 +154,8 @@ public function getRoutesNotFound() : array
continue;
}
foreach ($routes as $route) {
$origin = $route->getOrigin();
$origin = $origin === null ? $origins : [$origin];
$result[] = [
'origins' => $origin,
'origins' => $route->getOrigins() ?: $origins,
'action' => $this->reflection->name . '::' . $method->name,
];
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Attributes/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ protected function assertAttribute(string $method, array $arguments) : void
? self::assertSame('users.delete', $instance->getName())
: self::assertNull($instance->getName());
$method === 'index'
? self::assertSame('http://foo.com', $instance->getOrigin())
: self::assertNull($instance->getOrigin());
? self::assertSame(['http://foo.com'], $instance->getOrigins())
: self::assertEmpty($instance->getOrigins());
}

public function testIndex() : void
{
$this->assertAttribute('index', [
'GET',
'/users',
'origin' => 'http://foo.com',
'origins' => 'http://foo.com',
]);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ReflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ public function testInChildClass() : void
'name' => null,
'action' => ChildClass::class . '::replaceOrigin',
], $reflector->getRoutes());
self::assertContains([
'origins' => [
'xxx',
'yyy',
],
'methods' => ['GET'],
'path' => '/replace-origin-2',
'arguments' => '*',
'name' => null,
'action' => ChildClass::class . '::replaceOrigin2',
], $reflector->getRoutes());
self::assertContains([
'origins' => [
'http://bar.xyz',
Expand Down
7 changes: 6 additions & 1 deletion tests/Support/AbstractClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ public function hello() : void
{
}

#[Route('GET', '/replace-origin', origin: 'xxx')]
#[Route('GET', '/replace-origin', origins: 'xxx')]
public function replaceOrigin() : void
{
}

#[Route('GET', '/replace-origin-2', origins: ['xxx', 'yyy'])]
public function replaceOrigin2() : void
{
}
}
2 changes: 1 addition & 1 deletion tests/Support/UsersRouteActionsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#[Origin('http://api.domain.xyz')]
class UsersRouteActionsResource extends RouteActions implements ResourceInterface
{
#[Route('GET', '/users', origin: 'http://foo.com')]
#[Route('GET', '/users', origins: 'http://foo.com')]
public function index() : string
{
return __METHOD__;
Expand Down

0 comments on commit aaad6cd

Please sign in to comment.