Skip to content

Commit aaad6cd

Browse files
committed
Allow set many origins in Route and RouteNotFound attributes
1 parent a1cef70 commit aaad6cd

File tree

7 files changed

+43
-25
lines changed

7 files changed

+43
-25
lines changed

src/Attributes/Route.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class Route
2626
protected string $path;
2727
protected string $arguments;
2828
protected ?string $name;
29-
protected ?string $origin;
29+
/**
30+
* @var array<string>
31+
*/
32+
protected array $origins;
3033

3134
/**
3235
* Route constructor.
@@ -35,14 +38,14 @@ class Route
3538
* @param string $path The Route path
3639
* @param string $arguments The Route action arguments
3740
* @param string|null $name The Route name
38-
* @param string|null $origin The Route origin
41+
* @param array<string>|string $origins The Route origins
3942
*/
4043
public function __construct(
4144
array | string $methods,
4245
string $path,
4346
string $arguments = '*',
4447
string $name = null,
45-
string $origin = null,
48+
array | string $origins = [],
4649
) {
4750
$methods = (array) $methods;
4851
foreach ($methods as &$method) {
@@ -53,7 +56,7 @@ public function __construct(
5356
$this->path = $path;
5457
$this->arguments = $arguments;
5558
$this->name = $name;
56-
$this->origin = $origin;
59+
$this->origins = (array) $origins;
5760
}
5861

5962
/**
@@ -89,10 +92,10 @@ public function getName() : ?string
8992
}
9093

9194
/**
92-
* @return string|null
95+
* @return array<string>
9396
*/
94-
public function getOrigin() : ?string
97+
public function getOrigins() : array
9598
{
96-
return $this->origin;
99+
return $this->origins;
97100
}
98101
}

src/Attributes/RouteNotFound.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@
1919
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2020
class RouteNotFound
2121
{
22-
protected ?string $origin;
22+
/**
23+
* @var array<string>
24+
*/
25+
protected array $origins;
2326

2427
/**
2528
* RouteNotFound constructor.
2629
*
27-
* @param string|null $origin The Route Not Found origin
30+
* @param array<string>|string $origins The Route Not Found origins
2831
*/
29-
public function __construct(string $origin = null)
32+
public function __construct(array | string $origins = [])
3033
{
31-
$this->origin = $origin;
34+
$this->origins = (array) $origins;
3235
}
3336

3437
/**
35-
* @return string|null
38+
* @return array<string>
3639
*/
37-
public function getOrigin() : ?string
40+
public function getOrigins() : array
3841
{
39-
return $this->origin;
42+
return $this->origins;
4043
}
4144
}

src/Reflector.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ public function getRoutes() : array
104104
continue;
105105
}
106106
foreach ($routes as $route) {
107-
$origin = $route->getOrigin();
108-
$origin = $origin === null ? $origins : [$origin];
109107
$result[] = [
110-
'origins' => $origin,
108+
'origins' => $route->getOrigins() ?: $origins,
111109
'methods' => $route->getMethods(),
112110
'path' => $route->getPath(),
113111
'arguments' => $route->getArguments(),
@@ -156,10 +154,8 @@ public function getRoutesNotFound() : array
156154
continue;
157155
}
158156
foreach ($routes as $route) {
159-
$origin = $route->getOrigin();
160-
$origin = $origin === null ? $origins : [$origin];
161157
$result[] = [
162-
'origins' => $origin,
158+
'origins' => $route->getOrigins() ?: $origins,
163159
'action' => $this->reflection->name . '::' . $method->name,
164160
];
165161
}

tests/Attributes/RouteTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ protected function assertAttribute(string $method, array $arguments) : void
4747
? self::assertSame('users.delete', $instance->getName())
4848
: self::assertNull($instance->getName());
4949
$method === 'index'
50-
? self::assertSame('http://foo.com', $instance->getOrigin())
51-
: self::assertNull($instance->getOrigin());
50+
? self::assertSame(['http://foo.com'], $instance->getOrigins())
51+
: self::assertEmpty($instance->getOrigins());
5252
}
5353

5454
public function testIndex() : void
5555
{
5656
$this->assertAttribute('index', [
5757
'GET',
5858
'/users',
59-
'origin' => 'http://foo.com',
59+
'origins' => 'http://foo.com',
6060
]);
6161
}
6262

tests/ReflectorTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ public function testInChildClass() : void
110110
'name' => null,
111111
'action' => ChildClass::class . '::replaceOrigin',
112112
], $reflector->getRoutes());
113+
self::assertContains([
114+
'origins' => [
115+
'xxx',
116+
'yyy',
117+
],
118+
'methods' => ['GET'],
119+
'path' => '/replace-origin-2',
120+
'arguments' => '*',
121+
'name' => null,
122+
'action' => ChildClass::class . '::replaceOrigin2',
123+
], $reflector->getRoutes());
113124
self::assertContains([
114125
'origins' => [
115126
'http://bar.xyz',

tests/Support/AbstractClass.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ public function hello() : void
2020
{
2121
}
2222

23-
#[Route('GET', '/replace-origin', origin: 'xxx')]
23+
#[Route('GET', '/replace-origin', origins: 'xxx')]
2424
public function replaceOrigin() : void
2525
{
2626
}
27+
28+
#[Route('GET', '/replace-origin-2', origins: ['xxx', 'yyy'])]
29+
public function replaceOrigin2() : void
30+
{
31+
}
2732
}

tests/Support/UsersRouteActionsResource.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#[Origin('http://api.domain.xyz')]
2020
class UsersRouteActionsResource extends RouteActions implements ResourceInterface
2121
{
22-
#[Route('GET', '/users', origin: 'http://foo.com')]
22+
#[Route('GET', '/users', origins: 'http://foo.com')]
2323
public function index() : string
2424
{
2525
return __METHOD__;

0 commit comments

Comments
 (0)