Skip to content

Commit fc35ef3

Browse files
authored
Merge pull request #80 from sunrise-php/release/v2.11.0
v2.11.0
2 parents 07b0201 + c168513 commit fc35ef3

26 files changed

+754
-245
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
## Installation
1313

1414
```bash
15-
composer require 'sunrise/http-router:^2.10'
15+
composer require 'sunrise/http-router:^2.11'
1616
```
1717

1818
## Support for OpenAPI (Swagger) Specification (optional)
@@ -208,6 +208,25 @@ $response = $router->handle($request);
208208
$response = $router->process($request, $handler);
209209
```
210210

211+
```php
212+
use Sunrise\Http\Router\Annotation as Mapping;
213+
214+
#[Mapping\Prefix('/api/v1')]
215+
#[Mapping\Middleware(SomeMiddleware::class)]
216+
class SomeController {
217+
218+
#[Mapping\Route('foo', path: '/foo')]
219+
public function foo() {
220+
// will be available at: /api/v1/foo
221+
}
222+
223+
#[Mapping\Route('bar', path: '/bar')]
224+
public function bar() {
225+
// will be available at: /api/v1/bar
226+
}
227+
}
228+
```
229+
211230
#### Without loading strategy
212231

213232
```php

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
"test": [
6565
"phpcs",
6666
"XDEBUG_MODE=coverage phpunit --coverage-text"
67+
],
68+
"build": [
69+
"phpdoc -d src/ -t phpdoc/",
70+
"XDEBUG_MODE=coverage phpunit --coverage-html coverage/"
6771
]
6872
}
6973
}

src/Annotation/Host.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* It's free open-source software released under the MIT License.
5+
*
6+
* @author Anatoly Fenric <[email protected]>
7+
* @copyright Copyright (c) 2018, Anatoly Fenric
8+
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9+
* @link https://github.com/sunrise-php/http-router
10+
*/
11+
12+
namespace Sunrise\Http\Router\Annotation;
13+
14+
/**
15+
* Import classes
16+
*/
17+
use Attribute;
18+
19+
/**
20+
* @Annotation
21+
*
22+
* @Target({"CLASS", "METHOD"})
23+
*
24+
* @NamedArgumentConstructor
25+
*
26+
* @Attributes({
27+
* @Attribute("value", type="string", required=true),
28+
* })
29+
*
30+
* @since 2.11.0
31+
*/
32+
#[Attribute(Attribute::TARGET_CLASS|Attribute::TARGET_METHOD)]
33+
final class Host
34+
{
35+
36+
/**
37+
* The attribute value
38+
*
39+
* @var string
40+
*/
41+
public $value;
42+
43+
/**
44+
* Constructor of the class
45+
*
46+
* @param string $value
47+
*/
48+
public function __construct(string $value)
49+
{
50+
$this->value = $value;
51+
}
52+
}

src/Annotation/Middleware.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* It's free open-source software released under the MIT License.
5+
*
6+
* @author Anatoly Fenric <[email protected]>
7+
* @copyright Copyright (c) 2018, Anatoly Fenric
8+
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9+
* @link https://github.com/sunrise-php/http-router
10+
*/
11+
12+
namespace Sunrise\Http\Router\Annotation;
13+
14+
/**
15+
* Import classes
16+
*/
17+
use Attribute;
18+
19+
/**
20+
* @Annotation
21+
*
22+
* @Target({"CLASS", "METHOD"})
23+
*
24+
* @NamedArgumentConstructor
25+
*
26+
* @Attributes({
27+
* @Attribute("value", type="string", required=true),
28+
* })
29+
*
30+
* @since 2.11.0
31+
*/
32+
#[Attribute(Attribute::TARGET_CLASS|Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)]
33+
final class Middleware
34+
{
35+
36+
/**
37+
* The attribute value
38+
*
39+
* @var string
40+
*/
41+
public $value;
42+
43+
/**
44+
* Constructor of the class
45+
*
46+
* @param string $value
47+
*/
48+
public function __construct(string $value)
49+
{
50+
$this->value = $value;
51+
}
52+
}

src/Annotation/Postfix.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* It's free open-source software released under the MIT License.
5+
*
6+
* @author Anatoly Fenric <[email protected]>
7+
* @copyright Copyright (c) 2018, Anatoly Fenric
8+
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9+
* @link https://github.com/sunrise-php/http-router
10+
*/
11+
12+
namespace Sunrise\Http\Router\Annotation;
13+
14+
/**
15+
* Import classes
16+
*/
17+
use Attribute;
18+
19+
/**
20+
* @Annotation
21+
*
22+
* @Target({"CLASS", "METHOD"})
23+
*
24+
* @NamedArgumentConstructor
25+
*
26+
* @Attributes({
27+
* @Attribute("value", type="string", required=true),
28+
* })
29+
*
30+
* @since 2.11.0
31+
*/
32+
#[Attribute(Attribute::TARGET_CLASS|Attribute::TARGET_METHOD)]
33+
final class Postfix
34+
{
35+
36+
/**
37+
* The attribute value
38+
*
39+
* @var string
40+
*/
41+
public $value;
42+
43+
/**
44+
* Constructor of the class
45+
*
46+
* @param string $value
47+
*/
48+
public function __construct(string $value)
49+
{
50+
$this->value = $value;
51+
}
52+
}

