定制 temporal-php/support 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

temporal-php/support

Composer 安装命令:

composer require temporal-php/support

包简介

Helpers that simplify working with the Temporal PHP SDK

README 文档

README

Temporal PHP Support

Enhance your development experience with Temporal

The package includes attributes, helpers, factories, interfaces, interceptors, etc. to enhance the developer experience when using the Temporal PHP SDK.

Installation

To install the package in your PHP application, add it as a dev dependency to your project using Composer:

composer require temporal-php/support

PHP Latest Version on Packagist License Total Downloads

Usage

Factories

The package provides factories to create Activity and Worker stubs in a more convenient way. With these factories, there is less code because all nested options are moved to the parameters of one method.

Use the \Temporal\Support\Factory\ActivityStub factory to create an Activity stub:

use \Temporal\Support\Factory\ActivityStub;

#[\Temporal\Workflow\WorkflowInterface]
class HelloWorkflow {
    #[\Temporal\Workflow\WorkflowMethod]
    public function run(string $user) {
        yield ActivityStub::activity(
            class: UserService::class,
            startToCloseTimeout: 60,
            retryAttempts: 5,
        )->getContactEmail($user)->then(
            fn (string $email) => ActivityStub::activity(
                class: HelloService::class,
                startToCloseTimeout: '10 minutes',
                retryAttempts: 5,
            )->sendHelloEmail($user, $email),
        );
    }
}

Use the \Temporal\Support\Factory\WorkflowStub factory to create a Workflow stub in a client scope:

use \Temporal\Support\Factory\WorkflowStub;
/**
 * @var \Temporal\Client\WorkflowClient $client
 */
$stub = WorkflowStub::workflow($client, HelloWorkflow::class, executionTimeout: '1 day');
$run = $client->start($stub, 'User');
// ...

Or create a Child Workflow stub in a workflow scope:

use \Temporal\Support\Factory\WorkflowStub;

#[\Temporal\Workflow\WorkflowInterface]
class RegisterWorkflow {
    #[\Temporal\Workflow\WorkflowMethod]
    public function run(string $user) {
        yield \Temporal\Promise::all([
            WorkflowStub::childWorkflow(GreetingWorkflow::class, executionTimeout: '1 hour')->greet($user),
            WorkflowStub::childWorkflow(SubscribeNewsWorkflow::class, executionTimeout: '10 minutes')->subscribe($user),
            WorkflowStub::childWorkflow(PrepareUserSpaceWorkflow::class, executionTimeout: '1 hour')->handle($user),
        ])->then(
            // Suppress failures
            onRejected: static fn () => null,
        );

        // ...
    }
}

Attributes

Attributes can be used on Workflow or Activity definitions to set default stub options.

#[\Temporal\Support\Attribute\TaskQueue('my-task-queue')]
#[\Temporal\Support\Attribute\RetryPolicy(attempts: 5)]
#[WorkflowInterface]
interface HelloWorkflow {
    #[WorkflowMethod]
    public function greet(string $name);
}

$stub = \Temporal\Support\Factory\WorkflowStub::workflow($client, HelloWorkflow::class);

// TaskQueue is now set to 'my-task-queue' and RetryPolicy to 5 attempts
$stub->greet('User');

// You can override the default options
$stub = \Temporal\Support\Factory\WorkflowStub::workflow(
    $client,
    HelloWorkflow::class,
    taskQueue: 'another-task-queue',
    retryAttempts: 1,
)->greet('User');

Note

Attributes will work only if you use the Activity and Worker factories from this package.

Warning

Use attributes on the definitions that you use in factories. So, if you separate interfaces and implementation, apply attributes to the interfaces.

VirtualPromise interface

Every time we use yield in a Workflow to wait for an action to complete, a Promise is actually yielded. At this point, the IDE and static analyzer usually get lost in type definitions, and we experience difficulties and inconveniences because of this. However, if the Promise interface had the @yield annotation, we could explain to the IDE what type of value we expect to be sent back into the generator from the coroutine. Since ReactPHP isn't yet planning to add the @yield annotation to their promises (Temporal PHP uses ReactPHP promises), we suggest using our solution for typing - VirtualPromise.

use Temporal\Support\VirtualPromise;

#[\Temporal\Activity\ActivityInterface]
class HelloService {
    /**
     * @param non-empty-string $name
     *
     * @return VirtualPromise<non-empty-string>
     */
    public function greet(string $name) {
        // ...
    }
}

#[\Temporal\Workflow\WorkflowInterface]
class WorkflowClass {
    #[\Temporal\Workflow\WorkflowMethod]
    public function run(string $name) {
        $activity = \Temporal\Support\Factory\ActivityStub::activity(HelloService::class);

        // IDE will know that $name is a non-empty-string
        $name = yield $activity->greet($name);
        // ...
    }
}

Warning

don't implement the VirtualPromise interface yourself, use it only as a type hint.

Note

PHPStorm and Psalm can handle the @yield annotation, but PHPStan can't yet (issue).

Contributing

We believe in the power of community-driven development. Here's how you can contribute:

  • Report Bugs: Encounter a glitch? Let us know on our issue tracker.
  • Feature Suggestions: Have ideas to improve the package? Create a feature request!
  • Code Contributions: Submit a pull request to help us improve the codebase. You can find a list of issues labeled "help wanted" here.
  • Spread the Word: Share your experience with the package on social media and encourage others to contribute.
  • Donate: Support our work by becoming a patron or making a one-time donation
    roxblnfk butschster

temporal-php/support 适用场景与选型建议

temporal-php/support 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.73k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2024 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「helper」 「temporal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 temporal-php/support 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 temporal-php/support 我们能提供哪些服务?
定制开发 / 二次开发

基于 temporal-php/support 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-21