In light of recent discussions regarding response objects and body not
being immutable, as summarized quite well by Andrew Carter [1], I am
wondering what could be done to make it easier to use the withBody()
method.
[1]:
http://andrewcarteruk.github.io/programming/2016/05/22/psr-7-is-not-immutable.html
Right now, it is extremely difficult to create a new stream without
referring to a specific implementation and so we often end up doing
something like this:
$body = $response->getBody();
$body->write($content);
When really, what we should be doing is something like:
$body = get_stream_from_string($content);
$response = $response->withBody($body);
The former breaks the immutability of the response, which leads to the
suggestion that it cannot be trusted. The latter preserves
immutability by replacing the body content entirely, which is what we
_should_ be doing but have a very hard time doing because
StreamInterface is mutable and a resource wrapper.
Rasmus posted a possible factory interface [2] in relation to PSR-7
middleware that could be a basis for a psr/http-factory package. The
factory defines methods to create all types of PSR-7 objects and
definitely help resolve the middleware debate, or at least provide a
better foundation for Anthony's argument against having
ResponseInterface part of the signature. (Though I still don't agree
that dropping $response from the signature is the right thing to do.)
[2]:
https://bitbucket.org/mindplaydk/psr-middleware/src/5688dc2f42d5b9eefcacf2fbc39f140b50a14112/psr-7-factory/FactoryInterface.php
Thoughts?
--
Woody Gilk
http://about.me/shadowhand