radarphp / Radar.Adr

The Action-Domain-Responder core for Radar.
MIT License
55 stars 7 forks source link

Modify middleware pattern #6

Closed shadowhand closed 9 years ago

shadowhand commented 9 years ago

As per this discussion, the current implementation of middleware could be improved by following this pattern (which is derived from Stack):

public function __invoke(ServerRequest $request, Response $response) {
    // modify, read, etc
    return $response;
}

This preserves the immutable nature of PSR-7 interfaces without having to use pass-by-reference to "modify" the immutable objects.

pmjones commented 9 years ago

Agreed. I have worked up a rewrite of the middleware system to use chaining, a la Conduit from MWOP, rather than filters. You can see the mostly-tested but as-yet-undocumented work in the temporary "midwrap" branch. https://github.com/radarphp/Radar.Adr/tree/midwrap

pmjones commented 9 years ago

Radar.Project now has a matching "midwrap" branch with documentation; the relevant major doc change is here. I like this approach a lot better than the previous one; the immutability of the request/response pair biases architectures toward this kind of implementation, I think. @shadowhand your thoughts?

shadowhand commented 9 years ago

@pmjones sorry I didn't get back to you sooner... I think the "midwrap" changes are great! TBH I haven't completely wrapped (heheh) my mind around how the stacking works, but I probably just need to read the tests again. Thanks!

pmjones commented 9 years ago

That's good news, then. I think this ends up being easier once you "get" it but that might take a bit (like it took me).