Skip to content

Commit

Permalink
make compatible with stream interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 5, 2024
1 parent 4a69954 commit f4b9555
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 35 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
}
],
"require": {
"php": "^7.2 || ^8.0",
"nyholm/psr7": "^1.3",
"php-http/httplug": "^2.0",
"php": "^7.4 || ^8.0",
"nyholm/psr7": "^1.8.1",
"php-http/httplug": "^2.4",
"psr/http-client": "^1.0",
"symfony/options-resolver": "^2.6 || ^3.4 || ^4.4 || ^5.0 || ^6.0 || ^7.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.2 || ^3.0",
"friendsofphp/php-cs-fixer": "^3.51",
"php-http/client-integration-tests": "^3.0",
"php-http/message": "^1.9",
"php-http/client-common": "^2.3",
"php-http/message": "^1.16",
"php-http/client-common": "^2.7",
"phpunit/phpunit": "^8.5.23 || ~9.5"
},
"provide": {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<testsuites>
<testsuite name="Socket Client Test Suite">
<directory>tests/</directory>
<exclude>tests/SocketClientFeatureTest.php</exclude>
</testsuite>
</testsuites>
<php>
Expand Down
5 changes: 0 additions & 5 deletions src/ResponseReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
trait ResponseReader
{
/**
* @var ResponseFactory For creating response
*/
protected $responseFactory;

/**
* Read a response from a socket.
*
Expand Down
32 changes: 13 additions & 19 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(RequestInterface $request, $socket, ?int $size = nul
$this->request = $request;
}

public function __toString()
public function __toString(): string
{
try {
return $this->getContents();
Expand All @@ -70,7 +70,7 @@ public function __toString()
}
}

public function close()
public function close(): void
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand All @@ -93,12 +93,12 @@ public function detach()
/**
* @return int<0, max>|null
*/
public function getSize()
public function getSize(): ?int
{
return $this->size;
}

public function tell()
public function tell(): int
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand All @@ -111,7 +111,7 @@ public function tell()
return $tell;
}

public function eof()
public function eof(): bool
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand All @@ -120,46 +120,40 @@ public function eof()
return feof($this->socket);
}

public function isSeekable()
public function isSeekable(): bool
{
return false;
}

/**
* @return void
*/
public function seek($offset, $whence = SEEK_SET)
public function seek($offset, $whence = SEEK_SET): void
{
throw new StreamException('This stream is not seekable');
}

/**
* @return void
*/
public function rewind()
public function rewind(): void
{
throw new StreamException('This stream is not seekable');
}

public function isWritable()
public function isWritable(): bool
{
return false;
}

public function write($string)
public function write($string): int
{
throw new StreamException('This stream is not writable');
}

public function isReadable()
public function isReadable(): bool
{
return true;
}

/**
* @param int<0, max> $length
*/
public function read($length)
public function read($length): string
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand Down Expand Up @@ -197,7 +191,7 @@ public function read($length)
return $read;
}

public function getContents()
public function getContents(): string
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand Down
6 changes: 2 additions & 4 deletions tests/SocketHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
use Http\Client\Socket\Client as SocketHttpClient;
use Http\Client\Socket\Exception\NetworkException;
use Http\Client\Socket\Exception\TimeoutException;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Nyholm\Psr7\Factory\Psr17Factory;

class SocketHttpClientTest extends BaseTestCase
{
public function createClient($options = [])
{
$messageFactory = new GuzzleMessageFactory();

return new HttpMethodsClient(new SocketHttpClient($options), $messageFactory);
return new HttpMethodsClient(new SocketHttpClient($options), new Psr17Factory());
}

public function testTcpSocketDomain()
Expand Down

0 comments on commit f4b9555

Please sign in to comment.