adrenaline icon indicating copy to clipboard operation
adrenaline copied to clipboard

Added possibility to gather behaviors into plugins

Open dropdevcoding opened this issue 7 years ago • 1 comments

With this feature you may implement your own plugins and attach them to Adrenaline. This feature is helpful when you want to define a set of middlewares which should work together although they use different hooks of adrenaline.

Simple example:

final class ActionExecutionTimeLoggerPlugin implements Plugin
{
    public function applyTo(Adrenaline $adrenaline) 
    {
        $adrenaline->beforeExecuteAction(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null)  {
            $request = $request->withAttribute(self::class, time()); 
            if ($next) {
                $response = $next($request, $response);
            }
            return $response;
        });

        $adrenaline->beforeResolveResponder(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) use ($start) {
            $duration = time() - $request->getAttribute(self::class);
            // log the execution time
            if ($next) {
                $response = $next($request, $response);
            }
            return $response;
        });
    }
}

$adrenaline->attach(new ActionExecutionTimeLoggerPlugin());

Sure, this example is a very simple one but it demonstrates how cross-middleware functionalities may be bundled when one of the middlewares wouldn't make any sense standalone.

dropdevcoding avatar Aug 11 '16 18:08 dropdevcoding