Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 613fae7

Browse files
committed
Merge branch 'feature/route-result-subject'
Close #211
2 parents 2394bf3 + 47c7639 commit 613fae7

File tree

6 files changed

+46
-53
lines changed

6 files changed

+46
-53
lines changed

CHANGELOG.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ Third release candidate.
1717
array **must** be callables, service names resolving to callable middleware,
1818
or fully qualified class names that can be instantiated without arguments, and
1919
which result in invokable middleware.
20-
- [#200](https://github.com/zendframework/zend-expressive/pull/200) and
21-
[#206](https://github.com/zendframework/zend-expressive/pull/206) add a new
22-
interface, `Zend\Expressive\RouteResultObserverInterface`.
23-
`Zend\Expressive\Application` now also defines two methods,
24-
`attachRouteResultObserver()` and `detachRouteResultObserver()`, which accept
25-
instances of the interface. During `routeMiddleware()`, all observers are
26-
updated immediately following the call to `RouterInterface::match()` with the
27-
`RouteResult` instance. This feature enables the ability to notify objects of
28-
the calculated `RouteResult` without needing to inject middleware into the
29-
system.
20+
- [#200](https://github.com/zendframework/zend-expressive/pull/200),
21+
[#206](https://github.com/zendframework/zend-expressive/pull/206), and
22+
[#211](https://github.com/zendframework/zend-expressive/pull/211) add
23+
functionality for observing computed `RouteResult`s.
24+
`Zend\Expressive\Application` now implements
25+
`Zend\Expressive\Router\RouteResultSubjectInterface`, which allows attaching
26+
`Zend\Expressive\RouteResultObserverInterface` implementations and notifying
27+
them of computed `RouteResult` instances. The following methods are now
28+
available on the `Application` instance:
29+
- `attachRouteResultObserver(Router\RouteResultObserverInterface $observer)`
30+
- `detachRouteResultObserver(Router\RouteResultObserverInterface $observer)`
31+
- `notifyRouteResultObservers(RouteResult $result)`; `Application` calls this
32+
internally within `routeMiddleware`.
33+
This feature enables the ability to notify objects of the calculated
34+
`RouteResult` without needing to inject middleware into the system.
3035
- [#81](https://github.com/zendframework/zend-expressive/pull/81) adds a
3136
cookbook entry for creating 404 handlers.
3237
- [#210](https://github.com/zendframework/zend-expressive/pull/210) adds a

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"container-interop/container-interop": "^1.1",
2121
"psr/http-message": "^1.0",
2222
"zendframework/zend-diactoros": "^1.1",
23-
"zendframework/zend-expressive-router": "^1.0",
23+
"zendframework/zend-expressive-router": "^1.1",
2424
"zendframework/zend-expressive-template": "^1.0.1",
2525
"zendframework/zend-stratigility": "^1.1"
2626
},

doc/book/router/result-observers.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ you to notify such utilities of the results of matching.
2121
Route result observers must implement the `RouteResultObserverInterface`:
2222

2323
```php
24-
namespace Zend\Expressive;
24+
namespace Zend\Expressive\Router;
2525

2626
use Zend\Expressive\Router\RouteResult;
2727

@@ -52,6 +52,16 @@ You can detach an existing observer as well, by passing its instance to the
5252
$app->detachRouteResultObserver($observer);
5353
```
5454

55+
> ### RouteResultSubjectInterface
56+
>
57+
> `Zend\Expressive\Application` implements `Zend\Expressive\Router\RouteResultSubjectInterface`,
58+
> which defines methods for attaching and detaching route result observers, as
59+
> well as a method for notifying observers. Typically you'll only see the
60+
> `Application` class as an implementation of the interface, but you can always
61+
> create your own implementations as well if desired — for instance, if
62+
> you are implementing your own middleware runtime using the various interfaces
63+
> Expressive provides.
64+
5565
## Example
5666

5767
For this example, we'll build a simple URI generator. It will compose a
@@ -61,7 +71,7 @@ when invoked, generate a URI.
6171
```php
6272
use Zend\Expressive\Router\RouterInterface;
6373
use Zend\Expressive\Router\RouteResult;
64-
use Zend\Expressive\RouteResultObserverInterface;
74+
use Zend\Expressive\ROuter\RouteResultObserverInterface;
6575

6676
class UriGenerator implements RouteResultObserverInterface
6777
{

src/Application.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @method Router\Route patch($path, $middleware, $name = null)
3030
* @method Router\Route delete($path, $middleware, $name = null)
3131
*/
32-
class Application extends MiddlewarePipe
32+
class Application extends MiddlewarePipe implements Router\RouteResultSubjectInterface
3333
{
3434
/**
3535
* @var null|ContainerInterface
@@ -176,26 +176,38 @@ public function any($path, $middleware, $name = null)
176176
/**
177177
* Attach a route result observer.
178178
*
179-
* @param RouteResultObserverInterface $observer
179+
* @param Router\RouteResultObserverInterface $observer
180180
*/
181-
public function attachRouteResultObserver(RouteResultObserverInterface $observer)
181+
public function attachRouteResultObserver(Router\RouteResultObserverInterface $observer)
182182
{
183183
$this->routeResultObservers[] = $observer;
184184
}
185185

186186
/**
187187
* Detach a route result observer.
188188
*
189-
* @param RouteResultObserverInterface $observer
189+
* @param Router\RouteResultObserverInterface $observer
190190
*/
191-
public function detachRouteResultObserver(RouteResultObserverInterface $observer)
191+
public function detachRouteResultObserver(Router\RouteResultObserverInterface $observer)
192192
{
193193
if (false === ($index = array_search($observer, $this->routeResultObservers, true))) {
194194
return;
195195
}
196196
unset($this->routeResultObservers[$index]);
197197
}
198198

199+
/**
200+
* Notify all route result observers with the given route result.
201+
*
202+
* @param Router\RouteResult
203+
*/
204+
public function notifyRouteResultObservers(Router\RouteResult $result)
205+
{
206+
foreach ($this->routeResultObservers as $observer) {
207+
$observer->update($result);
208+
}
209+
}
210+
199211
/**
200212
* Overload pipe() operation.
201213
*
@@ -692,16 +704,4 @@ private function marshalLazyErrorMiddlewareService($middleware, ContainerInterfa
692704
return $invokable($error, $request, $response, $next);
693705
};
694706
}
695-
696-
/**
697-
* Notify all route result observers with the given route result.
698-
*
699-
* @param Router\RouteResult
700-
*/
701-
private function notifyRouteResultObservers(Router\RouteResult $result)
702-
{
703-
foreach ($this->routeResultObservers as $observer) {
704-
$observer->update($result);
705-
}
706-
}
707707
}

src/RouteResultObserverInterface.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/RouteMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Zend\Diactoros\ServerRequest;
1717
use Zend\Expressive\Application;
1818
use Zend\Expressive\Router\RouteResult;
19-
use Zend\Expressive\RouteResultObserverInterface;
19+
use Zend\Expressive\Router\RouteResultObserverInterface;
2020
use Zend\Expressive\Router\RouterInterface;
2121

2222
class RouteMiddlewareTest extends TestCase

0 commit comments

Comments
 (0)