interface MiddlewareInterface {}
interface ClientMiddlewareInterface extends MiddlewareInterface
{
public function process(RequestInterface $request, DelegateInterface $next);
}
interface ServerMiddlewareInterface extends MiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $frame);
}
interface DelegateInterface
{
public function next(RequestInterface $request);
}
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
return function ($request, array $options) use ($handler) {
// …
$request = $cookieJar->withCookieHeader($request);
return $handler($request, $options)
->then(function ($response) use ($cookieJar, $request) {
$cookieJar->extractCookies($request, $response);
return $response;
}
);
};
class AuthMiddleware implements ClientMiddlewareInterface
{
// …
public function process(RequestInterface $request, DelegateInterface $next)
{
if ($this->accessToken === null) {
$loginRequest = $this->createLoginRequest();
$this->accessToken = $next->next($loginRequest)->getBody()->getContents();
}
$request = $request->withHeader('Authorization', $this->accessToken);
return $next($request);
}
private function createLoginRequest()
{
$req = $this->requestFactory->createRequest('POST', '/login');
// add $login, $password…
return $req;
}
}
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/f57bb59a-5784-4e6d-aaea-bd738e49a00b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
Also that would maybe mean we can get rid of the dual "MiddlewareInterface" and "ServerMiddlewareInterface"?
You received this message because you are subscribed to a topic in the Google Groups "PHP Framework Interoperability Group" group.To unsubscribe from this topic, visit https://groups.google.com/d/topic/php-fig/vBk0BRgDe2s/unsubscribe.To unsubscribe from this group and all its topics, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/e2169b71-0fcc-44f7-9a3d-433f041b331c%40googlegroups.com.
Hi,
David and Matthieu already give my point of view, however there is still some things to consider:
At HttpPlug we wrote a lot of client side Middleware from what we have done we organise them in 3 categories (cf http://docs.php-http.org/en/latest/plugins/introduction.html part about ordering middleware) :
IMO, on the server side it should be the same “categories” of middleware. From what i imagine, implementation of “Observer middlewares” will be the same for server and client. However “Control Flow” and “Request/Response Modifier”
middlewares will not have the same implementations, as on the server you read the request to write the response and on the client you write the request to read the response.
As an example, a compression Middleware (gzip) will encode the response for a Server middleware but will decode it for a Client middleware
Creating a Middleware that works on boths worlds imply, IMO:
next
method on the $frame
As an example we could have the following middleware
class GzipEncoderMiddleware implements MiddlewareInterface
{
public function process(MessageInterface $message, DelegateInterface $frame)
{
$message = $message->withBody(new GzipStream($message->getBody()));
return $frame->next($message);
}
}
Then this middleware could be use for encoding the request in a client side approach or encoding the response in a server side approach.
But even there they will be many problems as the HTTP RFC does not always use the same headers for specifiying the response or the request so this will be in fact not usable.
IMO, Having a PSR15 that works on server and client is not something that should be considered, as it will overcomplicate the standard for something that cannot be shared (or will be extremly difficult). Futhermore i think this will confuse
many people that works on the client side.
For Mathieu and Michael:
Calling the root middleware for the server apps is a much less widespread use case AFAIK. In existing implementations and de-facto standards (HttpKernelInterface and “PSR-7 middlewares”) there is no such thing, so (IMO) it wouldn’t make sense to create such a big break for no gain in most cases.
I’m not sure, we may consider the HMVC approach of symfony (the sub request system) as a way to pass a new request to the root middleware.
However after rethinking why we have include the $root
middleware in our system, i think this was a mistake, it make things simple only for a single implementation (the Retry one) but overcomplicate the interface for all others implementations that never need the root.
Our chain of middleware (referenced by the root / first middleware) is unique at each new request. Having this unicity (at the request level) is necessary for the Retry
plugin as we can use it as a reference to detect circular redirection (and also avoid overlap with another call). There is certainly a better way to transform a RequestInterface
object into a unique reference, however i never had the time to look more at this.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/7e729268-e3cd-4a6b-8b00-4cd1b3c24525%40googlegroups.com.