Skip to content

Commit afc5114

Browse files
authored
Merge pull request #95 from sunrise-php/release/v2.14.0
v2.14.0
2 parents 9f07a5d + 3cc2a84 commit afc5114

File tree

13 files changed

+412
-85
lines changed

13 files changed

+412
-85
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
## v2.13.0
22

33
* Supports for events using the `symfony/event-dispatcher`.
4+
5+
## v2.14.0
6+
7+
* New method added: `Route::getHolder():Reflector`;
8+
* New method added: `Router::resolveHostname(string):?string`;
9+
* New method added: `Router::getRoutesByHostname(string):array`;
10+
* New method added: `RouterBuilder::setEventDispatcher(?EventDispatcherInterface):void`.

README.md

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
## Installation
1313

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

1818
## Support for OpenAPI (Swagger) Specification (optional)
@@ -444,6 +444,28 @@ use Sunrise\Http\Router\Annotation\Route;
444444
final class EntryUpdateRequestHandler implements RequestHandlerInterface
445445
```
446446

447+
##### Additional annotations
448+
449+
```php
450+
use Sunrise\Http\Router\Annotation\Host;
451+
452+
#[Host('admin')]
453+
#[Prefix('/api/v1')]
454+
#[Postfix('.json')]
455+
#[Middleware(SomeMiddleware::class)]
456+
final class SomeController
457+
{
458+
#[Route('foo', '/foo')]
459+
public function foo(ServerRequestInterface $request) : ResponseInterface
460+
{
461+
// this action will be available at:
462+
// http://admin.host/api/v1/foo.json
463+
//
464+
// this can be handy to reduce code duplication...
465+
}
466+
}
467+
```
468+
447469
---
448470

449471
## Useful to know
@@ -469,6 +491,16 @@ $request->getAttribute('@route');
469491
$request->getAttribute(\Sunrise\Http\Router\RouteInterface::ATTR_ROUTE);
470492
```
471493

494+
#### Through Event
495+
496+
> Available from version 2.13.
497+
498+
```php
499+
$eventDispatcher->addListener(RouteEvent::NAME, function (RouteEvent $event) {
500+
$event->getRoute();
501+
});
502+
```
503+
472504
### Generation a route URI
473505

