Skip to content

Commit

Permalink
Use generator instead of function argument
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Sep 10, 2023
1 parent 28383b0 commit 7adb5ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 53 deletions.
61 changes: 21 additions & 40 deletions src/Controller/DevServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,45 @@

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
use Yiisoft\Http\Header;
use Yiisoft\Yii\Debug\Api\ServerSentEventsStream;
use Yiisoft\Yii\Debug\DevServer\Connection;

final class DevServerController
{
public function __construct(
private DataResponseFactoryInterface $responseFactory,
private Aliases $aliases,
) {
}

public function stream(

Check warning on line 15 in src/Controller/DevServerController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/DevServerController.php#L15

Added line #L15 was not covered by tests
ResponseFactoryInterface $responseFactory
): ResponseInterface {
$maxRetries = 1;
$retries = 0;

if (\function_exists('pcntl_signal')) {
\pcntl_signal(\SIGINT, static function (): void {
exit(1);
});

Check warning on line 21 in src/Controller/DevServerController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/DevServerController.php#L18-L21

Added lines #L18 - L21 were not covered by tests
}

$socket = Connection::create();

Check failure on line 24 in src/Controller/DevServerController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

UndefinedClass

src/Controller/DevServerController.php:24:19: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Debug\DevServer\Connection does not exist (see https://psalm.dev/019)

Check failure on line 24 in src/Controller/DevServerController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Controller/DevServerController.php:24:19: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Debug\DevServer\Connection does not exist (see https://psalm.dev/019)

Check failure on line 24 in src/Controller/DevServerController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Controller/DevServerController.php:24:19: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Debug\DevServer\Connection does not exist (see https://psalm.dev/019)
$socket->bind();

Check warning on line 25 in src/Controller/DevServerController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/DevServerController.php#L24-L25

Added lines #L24 - L25 were not covered by tests

return $responseFactory->createResponse()
->withHeader('Content-Type', 'text/event-stream')
->withHeader('Cache-Control', 'no-cache')
->withHeader('Connection', 'keep-alive')
->withHeader(Header::CONTENT_TYPE, 'text/event-stream')
->withHeader(Header::CACHE_CONTROL, 'no-cache')
->withHeader(Header::CONNECTION, 'keep-alive')
->withBody(
new ServerSentEventsStream(function (array &$buffer) use (
&$hash,
&$retries,
$maxRetries,
) {
$socket = Connection::create();
$socket->bind();
$messages = $socket->read(
fn (string $data) => $data,
fn () => yield 0x001,
);
foreach ($messages as $message) {
if ($message === 0x001) {
return true;
new ServerSentEventsStream(function () use ($socket) {
foreach ($socket->read() as $message) {
switch ($message[0]) {
case Connection::TYPE_ERROR:
return '';

Check warning on line 36 in src/Controller/DevServerController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/DevServerController.php#L27-L36

Added lines #L27 - L36 were not covered by tests
default:
/**
* Break the loop if the client aborted the connection (closed the page)
*/
if (connection_aborted()) {
return $message[1];

Check warning on line 42 in src/Controller/DevServerController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/DevServerController.php#L41-L42

Added lines #L41 - L42 were not covered by tests
}
yield $message[1];

Check warning on line 44 in src/Controller/DevServerController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/DevServerController.php#L44

Added line #L44 was not covered by tests
}
$buffer[] = $message;

// break the loop if the client aborted the connection (closed the page)
if (connection_aborted()) {
return true;
}
if ($retries++ >= $maxRetries) {
return true;
}

sleep(1);
return true;
}
return '';
})
);

Check warning on line 49 in src/Controller/DevServerController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/DevServerController.php#L47-L49

Added lines #L47 - L49 were not covered by tests
}
Expand Down
25 changes: 12 additions & 13 deletions src/ServerSentEventsStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace Yiisoft\Yii\Debug\Api;

use Closure;
use Generator;
use Psr\Http\Message\StreamInterface;

final class ServerSentEventsStream implements StreamInterface, \Stringable
{
public array $buffer = [];
private bool $eof = false;

/**
* @param Closure(): Generator $stream
*/
public function __construct(
private Closure $stream,
) {
Expand Down Expand Up @@ -77,24 +80,20 @@ public function isReadable(): bool
*/
public function read(int $length): string
{
$continue = ($this->stream)($this->buffer);
foreach (($this->stream)($this) as $message) {

Check failure on line 83 in src/ServerSentEventsStream.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

TooManyArguments

src/ServerSentEventsStream.php:83:18: TooManyArguments: Too many arguments for - expecting 0 but saw 1 (see https://psalm.dev/026)

Check failure on line 83 in src/ServerSentEventsStream.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

TooManyArguments

src/ServerSentEventsStream.php:83:18: TooManyArguments: Too many arguments for - expecting 0 but saw 1 (see https://psalm.dev/026)

Check failure on line 83 in src/ServerSentEventsStream.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

TooManyArguments

src/ServerSentEventsStream.php:83:18: TooManyArguments: Too many arguments for - expecting 0 but saw 1 (see https://psalm.dev/026)
if (empty($message)) {
break;

Check warning on line 85 in src/ServerSentEventsStream.php

View check run for this annotation

Codecov / codecov/patch

src/ServerSentEventsStream.php#L83-L85

Added lines #L83 - L85 were not covered by tests
}

if (!$continue) {
$this->eof = true;
return sprintf("data: %s\n\n", $message);

Check warning on line 88 in src/ServerSentEventsStream.php

View check run for this annotation

Codecov / codecov/patch

src/ServerSentEventsStream.php#L88

Added line #L88 was not covered by tests
}

$output = '';
foreach ($this->buffer as $key => $value) {
unset($this->buffer[$key]);
$output .= sprintf("data: %s\n", $value);
}
$output .= "\n";
return $output;
$this->eof = true;
return '';

Check warning on line 91 in src/ServerSentEventsStream.php

View check run for this annotation

Codecov / codecov/patch

src/ServerSentEventsStream.php#L90-L91

Added lines #L90 - L91 were not covered by tests
}

public function getContents(): string
{
return $this->read(1024);
return $this->read(8_388_608); // 8MB

Check warning on line 96 in src/ServerSentEventsStream.php

View check run for this annotation

Codecov / codecov/patch

src/ServerSentEventsStream.php#L96

Added line #L96 was not covered by tests
}

public function getMetadata($key = null): array
Expand Down

0 comments on commit 7adb5ea

Please sign in to comment.