Skip to content

Commit c3db73a

Browse files
authored
Merge pull request #91 from sunrise-php/release/v2.12.0
v2.12.0
2 parents bb07efb + 11662ce commit c3db73a

File tree

6 files changed

+56
-15
lines changed

6 files changed

+56
-15
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,27 @@ final class EntryUpdateRequestHandler implements RequestHandlerInterface
448448

449449
## Useful to know
450450

451+
### Get a current route
452+
453+
#### Through Router
454+
455+
> Available from version 2.12.
456+
457+
```php
458+
$router->getMatchedRoute();
459+
```
460+
461+
#### Through Request
462+
463+
> Available from version 1.x, but wasn't documented before...
464+
465+
```php
466+
$request->getAttribute('@route');
467+
468+
// or
469+
$request->getAttribute(\Sunrise\Http\Router\RouteInterface::ATTR_ROUTE);
470+
```
471+
451472
### Generation a route URI
452473

453474
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"scripts": {
6464
"test": [
6565
"phpcs",
66-
"XDEBUG_MODE=coverage phpunit --coverage-text"
66+
"XDEBUG_MODE=coverage phpunit --coverage-text --colors=always"
6767
],
6868
"build": [
6969
"phpdoc -d src/ -t phpdoc/",

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<phpunit colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
33
<coverage>
44
<include>
55
<directory>./src</directory>

src/RouteCollection.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,6 @@ public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteCollec
148148
return $this;
149149
}
150150

151-
/**
152-
* Alias to the addMiddleware method
153-
*
154-
* @param MiddlewareInterface ...$middlewares
155-
*
156-
* @return RouteCollectionInterface
157-
*/
158-
public function appendMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
159-
{
160-
return $this->addMiddleware(...$middlewares);
161-
}
162-
163151
/**
164152
* {@inheritdoc}
165153
*/
@@ -172,6 +160,14 @@ public function prependMiddleware(MiddlewareInterface ...$middlewares) : RouteCo
172160
return $this;
173161
}
174162

163+
/**
164+
* @deprecated 2.12.0 Use the addMiddleware method.
165+
*/
166+
public function appendMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
167+
{
168+
return $this->addMiddleware(...$middlewares);
169+
}
170+
175171
/**
176172
* @deprecated 2.10.0 Use the prependMiddleware method.
177173
*/

src/Router.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class Router implements MiddlewareInterface, RequestHandlerInterface, RequestMet
8383
*/
8484
private $middlewares = [];
8585

86+
/**
87+
* The router's matched route
88+
*
89+
* @var RouteInterface|null
90+
*/
91+
private $matchedRoute = null;
92+
8693
/**
8794
* Gets the router host table
8895
*
@@ -115,6 +122,16 @@ public function getMiddlewares() : array
115122
return array_values($this->middlewares);
116123
}
117124

125+
/**
126+
* Gets the router's matched route
127+
*
128+
* @return RouteInterface|null
129+
*/
130+
public function getMatchedRoute() : ?RouteInterface
131+
{
132+
return $this->matchedRoute;
133+
}
134+
118135
/**
119136
* Adds the given patterns to the router
120137
*
@@ -355,7 +372,9 @@ public function run(ServerRequestInterface $request) : ResponseInterface
355372
{
356373
// lazy resolving of the given request...
357374
$routing = new CallableRequestHandler(function (ServerRequestInterface $request) : ResponseInterface {
358-
return $this->match($request)->handle($request);
375+
$route = $this->match($request);
376+
$this->matchedRoute = $route;
377+
return $route->handle($request);
359378
});
360379

361380
$middlewares = $this->getMiddlewares();
@@ -375,6 +394,7 @@ public function run(ServerRequestInterface $request) : ResponseInterface
375394
public function handle(ServerRequestInterface $request) : ResponseInterface
376395
{
377396
$route = $this->match($request);
397+
$this->matchedRoute = $route;
378398

379399
$middlewares = $this->getMiddlewares();
380400
if (empty($middlewares)) {

tests/RouterTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ public function testRun() : void
428428
$routes[1]->getPath()
429429
));
430430

431+
$this->assertNotNull($router->getMatchedRoute());
432+
$this->assertSame($routes[1]->getName(), $router->getMatchedRoute()->getName());
431433
$this->assertTrue($routes[1]->getRequestHandler()->isRunned());
432434
}
433435

@@ -509,6 +511,8 @@ public function testHandle() : void
509511
$routes[2]->getPath()
510512
));
511513

514+
$this->assertNotNull($router->getMatchedRoute());
515+
$this->assertSame($routes[2]->getName(), $router->getMatchedRoute()->getName());
512516
$this->assertTrue($routes[2]->getRequestHandler()->isRunned());
513517
}
514518

0 commit comments

Comments
 (0)