On Sat, Oct 12, 2019 at 9:25 AM Anton Fedonjuk <[email protected]>
wrote:
> *Not reusable/resettable objects it`s potential problem in future.*
> *Yes, we can send factories or add empty instances, but it's harder than
> add method to reset content.*
>
>
Resetting content means _resetting the StreamInterface instance used with
the message_.
This is by design. Since the `withBody()` method returns a new instance, it
ensures that changes to the original body stream do not propagate to the
current instance. If you want to reset content for the purpose of writing
to it, _it should not matter what kind of stream was used before_.
If it does, then your code is tied to an _implementation_, and, if that's
the case, you should directly instantiate objects from that implementation,
using the signature of their constructors.
If it doesn't (and it _shouldn't_), using a composed PSR-17
StreamFactoryInterface will get you a new stream that you can use
immediately, and you're now no longer tied to a specific implementation.
> My current code:
>
> abstract class HttpGateway extends Gateway
> {
> protected $client;
> protected $request;
> protected $dataSeparator;
>
> public function __construct(ClientInterface $client,
> RequestFactoryInterface $factory)
> {
> $this->client = $client;
> // Checks properties because extended class can overwrite them.
> if (! $this->request) {
> $this->request = $factory->createRequest('GET', $this->getUrl
> ());
> }
> if (! $this->dataSeparator) {
> $this->dataSeparator = ini_get('arg_separator.output') ?: '&';
> }
> }
>
> protected function sendRequest(array $data)
> {
> $data = http_build_query($data, '', $this->dataSeparator,
> PHP_QUERY_RFC3986);
> if ($this->request->getMethod() == 'POST') {
> $request = clone $this->request;
> $request->getBody()->write($data);
> } else {
> $uri = $request->getUri()->withQuery($data);
> $request = $this->request->withUri($uri, true);
> }
> $response = $this->client->sendRequest($request);
> return $this->parseResponse($response);
> }
> }
>
> *As result: I can`t use PSR-7 without PSR-17, than why not merge it and
> adds exception iterfaces?*
>
You can use PSR-7 without PSR-17 just fine. We did it in Expressive by
providing prototype factories, which users could override by registering
their own versions that would return instances from the PSR-7
implementation they chose.
PSR-17 _simplifies_ this by providing a standard.
But you can totally use PSR-7 without PSR-17. As an example, a _library_
that consumes PSR-7 instances directly _does not care about the factories
at all_, only the PSR-7 instances.
There's reasons to keep them separate.
At the application level, yes, you will generally use them in tandem. But,
again, it's not required.
--
Matthew Weier O'Phinney
[email protected]
https://mwop.net/
he/him
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/php-fig/CAJp_myXkq2Tz6Dd5bmPRKSsy7OSyzuZFTjFfV6yftbPY%2BEX%2BOQ%40mail.gmail.com.