-
-
Notifications
You must be signed in to change notification settings - Fork 28
Refactor: Introduce fluent API for dispatch() helper function #998
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
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
91b975f
refactor: introduce fluent API for dispatch() helper function
huangdijia 097eed9
docs: add warning about return value assignment in dispatch() function
huangdijia bfd98db
test: add comprehensive tests for fluent dispatch API
huangdijia ab8931a
test: refactor DispatchTest to improve container mock setup and excep…
huangdijia 1c87ac2
Update dispatch() return type in test
huangdijia 69813b4
test: add type assertion for dispatch() with closure
huangdijia e6891a1
Apply suggestion from @huangdijia
huangdijia e5573f7
Apply suggestion from @huangdijia
huangdijia 16dbeb2
feat: add withHeader method to PendingAmqpProducerMessageDispatch for…
huangdijia 1127b22
feat: add setKey and setValue methods to PendingKafkaProducerMessageD…
huangdijia cb2ca09
feat: enhance dispatch function to check class existence before type …
huangdijia dee0cf0
Refactor dispatching mechanism and remove deprecated classes
huangdijia 22dd7e4
feat: 添加 AsyncTaskInterface 的使用声明
huangdijia 04beb0d
fix: 修复导入语句,确保正确使用 Hyperf\Amqp\Producer
huangdijia 7ef7ce2
fix: 修复 dispatch() 测试的返回类型,确保返回布尔值
huangdijia dc71e9c
fix: 修复导入语句,确保正确使用 FriendsOfHyperf\Support\dispatch 函数
huangdijia 8095595
fix: 添加弃用注释,建议使用 FriendsOfHyperf\Support\dispatch() 替代
huangdijia ae5fd97
fix: 添加对 ProducerMessage 的 setPoolName 方法的模拟,确保正确处理自定义池名称
huangdijia af2c45a
更新 Functions.php
huangdijia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Support; | ||
|
|
||
| use Hyperf\Amqp\Message\ProducerMessageInterface; | ||
| use Hyperf\Amqp\Producer; | ||
| use Hyperf\Conditionable\Conditionable; | ||
| use Hyperf\Context\ApplicationContext; | ||
|
|
||
| /** | ||
| * @property array{application_headers?:AMQPTable} $properties | ||
| */ | ||
| class PendingAmqpProducerMessageDispatch | ||
| { | ||
| use Conditionable; | ||
|
|
||
| public ?string $pool = null; | ||
|
|
||
| public int $timeout = 5; | ||
|
|
||
| public bool $confirm = false; | ||
|
|
||
| public function __construct(protected ProducerMessageInterface $message) | ||
| { | ||
| } | ||
|
|
||
| public function __destruct() | ||
| { | ||
| $this->pool && $this->message->setPoolName($this->pool); | ||
| ApplicationContext::getContainer() | ||
| ->get(Producer::class) | ||
| ->produce($this->message, $this->confirm, $this->timeout); | ||
| } | ||
|
|
||
| public function onPool(string $pool): static | ||
| { | ||
| $this->pool = $pool; | ||
| return $this; | ||
| } | ||
|
|
||
| public function setPayload(mixed $data): static | ||
| { | ||
| $this->message->setPayload($data); | ||
| return $this; | ||
| } | ||
|
|
||
| public function withHeader(string $key, mixed $value, ?int $ttl = null): static | ||
| { | ||
| (function () use ($key, $value, $ttl) { | ||
| $this->properties['application_headers'] ??= new \PhpAmqpLib\Wire\AMQPTable(); // @phpstan-ignore-line | ||
| $this->properties['application_headers']->set($key, $value, $ttl); | ||
| })->call($this->message); | ||
| return $this; | ||
| } | ||
|
|
||
| public function setConfirm(bool $confirm): static | ||
| { | ||
| $this->confirm = $confirm; | ||
| return $this; | ||
| } | ||
|
|
||
| public function setTimeout(int $timeout): static | ||
| { | ||
| $this->timeout = $timeout; | ||
| return $this; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Support; | ||
|
|
||
| use Hyperf\AsyncQueue\Driver\DriverFactory; | ||
| use Hyperf\AsyncQueue\JobInterface; | ||
| use Hyperf\Conditionable\Conditionable; | ||
| use Hyperf\Context\ApplicationContext; | ||
|
|
||
| class PendingAsyncQueueDispatch | ||
| { | ||
| use Conditionable; | ||
|
|
||
| public string $pool = 'default'; | ||
|
|
||
| public int $delay = 0; | ||
|
|
||
| public function __construct(protected JobInterface $job) | ||
| { | ||
| } | ||
|
|
||
| public function __destruct() | ||
| { | ||
| ApplicationContext::getContainer() | ||
| ->get(DriverFactory::class) | ||
| ->get($this->pool) | ||
| ->push($this->job, $this->delay); | ||
| } | ||
|
|
||
| public function setMaxAttempts(int $maxAttempts): static | ||
| { | ||
| $this->job->setMaxAttempts($maxAttempts); | ||
| return $this; | ||
| } | ||
|
|
||
| public function onPool(string $pool): static | ||
| { | ||
| $this->pool = $pool; | ||
| return $this; | ||
| } | ||
|
|
||
| public function delay(int $delay): static | ||
| { | ||
| $this->delay = $delay; | ||
| return $this; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Support; | ||
|
|
||
| use Hyperf\Conditionable\Conditionable; | ||
| use Hyperf\Context\ApplicationContext; | ||
| use Hyperf\Kafka\ProducerManager; | ||
| use longlang\phpkafka\Producer\ProduceMessage; | ||
| use longlang\phpkafka\Protocol\RecordBatch\RecordHeader; | ||
|
|
||
| /** | ||
| * @property array $headers | ||
| * @property null|string $key | ||
| * @property null|string $value | ||
| */ | ||
| class PendingKafkaProducerMessageDispatch | ||
| { | ||
| use Conditionable; | ||
|
|
||
| public string $pool = 'default'; | ||
|
|
||
| public function __construct(protected ProduceMessage $message) | ||
| { | ||
| } | ||
|
|
||
| public function __destruct() | ||
| { | ||
| ApplicationContext::getContainer() | ||
| ->get(ProducerManager::class) | ||
| ->getProducer($this->pool) | ||
| ->sendBatch([$this->message]); | ||
| } | ||
|
|
||
| public function onPool(string $pool): static | ||
| { | ||
| $this->pool = $pool; | ||
| return $this; | ||
| } | ||
|
|
||
| public function setKey(string $key): static | ||
| { | ||
| (fn () => $this->key = $key)->call($this->message); | ||
| return $this; | ||
| } | ||
|
|
||
| public function setValue(string $value): static | ||
| { | ||
| (fn () => $this->value = $value)->call($this->message); | ||
| return $this; | ||
| } | ||
|
|
||
| public function withHeader(string $key, string $value): static | ||
| { | ||
| $header = (new RecordHeader())->setHeaderKey($key)->setValue($value); | ||
| (fn () => $this->headers[] = $header)->call($this->message); | ||
| return $this; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.