Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Senders lazy #158

Merged
merged 1 commit into from
Jan 23, 2025
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
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"websockets",
"server",
"helper",
"dev"
"dev",
"fibers",
"dumper"
],
"authors": [
{
Expand All @@ -28,17 +30,13 @@
],
"homepage": "https://buggregator.dev/",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/buggregator"
},
{
"type": "patreon",
"url": "https://patreon.com/roxblnfk"
},
{
"type": "patreon",
"url": "https://patreon.com/butschster"
"type": "boosty",
"url": "https://boosty.to/roxblnfk"
}
],
"require": {
Expand Down
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Command/Run.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.2, locked)

Ignored error pattern #^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$# in path /home/runner/work/trap/trap/src/Command/Run.php was not matched in reported errors.

Check failure on line 1 in src/Command/Run.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.2, locked)

Ignored error pattern #^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$# in path /home/runner/work/trap/trap/src/Command/Run.php was not matched in reported errors.

declare(strict_types=1);

Expand Down Expand Up @@ -80,17 +80,11 @@
public function createRegistry(OutputInterface $output): Sender\SenderRegistry
{
$registry = new Sender\SenderRegistry();
$registry->register('console', Sender\ConsoleSender::create($output));
$registry->register('file', new Sender\EventsToFileSender());
$registry->register('file-body', new Sender\BodyToFileSender());
$registry->register('mail-to-file', new Sender\MailToFileSender());
$registry->register(
'server',
new Sender\RemoteSender(
host: '127.0.0.1',
port: 9099,
),
);
$registry->register('console', static fn(): Sender => Sender\ConsoleSender::create($output));
$registry->register('file', static fn(): Sender => new Sender\EventsToFileSender());
$registry->register('file-body', static fn(): Sender => new Sender\BodyToFileSender());
$registry->register('mail-to-file', static fn(): Sender => new Sender\MailToFileSender());
$registry->register('server', static fn(): Sender => new Sender\RemoteSender(host: '127.0.0.1', port: 9099));

Check warning on line 87 in src/Command/Run.php

View check run for this annotation

Codecov / codecov/patch

src/Command/Run.php#L83-L87

Added lines #L83 - L87 were not covered by tests

return $registry;
}
Expand Down
16 changes: 9 additions & 7 deletions src/Sender/SenderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
/** @var array<non-empty-string, Sender> */
private array $senders = [];

/** @var array<non-empty-string, \Closure(): Sender> */
private array $factory = [];

/**
* @param non-empty-string $name
* @param callable(): Sender $factory
*/
public function register(string $name, Sender $sender): void
public function register(string $name, callable $factory): void

Check warning on line 24 in src/Sender/SenderRegistry.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/SenderRegistry.php#L24

Added line #L24 was not covered by tests
{
$this->senders[$name] = $sender;
$this->factory[$name] = $factory(...);

Check warning on line 26 in src/Sender/SenderRegistry.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/SenderRegistry.php#L26

Added line #L26 was not covered by tests
}

/**
Expand All @@ -30,11 +34,9 @@
{
$senders = [];
foreach ($types as $type) {
if (!isset($this->senders[$type])) {
throw new \InvalidArgumentException(\sprintf('Unknown sender type "%s"', $type));
}

$senders[] = $this->senders[$type];
$senders[] = $this->senders[$type] ?? ($this->factory[$type] ?? throw new \InvalidArgumentException(
"Unknown sender type `{$type}`.",
))();

Check warning on line 39 in src/Sender/SenderRegistry.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/SenderRegistry.php#L37-L39

Added lines #L37 - L39 were not covered by tests
}

return $senders;
Expand Down
Loading