474506
```php
@@ -485,6 +517,8 @@ $response = $router->getRoute('route.name')->handle($request);
485517

486518
### Route grouping
487519

520+
Example for annotations [here](#additional-annotations).
521+
488522
```php
489523
$collector->group(function ($collector) {
490524
$collector->group(function ($collector) {
@@ -528,15 +562,39 @@ $collector->get('api.entry.read', '/api/v1/entry/{slug<@slug>}');
528562
$collector->get('api.entry.read', '/api/v1/entry/{id<@id>}');
529563
```
530564

565+
It is better to set patterns through the router:
566+
567+
```php
568+
// available since version 2.11.0
569+
$router->addPatterns([
570+
'@id' => '[1-9][0-9]*',
571+
]);
572+
```
573+
574+
...or through the router's builder:
575+
576+
```php
577+
// available since version 2.11.0
578+
$builder->setPatterns([
579+
'@id' => '[1-9][0-9]*',
580+
]);
581+
```
582+
531583
### Hosts (available from version 2.6.0)
532584

533585
> Note: if you don't assign a host for a route, it will be available on any hosts!
534586
535587
```php
536588
// move the hosts table into the settings...
537-
$router->addHost('public.host', 'www.example.com');
538-
$router->addHost('admin.host', 'secret.example.com');
539-
$router->addHost('api.host', 'api.example.com');
589+
$router->addHost('public.host', 'www.example.com', ...);
590+
$router->addHost('admin.host', 'secret.example.com', ...);
591+
$router->addHost('api.host', 'api.example.com', ...);
592+
593+
// ...or:
594+
$router->addHosts([
595+
'public.host' => ['www.example.com', ...],
596+
...
597+
]);
540598

541599
// the route will available only on the `secret.example.com` host...
542600
$route->setHost('admin.host');
@@ -548,6 +606,28 @@ $collector->group(function ($collector) {
548606
->setHost('admin.host');
549607
```
550608

609+
You can resolve the hostname since version 2.14.0 as follows:
610+
611+
```php
612+
$router->addHost('admin', 'www1.admin.example.com', 'www2.admin.example.com');
613+
614+
$router->resolveHostname('www1.admin.example.com'); // return "admin"
615+
$router->resolveHostname('www2.admin.example.com'); // return "admin"
616+
$router->resolveHostname('unknown'); // return null
617+
```
618+
619+
Also you can get all routes by hostname:
620+
621+
```php
622+
$router->getRoutesByHostname('www1.admin.example.com');
623+
```
624+
625+
### Route Holder
626+
627+
```php
628+
$route->getHolder(); // return Reflector (class, method or function)
629+
```
630+
551631
### The router builder
552632

553633
```php
@@ -559,6 +639,7 @@ $router = (new RouterBuilder)
559639
->useDescriptorLoader([]) // array with classes or directory with classes...
560640
->setHosts([]) //
561641
->setMiddlewares([]) // array with middlewares...
642+
->setPatterns([]) // available since version 2.11.0
562643
->build();
563644
```
564645

src/Command/RouteListCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class RouteListCommand extends Command
4747

4848
/**
4949
* {@inheritdoc}
50+
*
51+
* @var string
5052
*/
5153
protected static $defaultDescription = 'Lists all routes in your application';
5254

src/Loader/ConfigLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public function setContainer(?ContainerInterface $container) : void
108108
public function attach($resource) : void
109109
{
110110
if (is_dir($resource)) {
111-
$resources = glob($resource . '/*.php');
112-
foreach ($resources as $resource) {
113-
$this->resources[] = $resource;
111+
$fileNames = glob($resource . '/*.php');
112+
foreach ($fileNames as $fileName) {
113+
$this->resources[] = $fileName;
114114
}
115115

116116
return;

src/Loader/DescriptorLoader.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
use Doctrine\Common\Annotations\SimpleAnnotationReader;
1818
use Psr\Container\ContainerInterface;
19+
use Psr\Http\Server\MiddlewareInterface;
1920
use Psr\Http\Server\RequestHandlerInterface;
2021
use Psr\SimpleCache\CacheInterface;
2122
use Sunrise\Http\Router\Annotation\Host;
@@ -61,7 +62,7 @@ class DescriptorLoader implements LoaderInterface
6162
{
6263

6364
/**
64-
* @var string[]
65+
* @var class-string[]
6566
*/
6667
private $resources = [];
6768

@@ -194,9 +195,9 @@ public function setCacheKey(?string $cacheKey) : void
194195
public function attach($resource) : void
195196
{
196197
if (is_dir($resource)) {
197-
$resources = $this->scandir($resource);
198-
foreach ($resources as $resource) {
199-
$this->resources[] = $resource;
198+
$classNames = $this->scandir($resource);
199+
foreach ($classNames as $className) {
200+
$this->resources[] = $className;
200201
}
201202

202203
return;
@@ -234,9 +235,9 @@ public function load() : RouteCollectionInterface
234235

235236
$routes = [];
236237
foreach ($descriptors as $descriptor) {
237-
$middlewares = $descriptor->middlewares;
238-
foreach ($middlewares as &$middleware) {
239-
$middleware = $this->referenceResolver->toMiddleware($middleware);
238+
$middlewares = [];
239+
foreach ($descriptor->middlewares as $className) {
240+
$middlewares[] = $this->referenceResolver->toMiddleware($className);
240241
}
241242

242243
$routes[] = $this->routeFactory->createRoute(
@@ -403,7 +404,7 @@ private function supplementDescriptor(Route $descriptor, Reflector $reflector) :
403404
* @param ReflectionClass|ReflectionMethod $reflector
404405
* @param class-string<T> $annotationName
405406
*
406-
* @return array<T>
407+
* @return T[]
407408
*
408409
* @template T
409410
*/
@@ -438,7 +439,7 @@ private function getAnnotations(Reflector $reflector, string $annotationName) :
438439
*
439440
* @param string $directory
440441
*
441-
* @return string[]
442+
* @return class-string[]
442443
*/
443444
private function scandir(string $directory) : array
444445
{

src/ReferenceResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,19 @@ public function toMiddleware($reference) : MiddlewareInterface
126126
*
127127
* @param mixed $reference
128128
*
129-
* @return array
129+
* @return array{0: ?class-string, 1: ?string}
130130
*/
131131
private function normalizeReference($reference) : array
132132
{
133133
if (is_array($reference) && is_callable($reference, true)) {
134+
/** @var array{0: class-string, 1: string} $reference */
135+
134136
return $reference;
135137
}
136138

137139
if (is_string($reference)) {
140+
/** @var class-string $reference */
141+
138142
return [$reference, null];
139143
}
140144

src/Route.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
use Psr\Http\Message\ServerRequestInterface;
1919
use Psr\Http\Server\MiddlewareInterface;
2020
use Psr\Http\Server\RequestHandlerInterface;
21+
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler;
2122
use Sunrise\Http\Router\RequestHandler\QueueableRequestHandler;
23+
use Closure;
24+
use ReflectionClass;
25+
use ReflectionMethod;
26+
use ReflectionFunction;
27+
use Reflector;
2228

2329
/**
2430
* Import functions
@@ -228,6 +234,26 @@ public function getTags() : array
228234
return $this->tags;
229235
}
230236

237+
/**
238+
* {@inheritdoc}
239+
*/
240+
public function getHolder() : Reflector
241+
{
242+
$handler = $this->requestHandler;
243+
if ($handler instanceof CallableRequestHandler) {
244+
$callback = $handler->getCallback();
245+
if ($callback instanceof Closure) {
246+
return new ReflectionFunction($callback);
247+
}
248+
249+
/** @var array{0: class-string|object, 1: string} $callback */
250+
251+
return new ReflectionMethod(...$callback);
252+
}
253+
254+
return new ReflectionClass($handler);
255+
}
256+
231257
/**
232258
* {@inheritdoc}
233259
*/

src/RouteInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
use Psr\Http\Server\MiddlewareInterface;
1818
use Psr\Http\Server\RequestHandlerInterface;
19+
use ReflectionClass;
20+
use ReflectionMethod;
21+
use ReflectionFunction;
22+
use Reflector;
1923

2024
/**
2125
* RouteInterface
@@ -110,6 +114,15 @@ public function getDescription() : string;
110114
*/
111115
public function getTags() : array;
112116

117+
/**
118+
* Gets the route holder
119+
*
120+
* @return ReflectionClass|ReflectionMethod|ReflectionFunction
121+
*
122+
* @since 2.14.0
123+
*/
124+
public function getHolder() : Reflector;
125+
113126
/**
114127
* Sets the given name to the route
115128
*

0 commit comments

Comments
 (0)