Skip to content

Commit

Permalink
Merge pull request #91 from sunrise-php/release/v2.12.0
Browse files Browse the repository at this point in the history
v2.12.0
  • Loading branch information
fenric authored Jan 26, 2022
2 parents bb07efb + 11662ce commit c3db73a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,27 @@ final class EntryUpdateRequestHandler implements RequestHandlerInterface

## Useful to know

### Get a current route

#### Through Router

> Available from version 2.12.
```php
$router->getMatchedRoute();
```

#### Through Request

> Available from version 1.x, but wasn't documented before...
```php
$request->getAttribute('@route');

// or
$request->getAttribute(\Sunrise\Http\Router\RouteInterface::ATTR_ROUTE);
```

### Generation a route URI

```php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"scripts": {
"test": [
"phpcs",
"XDEBUG_MODE=coverage phpunit --coverage-text"
"XDEBUG_MODE=coverage phpunit --coverage-text --colors=always"
],
"build": [
"phpdoc -d src/ -t phpdoc/",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
Expand Down
20 changes: 8 additions & 12 deletions src/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,6 @@ public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteCollec
return $this;
}

/**
* Alias to the addMiddleware method
*
* @param MiddlewareInterface ...$middlewares
*
* @return RouteCollectionInterface
*/
public function appendMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
{
return $this->addMiddleware(...$middlewares);
}

/**
* {@inheritdoc}
*/
Expand All @@ -172,6 +160,14 @@ public function prependMiddleware(MiddlewareInterface ...$middlewares) : RouteCo
return $this;
}

/**
* @deprecated 2.12.0 Use the addMiddleware method.
*/
public function appendMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
{
return $this->addMiddleware(...$middlewares);
}

/**
* @deprecated 2.10.0 Use the prependMiddleware method.
*/
Expand Down
22 changes: 21 additions & 1 deletion src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class Router implements MiddlewareInterface, RequestHandlerInterface, RequestMet
*/
private $middlewares = [];

/**
* The router's matched route
*
* @var RouteInterface|null
*/
private $matchedRoute = null;

/**
* Gets the router host table
*
Expand Down Expand Up @@ -115,6 +122,16 @@ public function getMiddlewares() : array
return array_values($this->middlewares);
}

/**
* Gets the router's matched route
*
* @return RouteInterface|null
*/
public function getMatchedRoute() : ?RouteInterface
{
return $this->matchedRoute;
}

/**
* Adds the given patterns to the router
*
Expand Down Expand Up @@ -355,7 +372,9 @@ public function run(ServerRequestInterface $request) : ResponseInterface
{
// lazy resolving of the given request...
$routing = new CallableRequestHandler(function (ServerRequestInterface $request) : ResponseInterface {
return $this->match($request)->handle($request);
$route = $this->match($request);
$this->matchedRoute = $route;
return $route->handle($request);
});

$middlewares = $this->getMiddlewares();
Expand All @@ -375,6 +394,7 @@ public function run(ServerRequestInterface $request) : ResponseInterface
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$route = $this->match($request);
$this->matchedRoute = $route;

$middlewares = $this->getMiddlewares();
if (empty($middlewares)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ public function testRun() : void
$routes[1]->getPath()
));

$this->assertNotNull($router->getMatchedRoute());
$this->assertSame($routes[1]->getName(), $router->getMatchedRoute()->getName());
$this->assertTrue($routes[1]->getRequestHandler()->isRunned());
}

Expand Down Expand Up @@ -509,6 +511,8 @@ public function testHandle() : void
$routes[2]->getPath()
));

$this->assertNotNull($router->getMatchedRoute());
$this->assertSame($routes[2]->getName(), $router->getMatchedRoute()->getName());
$this->assertTrue($routes[2]->getRequestHandler()->isRunned());
}

Expand Down

0 comments on commit c3db73a

Please sign in to comment.