flarum / framework

Simple forum software for building great communities.
http://flarum.org/
6.35k stars 834 forks source link

Sorted middleware stack #1957

Closed luceos closed 4 years ago

luceos commented 4 years ago

Based on work done in #1919 we now need to be able to mutate the middleware stack before it is finalised. In order to do so we will bind the middleware stack (array) into IoC and resolve it for the pipeline.

The methods we need to allow for are at least:

Then when we use the Middleware extender, we can allow direct mutation of this stack as well. An example would be:

$stack->insertAfter(Flarum\Http\Middleware\AuthenticateWithHeader::class, App\Http\Middleware\AuthenticateWithLdap::class);

Or similar using the extender:

(new Flarum\Extend\Middleware('forum'))
    ->insertAfter(Flarum\Http\Middleware\AuthenticateWithHeader::class, App\Http\Middleware\AuthenticateWithLdap::class);

One could possibly use the magic methods to proxy the method to the stack.

tankerkiller125 commented 4 years ago

Replace, Add and Remove are all function that should work using the current middleware dependency (pipe) however the insertAfter and insertBefore may not work exactly as intended because the middleware pipe is Asynchronous so although it may sometimes work in the order that we place the middlewares it may also not sometimes if a middleware takes longer to process than others.

franzliedke commented 4 years ago

Replace, Add and Remove are all function that should work using the current middleware dependency (pipe)

Hmm, I thought that was not possible, judging by the MiddlewarePipe class' implementation:

https://github.com/zendframework/zend-stratigility/blob/58b783400da6960cb1e70d2ec07e19e33b8c6fb1/src/MiddlewarePipe.php

tankerkiller125 commented 4 years ago

The function is not built in no, but if we manipulate the middleware array before finalization it is possible as noted in the original ticket:

In order to do so we will bind the middleware stack (array) into IoC and resolve it for the pipeline.

It can be done

franzliedke commented 4 years ago

Oh, yes. Okay, misunderstanding cleared then.

We first have to bind the array to the container, manipulate that via extenders, and only wrap it in the pipe when we actually have a request that needs to be handled.