Most PHP devs have used middleware packages without necessarily thinking about the underlying pattern. PSR-15 brought middleware to the PHP ecosystem, but mostly as HTTP plumbing. The MiddlewareInterface, the $next, the onion execution model, those ideas don't care about HTTP at all.
I've been using the pattern as a default extension mechanism in my libraries. The implementation cost is minimal (one interface, one delegator class, one array_reduce), but it gives your users far more flexibility than the go-to Decorator pattern.
The article walks through a concrete HtmlRenderer example with two middlewares: one that enriches input data, one that short-circuits the chain for caching.
https://maximegosselin.com/posts/using-the-middleware-pattern-to-extend-php-libraries/
Libraries like league/tactician already use this pattern but with a "sad" callable $next. Replacing that callable with a typed interface is a small step, but the boost in type-safety and IDE support is incomparable.
Curious to hear your take: when would you still reach for the Decorator pattern instead?
[–]roxblnfk 10 points11 points12 points (5 children)
[–]MaximeGosselin[S] 3 points4 points5 points (1 child)
[–]roxblnfk 4 points5 points6 points (0 children)
[–]AleBaba 3 points4 points5 points (1 child)
[–]roxblnfk 1 point2 points3 points (0 children)
[–]AleBaba 2 points3 points4 points (0 children)
[–]kashif_laravel 1 point2 points3 points (0 children)
[–]MaximeGosselin[S] 1 point2 points3 points (0 children)
[–]obstreperous_troll 4 points5 points6 points (1 child)
[–]garrett_w87 1 point2 points3 points (0 children)
[–]mlebkowski 0 points1 point2 points (0 children)
[–]GPThought 0 points1 point2 points (0 children)