--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/84a56774-1dbf-4ec3-b906-b29e8b322add%40googlegroups.com.
--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/3f61d384-3022-49aa-aad7-3527e35a510c%40googlegroups.com.
Create a new stream using PSR-17 StreamFactory.
On Thu, Oct 10, 2019 at 6:42 PM Anton Fedonjuk <antonf...@gmail.com> wrote:
Class contains method setRequest(RequestInterface $request), his abstact code:
Request body already sets: $request->getBody()->write('...');
I want send this request with other body content, but dont know how:
1. StreamInterface don't have methods as PHP ftruncate()2. Constructor not defined in StreamInterface: can't use $request->withBody() because dont know how create clone of that Steam object without contents.--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/84a56774-1dbf-4ec3-b906-b29e8b322add%40googlegroups.com.
Not sure what you mean, StreamFactoryInterface is designed to create new streams from strings, files, or existing resources. As the editor of PSR-17, I am quite sure this is the case.
On Thu, Oct 10, 2019 at 7:24 PM Anton Fedonjuk <antonf...@gmail.com> wrote:
Nope, u dont know factory class name--
пятница, 11 октября 2019 г., 3:05:42 UTC+3 пользователь Woody Gilk написал:Create a new stream using PSR-17 StreamFactory.
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...@googlegroups.com.
--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/84a56774-1dbf-4ec3-b906-b29e8b322add%40googlegroups.com.
class Stream implements StreamtInterface
{
protected $handler;
public function __construct()
{
$this->handler = tmpfile();
}
public function write($str)
{
fwrite($this->handler, $str);
}
}
class FilteredStream extends Stream
{
protected $filter;
public function __construct(callable $filter)
{
parent::__construct();
$this->filter = $filter;
}
public function write($str)
{
parent::write(($this->filter)($str));
}
}
$request->withBody(new Stream());
$myAbstractObject->setRequest($request);
// in other case
$request->withBody(new FilteredStream('myFilterFunct'));
$myAbstractObject->setRequest($request);
interface StreamInterface
{
/**
* Truncates a stream data to a given length.
*
* @see https://www.php.net/manual/en/function.ftruncate.php
*
* @param int $size The size to truncate to.
* @throws \InvalidArgumentException Size not integer or less zero.
* @throws \RuntimeException Not seekable.
*/
public function truncate($size);
/**
* Returns new instance with the specified contents.
*
* @param string $str New contents.
* @return static New instance.
* @throws \RuntimeException On failure.
*/
public function withContents($str);
}
class Stream implements StreamInterface
{
public function truncate($size)
{
if (intval($size) != $size || $size < 0) {
throw new \InvalidArgumentException('Size must be not negative integer');
}
if (! $this->isSeekable()) {
throw new \RuntimeException('Stream not seekable');
}
ftruncate($this->handle, $size);
if ($this->tell() > $size) {
// Fix cursor position
$this->seek(0, SEEK_END);
}
}
public function withContents($str)
{
$clone = clone $this;
$clone->rewind();
$clone->write($str);
ftruncate($this->handle, strlen($str));
// or $this->truncate(strlen($str));
return $clone;
}
}
class MySpecialRequestHandler
{
private $streamFactory;
public function __construct(StreamFactoryInterface $streamFactory)
{
$this->streamFactory = $streamFactory;
}
public function handleRequest(RequestInterface $request): RequestInterface
{
$newBody = $this->streamFactory->createStream('New body!');
return $request->withBody($newBody);
}
}
it is much better to initiate a new Stream and use that through the withBody() method. This is especially true if you do not know the underlying Stream architecture!
Also note that you can use any StreamInterface implementation.
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 ($request->getMethod() == 'POST') {
$request = clone $this->request;
$request->getBody()->write($data);
} else {
$uri = $this->request->getUri()->withQuery($data);
$request = $this->request->withUri($uri, true);
}
$response = $this->client->sendRequest($request);
return $this->parseResponse($response);
}
}
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.
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.
You can use PSR-7 without PSR-17 just fine.
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.
--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/5e72e957-3393-431c-a416-597df44bcd0d%40googlegroups.com.
Anton please keep it to a civil tone. Talking about "mistification" is not friendly nor acceptable here.
supporting PSR-7 alone has absolutely sense, and there are tons of packages that do that, because they care only about working on requests, not creating them.