Allow `$middleware->replace()` to accept an array of replacements

Merged
ZI
22d ago
+107-11

This PR updates the withMiddleware() helper object so that $middleware->replace() accepts an array of replacements.

This small change makes it possible to easily insert custom middleware before or after Laravel's built-in ones, without having to re-declare part or all of default middleware stack.

->withMiddleware(function (Middleware $middleware) {
    // Replace Foo with Bar and Baz...
    $middleware->replace(Foo::class, [Bar::class, Baz::class]);

    // Run after built-in TrustProxies...
    $middleware->replace(TrustProxies::class, [TrustProxies::class, StripCrossOriginReferer::class]);

    // Run before built-in TrimStrings...
    $middleware->replace(TrimStrings::class, [SanitiseStrings::class, TrimStrings::class]);
});

In future it might be possible to add some nicer aliases such as insertBefore() and insertAfter() for convenience, but for now this makes it possible with very minimal code changes.