Guys, it's all about the flow control and how to handle it while keeping as 
many things in the stack and not as state on middleware.

For controlling the flow, we have this two approaches, first being the one 
intended and the second being more or less the one Alessandro propose.

class Middleware1
{
    public function __invoke($request, $response, $next)
    {
        $request = $this->changeRequestBefore($request);
        $response = $this->changeReponseBefore($response, $request);

        $response = $next($request, $response);

        $response = $this->changeReponseAfter($response, $request);

        return $response;
    }
}


class Middleware2
{
    public function requestBefore($request)
    {
        return $this->changeRequestBefore($request);
    }
    
    public function responseBefore($request, $response)
    {
        return $this->changeReponseBefore($response, $request);
   }

    public function responseAfter($request, $response)
    {
        return $this->changeReponseAfter($response, $request);
    }
}


Indeed, the second approach allows to have better single responsibility per 
method/class but forces usage of the state because in practice, it's often 
the case when some local variable calculated in requestBefore part or 
responseBefore part are needed in responseAfter part also. If we have them 
all in one method, we can keep them locally.
IMO, calling $next is just a way to control the flow, exiting the current 
method and continuing after. Just how stacks work.

One more thing that sometimes is forgotten: the main purpose of FIG is for 
framework interoperability and PSRs are sometimes more on how frameworks 
are and not necessary on best design principles. And that's good, because 
this brings help related to most helpful design from practice. First 
approach is more helpful in practice.

Alex




-- 
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to php-fig+unsubscr...@googlegroups.com.
To post to this group, send email to php-fig@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/9a59545a-cfd9-4c2f-9a56-55b127bde749%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to