Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Put env variables defaults here
# Override locally in gitignored .env.local
PHP_IMAGE_VERSION=8.3
PHP_IMAGE_VERSION=8.4
2 changes: 1 addition & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
strategy: &strategy
fail-fast: false
matrix:
php: [ 8.3, 8.4, 8.5 ]
php: [ 8.4, 8.5 ]
deps: [ lowest, highest ]
steps:
- uses: actions/checkout@v6
Expand Down
4 changes: 2 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
use PHPyh\CodingStandard\PhpCsFixerCodingStandard;

$config = (new Config())
$config = new Config()
->setFinder(
Finder::create()
->in(__DIR__ . '/examples')
Expand All @@ -20,7 +20,7 @@
->setParallelConfig(ParallelConfigFactory::detect())
->setCacheFile(__DIR__ . '/var/' . basename(__FILE__) . '.cache');

(new PhpCsFixerCodingStandard())->applyTo($config, [
new PhpCsFixerCodingStandard()->applyTo($config, [
'numeric_literal_separator' => false,
]);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^8.3",
"php": "^8.4",
"ext-filter": "*",
"amphp/amp": "^3.1.1",
"amphp/byte-stream": "^2.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/jetstream/message-get.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
subject: 'events.activated',
message: new Nats\Message(
payload: 'Message',
headers: (new Nats\Headers())
headers: new Nats\Headers()
->with(Nats\Header\ScalarKey::string('x'), 'y'),
),
);
Expand Down
6 changes: 3 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,14 +819,14 @@ private function connection(?Cancellation $cancellation = null): Connection\Conn
/** @var ?\Throwable $reason the fatal server error that closed the connection */
$reason = null;

$connection->hooks()->onMessage($this->invokeSubscriber(...));
$connection->hooks()->onError(function (Hooks\ErrorReceived $event) use (
$connection->hooks->onMessage($this->invokeSubscriber(...));
$connection->hooks->onError(function (Hooks\ErrorReceived $event) use (
$connection,
&$reason,
): void {
$reason ??= $this->handleError($event, $connection);
});
$connection->hooks()->onClose(function () use (
$connection->hooks->onClose(function () use (
$connection,
&$reason,
): void {
Expand Down
8 changes: 1 addition & 7 deletions src/Description.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ public function is(string ...$values): bool
*/
public function startsWith(string ...$prefixes): bool
{
foreach ($prefixes as $prefix) {
if (str_starts_with($this->value, $prefix)) {
return true;
}
}

return false;
return array_any($prefixes, fn(string $prefix): bool => str_starts_with($this->value, $prefix));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Connection
*/
public function execute(Protocol\Frame|array $frames, ?Cancellation $cancellation = null): void;

public function hooks(): Hooks\Provider;
public Hooks\Provider $hooks { get; }

/**
* @throws NatsException
Expand Down
6 changes: 3 additions & 3 deletions src/Internal/Connection/PingPongHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public function startup(int $interval, int $maxPings): void
$this->connection->execute(Protocol\Ping::Frame);
});

$this->connection->hooks()->onPing(function (): void {
$this->connection->hooks->onPing(function (): void {
$this->connection->execute(Protocol\Pong::Frame);
});

$this->connection->hooks()->onPong(function (): void {
$this->connection->hooks->onPong(function (): void {
$this->pings = 0;
});

$this->connection->hooks()->onClose($this->stop(...));
$this->connection->hooks->onClose($this->stop(...));
}

public function stop(): void
Expand Down
7 changes: 1 addition & 6 deletions src/Internal/Connection/SocketConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class SocketConnection implements Connection
/** @var \SplQueue<DeferredFuture<Protocol\Frame>> */
private readonly \SplQueue $queue;

private readonly Hooks\ConcurrentProvider $hooks;
public readonly Hooks\ConcurrentProvider $hooks;

private readonly PingPongHandler $pingpongs;

Expand Down Expand Up @@ -163,11 +163,6 @@ public function execute(Protocol\Frame|array $frames, ?Cancellation $cancellatio
}
}

public function hooks(): Hooks\Provider
{
return $this->hooks;
}

public function info(): ConnectionInfo
{
return $this->info ?? throw new ConnectionIsNotAvailable();
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Connection/SocketConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function __construct(

public static function fromConfig(Config $config): self
{
$context = (new ConnectContext())
$context = new ConnectContext()
->withConnectTimeout($config->connectionTimeout);

if ($config->tcpNoDelay) {
Expand Down
6 changes: 3 additions & 3 deletions src/JetStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ public function createOrUpdateCounter(Counter\CounterConfig $config): Counter\Co
prefix: $prefix = "\$CNT.{$config->name}.",
publishPrefix: $this->publishPrefix(
prefix: $prefix,
jsPrefix: $this->router->prefix() !== Api\Router::DEFAULT_PREFIX ? $this->router->prefix() : null,
jsPrefix: $this->router->prefix !== Api\Router::DEFAULT_PREFIX ? $this->router->prefix : null,
),
);
}
Expand Down Expand Up @@ -786,7 +786,7 @@ private function keyValueStreamConfig(KeyValue\BucketConfig $config): Api\Stream
*/
private function setupKeyValue(string $bucket, JetStream\Stream $stream): KeyValue\Bucket
{
$jsPrefix = $this->router->prefix() !== Api\Router::DEFAULT_PREFIX ? $this->router->prefix() : '';
$jsPrefix = $this->router->prefix !== Api\Router::DEFAULT_PREFIX ? $this->router->prefix : '';

$prefix = "\$KV.{$bucket}.";
$putPrefix = "{$jsPrefix}{$prefix}";
Expand Down Expand Up @@ -914,7 +914,7 @@ private function paginatedRequest(Api\PaginatedRequest $request): iterable

$offset += \count($response);

if ($offset >= $response->total()) {
if ($offset >= $response->total) {
break;
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/JetStream/Api/ConsumerInfoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public function __construct(
public ?array $consumers = null,
) {}

public function total(): int
{
return $this->total;
}

public function getIterator(): \Traversable
{
yield from $this->consumers ?? [];
Expand Down
5 changes: 0 additions & 5 deletions src/JetStream/Api/ConsumerNamesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public function __construct(
public ?array $consumers = null,
) {}

public function total(): int
{
return $this->total;
}

public function getIterator(): \Traversable
{
yield from $this->consumers ?? [];
Expand Down
4 changes: 2 additions & 2 deletions src/JetStream/Api/PaginatedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface PaginatedResponse extends
\Countable
{
/**
* @return non-negative-int
* @var non-negative-int
*/
public function total(): int;
public int $total { get; }
}
16 changes: 3 additions & 13 deletions src/JetStream/Api/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,19 @@
/**
* @internal
*/
final class Router
final readonly class Router
{
public const string DEFAULT_PREFIX = '$JS.API.';

/** @var non-empty-string */
private string $prefix = self::DEFAULT_PREFIX;
public string $prefix;

/**
* @param ?non-empty-string $domain
*/
public function __construct(?string $domain = null)
{
if ($domain !== null) {
$this->prefix = substr_replace($this->prefix, "\$JS.{$domain}.API.", 0);
}
}

/**
* @return non-empty-string
*/
public function prefix(): string
{
return $this->prefix;
$this->prefix = $domain !== null ? "\$JS.{$domain}.API." : self::DEFAULT_PREFIX;
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/JetStream/Api/StreamInfoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public function __construct(
public ?array $streams = null,
) {}

public function total(): int
{
return $this->total;
}

public function getIterator(): \Traversable
{
yield from $this->streams ?? [];
Expand Down
5 changes: 0 additions & 5 deletions src/JetStream/Api/StreamNamesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public function __construct(
public ?array $streams = null,
) {}

public function total(): int
{
return $this->total;
}

public function getIterator(): \Traversable
{
yield from $this->streams ?? [];
Expand Down
6 changes: 3 additions & 3 deletions src/JetStream/KeyValue/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function delete(string $key, ?int $revision = null): void
$this->publish(
$key,
new Message(
headers: (new Headers())
headers: new Headers()
->with(Header\KvOperation::header(), Header\KvOperation::OP_DEL),
),
new PublishOptions(expectedLastSubjectSequence: $revision),
Expand All @@ -180,7 +180,7 @@ public function purge(
$this->publish(
$key,
new Message(
headers: (new Headers())
headers: new Headers()
->with(Header\KvOperation::header(), Header\KvOperation::OP_PURGE)
->with(Header\MsgRollup::header(), Header\MsgRollup::ROLLUP_SUBJECT),
),
Expand Down Expand Up @@ -294,7 +294,7 @@ private function initialEntries(string|array $filters, WatchConfig $config, ?Can
}
}

return $entries->entries();
return $entries->entries;
}

/**
Expand Down
10 changes: 1 addition & 9 deletions src/JetStream/KeyValue/Internal/EntryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@
final class EntryCollector
{
/** @var list<Entry> */
private array $entries = [];
public private(set) array $entries = [];

public function __invoke(Entry $entry): void
{
$this->entries[] = $entry;
}

/**
* @return list<Entry>
*/
public function entries(): array
{
return $this->entries;
}
}
2 changes: 1 addition & 1 deletion src/JetStream/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function parse(string $subject): self
delivered: (int) $tokens[4],
streamSequence: (int) $tokens[5],
consumerSequence: (int) $tokens[6],
timestamp: (new \DateTimeImmutable())->setTimestamp(
timestamp: new \DateTimeImmutable()->setTimestamp(
TimeSpan::fromNanoseconds((int) $tokens[7])->toSeconds(),
),
pending: (int) $tokens[8],
Expand Down
6 changes: 3 additions & 3 deletions src/JetStream/ObjectStore/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function put(ObjectMeta $meta, Reader|string $object): ObjectInfo
subject: $subject,
message: new Message(
payload: $this->json->encode($objectInfo),
headers: (new Headers())
headers: new Headers()
->with(MsgRollup::header(), MsgRollup::ROLLUP_SUBJECT),
),
);
Expand Down Expand Up @@ -204,7 +204,7 @@ public function delete(string $name): void
subject: "\$O.{$info->bucket}.M." . self::base64encode($info->name),
message: new Message(
payload: $this->json->encode($info),
headers: (new Headers())
headers: new Headers()
->with(MsgRollup::header(), MsgRollup::ROLLUP_SUBJECT),
),
);
Expand Down Expand Up @@ -313,7 +313,7 @@ public function addLink(string $name, ObjectInfo $object): ObjectInfo
subject: "\$O.{$info->bucket}.M." . self::base64encode($info->name),
message: new Message(
payload: $this->json->encode($info->withoutTime()),
headers: (new Headers())
headers: new Headers()
->with(MsgRollup::header(), MsgRollup::ROLLUP_SUBJECT),
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
public static function incr(int $value): self
{
return new self(
headers: (new Headers())
headers: new Headers()
->with(Incr::Header, $value),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serialization/ValinorSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public function __construct(?string $cachePath = null, bool $debug = false)
{
$builder = (new MapperBuilder())
$builder = new MapperBuilder()
->allowSuperfluousKeys()
->registerConstructor(
TimeSpan::fromNanoseconds(...),
Expand Down
2 changes: 1 addition & 1 deletion tests/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testHeaders(): void

public function testSequenceHeaders(): void
{
$headers = (new Headers())
$headers = new Headers()
->with(Header\LastSequence::Header, 10)
->with(Header\UpToSequence::Header, 20);

Expand Down
3 changes: 2 additions & 1 deletion tests/Internal/Connection/PingPongHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Thesis\Nats\Internal\Connection;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Runtime\PropertyHook;
use PHPUnit\Framework\TestCase;
use Thesis\Nats\Internal\Hooks\ConcurrentProvider;
use Thesis\Nats\Internal\Hooks\PingReceived;
Expand All @@ -21,7 +22,7 @@ public function testPingPong(): void

$connection = $this->createMock(Connection::class);
$connection
->method('hooks')
->method(PropertyHook::get('hooks'))
->willReturn($provider);

$connection
Expand Down
Loading
Loading