survos/state-bundle
Composer 安装命令:
composer require survos/state-bundle
包简介
Add some tools managing state machines using the Symfony Workflow Component
关键字:
README 文档
README
Configure a workflow using PHP attributes. Prefer separating the durable workflow definition from the event listener/orchestrator:
*Flowis the attribute definition class, for exampleImageFloworSubmissionFlow.*Workflowis the listener/service class that reacts to transitions, queues work, and applies app policy, for exampleImageWorkflow.
Older apps may still use *WF, *WorkflowInterface, or a single class that both defines and handles the workflow. New code should use *Flow for the definition because it is short, readable, and leaves Workflow for the runtime service.
auto-registration!
Workflow Constants In Twig
The bundle now exposes additive Twig helpers for resolving workflow definition constants without hard-coding raw place or transition strings in templates.
{% set removePlace = workflow_const(image, 'PLACE_REMOVE') %}
{% if image.marking != removePlace %}
...
{% endif %}
You can also resolve by workflow name:
{% set removeTransition = workflow_const('ImageFlow', 'TRANSITION_REMOVE') %}
Available helpers:
workflow_const(subjectOrWorkflow, constantName): resolves a PHP constant from the workflow definition classworkflow_name(subjectOrWorkflow): resolves the workflow name from a subject or returns the provided workflow namesurvos_workflow_metadata(workflowName, key, metadataSubject): existing metadata helper for workflow/place/transition metadata
This is additive. Existing metadata helpers and app-level Twig extensions can remain in place.
How It Works
During bundle prepend/compile time, AttributesWorkflowConfigBuilder now publishes an internal map of:
workflow name => workflow definition classsupported entity class => workflow definition class[]
WorkflowHelperService uses that map to resolve the workflow definition class for either:
- a workflow name like
ImageFlow - an entity instance like
App\Entity\Image
That lets Twig resolve constants from the actual PHP workflow definition instead of relying on brittle string literals in templates.
Tests
The bundle now includes PHPUnit 13-compatible unit tests covering:
- compile-time workflow definition mapping
- constant resolution in
WorkflowHelperService - Twig helper exposure in
WorkflowExtension
Run them from the bundle root:
composer install vendor/bin/phpunit
Vibing
Doctrine-free jsonl workflow: https://claude.ai/share/9c89f52c-1655-44b6-bb86-d773d29bc20b
@todo: https://joppe.dev/2024/10/11/dynamic-workflows-with-symfony-workflow-component/
for easyadmin integration, also see https://github.com/WandiParis/EasyAdminPlusBundle
<?php // SubmissionFlow.php namespace App\Workflow; use App\Entity\Submission; use Survos\StateBundle\Attribute\Place; use Survos\StateBundle\Attribute\Transition; use Survos\StateBundle\Attribute\Workflow; #[Workflow(supports: [Submission::class], name: self::WORKFLOW_NAME)] final class SubmissionFlow { const WORKFLOW_NAME='SubmissionFlow'; #[Place(initial: true, metadata: ['description' => "starting place after submission"])] const PLACE_NEW='new'; #[Place(metadata: ['description' => "waiting for admin approval"])] const PLACE_WAITING='waiting'; const PLACE_APPROVED='approved'; const PLACE_REJECTED='rejected'; const PLACE_WITHDRAWN='withdrawn'; #[Transition(from:[self::PLACE_NEW], to: self::PLACE_WAITING)] const TRANSITION_SUBMIT='submit'; #[Transition(from:[self::PLACE_NEW], to: self::PLACE_APPROVED, guard: "is_granted('ROLE_ADMIN')")] const TRANSITION_APPROVE='approve'; #[Transition(from:[self::PLACE_NEW], to: self::PLACE_REJECTED, guard: "is_granted('ROLE_ADMIN')")] const TRANSITION_REJECT='reject'; #[Transition(from:[self::PLACE_NEW, self::PLACE_APPROVED], to: self::PLACE_WITHDRAWN, guard: "is_granted('ROLE_USER')")] const TRANSITION_WITHDRAW='withdrawn'; #[Transition(from:[self::PLACE_REJECTED, self::PLACE_APPROVED], to: self::PLACE_NEW)] const TRANSITION_RESET='reset'; }
Now create a separate SubmissionWorkflow service/listener that uses these constants and acts on workflow events. The definition class stays declarative; the workflow class owns behavior.
symfony new workflow-demo --webapp --php=8.4 && cd workflow-demo composer config extra.symfony.allow-contrib true bin/console importmap:require d3 composer config minimum-stability beta bin/console make:controller d3 -i symfony server:start -d symfony open:local --path=/d3 ../survos/bin/lb.sh workflow-helper # composer req survos/state-bundle bin/console make:controller d3 -i cat > templates/d3 .html.twig <<END {% extends 'base.html.twig' %} {% block body %} workflow here. {% endblock %} END symfony server:start -d symfony open:local --path=/d3
Notes
Since the workflow may use a message bus, a reminder on how to configure that with the Symfony CLI: https://symfony.com/doc/current/setup/symfony_server.html#symfony-server_configuring-workers
https://github.com/survos/SurvosWorkflowHelperBundle/network/dependents https://github.com/codereviewvideos/symfony-workflow-example
survos/state-bundle 适用场景与选型建议
survos/state-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.35k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「workflow」 「symfony-ux」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 survos/state-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 survos/state-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 survos/state-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Workflow for NetCommons Plugin
Sylius eCommerce administration panel component.
The bundle for easy using json-rpc api on your project
Doctrine Block management made easy
Workflow logger
Symfony bundle for the Rekapager library
统计信息
- 总下载量: 1.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 5
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-05