src/Annotation/Prefix.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* It's free open-source software released under the MIT License.
5+
*
6+
* @author Anatoly Fenric <[email protected]>
7+
* @copyright Copyright (c) 2018, Anatoly Fenric
8+
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9+
* @link https://github.com/sunrise-php/http-router
10+
*/
11+
12+
namespace Sunrise\Http\Router\Annotation;
13+
14+
/**
15+
* Import classes
16+
*/
17+
use Attribute;
18+
19+
/**
20+
* @Annotation
21+
*
22+
* @Target({"CLASS", "METHOD"})
23+
*
24+
* @NamedArgumentConstructor
25+
*
26+
* @Attributes({
27+
* @Attribute("value", type="string", required=true),
28+
* })
29+
*
30+
* @since 2.11.0
31+
*/
32+
#[Attribute(Attribute::TARGET_CLASS|Attribute::TARGET_METHOD)]
33+
final class Prefix
34+
{
35+
36+
/**
37+
* The attribute value
38+
*
39+
* @var string
40+
*/
41+
public $value;
42+
43+
/**
44+
* Constructor of the class
45+
*
46+
* @param string $value
47+
*/
48+
public function __construct(string $value)
49+
{
50+
$this->value = $value;
51+
}
52+
}

src/Annotation/Route.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ final class Route
4444
/**
4545
* The descriptor holder
4646
*
47-
* Don't use the property outside of the package.
48-
*
4947
* @var mixed
5048
*
5149
* @internal
@@ -154,6 +152,12 @@ public function __construct(
154152
$methods[] = $method;
155153
}
156154

155+
// if no methods are specified,
156+
// such a route is a GET route.
157+
if (empty($methods)) {
158+
$methods[] = 'GET';
159+
}
160+
157161
$this->name = $name;
158162
$this->host = $host;
159163
$this->path = $path;

src/Command/RouteListCommand.php

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/**
1515
* Import classes
1616
*/
17+
use RuntimeException;
1718
use Sunrise\Http\Router\Router;
1819
use Symfony\Component\Console\Command\Command;
1920
use Symfony\Component\Console\Helper\Table;
@@ -23,39 +24,79 @@
2324
/**
2425
* Import functions
2526
*/
26-
use function Sunrise\Http\Router\path_plain;
2727
use function join;
28+
use function sprintf;
29+
use function Sunrise\Http\Router\path_plain;
2830

2931
/**
30-
* RouteListCommand
32+
* This command will list all routes in your application
33+
*
34+
* If you cannot pass the router to the constructor
35+
* or your architecture has problems with autowiring,
36+
* then just inherit this class and override the getRouter method.
3137
*
3238
* @since 2.9.0
3339
*/
34-
final class RouteListCommand extends Command
40+
class RouteListCommand extends Command
3541
{
3642

3743
/**
38-
* @var Router
44+
* {@inheritdoc}
3945
*/
40-
private $router;
46+
protected static $defaultName = 'router:route-list';
4147

4248
/**
4349
* {@inheritdoc}
50+
*/
51+
protected static $defaultDescription = 'Lists all routes in your application';
52+
53+
/**
54+
* The router instance populated with routes
4455
*
45-
* @param Router $router
46-
* @param string|null $name
56+
* @var Router|null
4757
*/
48-
public function __construct(Router $router, ?string $name = null)
58+
private $router;
59+
60+
/**
61+
* Constructor of the class
62+
*
63+
* @param Router|null $router
64+
*/
65+
public function __construct(?Router $router = null)
4966
{
67+
parent::__construct();
68+
5069
$this->router = $router;
70+
}
71+
72+
/**
73+
* Gets the router instance populated with routes
74+
*
75+
* @return Router
76+
*
77+
* @throws RuntimeException
78+
* If the command doesn't contain the router instance.
79+
*
80+
* @since 2.11.0
81+
*/
82+
protected function getRouter() : Router
83+
{
84+
if (null === $this->router) {
85+
throw new RuntimeException(sprintf(
86+
'The %2$s() method MUST return the %1$s class instance. ' .
87+
'Pass the %1$s class instance to the constructor, or override the %2$s() method.',
88+
Router::class,
89+
__METHOD__
90+
));
91+
}
5192

52-
parent::__construct($name ?? 'router:route-list');
93+
return $this->router;
5394
}
5495

5596
/**
5697
* {@inheritdoc}
5798
*/
58-
public function execute(InputInterface $input, OutputInterface $output) : int
99+
final protected function execute(InputInterface $input, OutputInterface $output) : int
59100
{
60101
$table = new Table($output);
61102

@@ -66,7 +107,7 @@ public function execute(InputInterface $input, OutputInterface $output) : int
66107
'Verb',
67108
]);
68109

69-
foreach ($this->router->getRoutes() as $route) {
110+
foreach ($this->getRouter()->getRoutes() as $route) {
70111
$table->addRow([
71112
$route->getName(),
72113
$route->getHost() ?? 'ANY',

0 commit comments

Comments
 (0)