This also breaks the AuthenticationMiddleware in the documented example, since the returned response is not detected/used properly:
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Return Error 401 "Unauthorized" if the provided API key doesn't match the expected one
if ($request->getHeader("x-api-key") !== [$this->apiKey]) {
return $this->errorResponsePrototype->withStatus(401); //Will not have effect?
}
// Invoke the remaining middleware if authentication was successful
return $handler->handle($request);
}
Now my question: is this known (undocumented) behaviour, or is it a bug? I suppose the latter, but I might be wrong.
I have a middleware class which checks for the existence of a cache entry and either:
Something like this:
This works properly when adding it to Harmony using:
However when using a condition, it seems that the remaining middleware is always invoked, even when the response was returned directly:
This also breaks the AuthenticationMiddleware in the documented example, since the returned response is not detected/used properly:
Now my question: is this known (undocumented) behaviour, or is it a bug? I suppose the latter, but I might be wrong.
Analysis I think it is caused by the way the condition functionality is implemented. In case it evaluates to true, a new Harmony instance is created and executed, after which the remaining middleware is always processed: https://github.com/woohoolabs/harmony/blob/2a02f490258ff990de4609694d17be938edb8522/src/Harmony.php#L138
Possible solution A solution might be to add remaining middleware to the new Harmony instance when a condition evaluates to true, something like:
Not sure whether this works properly with nested conditions and such?
@kocsismate ?