Symfony workflow::can 通过工作流守卫事件监听器

Posted

技术标签:

【中文标题】Symfony workflow::can 通过工作流守卫事件监听器【英文标题】:Symfony workflow::can passes through workflow guard event listener 【发布时间】:2017-05-18 22:16:37 【问题描述】:

如果你们中的很多人已经测试了symfony workflow 组件,我现在不知道,但我希望你们中的一些人已经:)

所以,我在 两个对象 上使用这个组件,我希望 第一个对象 更新 第二个 strong> 取决于它所应用的transition

为此,我在我的第一个对象上使用workflow guard listener,并尝试在我的第二个对象上执行workflow::apply(使用第二个工作流程...)。

问题是,当我创建workflow::can 时,事件已调度,它会尝试应用我的第二个对象 ...这很不正常,因为我只是问我是否可以应用一些过渡,而要求实际应用它在我的第一个对象上。

配置

framework:
    workflows:
        request_for_operation:
            type: 'state_machine'
            marking_store:
                type: 'single_state'
                arguments:
                    - 'status'
            supports:
                - AppBundle\Entity\RequestForOperation
            places:
                - draft
                - pending_for_management
                - in_progress
                - finished
                - canceled
            transitions:
                request_for_operations:
                    from: draft
                    to:   pending_for_management
                start_rfop_management:
                    from: pending_for_management
                    to:   in_progress
                close:
                    from: in_progress
                    to:   finished
                cancel:
                    from: [pending_for_management, in_progress]
                    to:   canceled
        operation:
            type: 'state_machine'
            marking_store:
                type: 'single_state'
                arguments:
                    - 'status'
            supports:
                - AppBundle\Entity\Operation
            places:
                - draft
                - pending_for_management
                - in_progress
                - finished
                - canceled
            transitions:
                validate_operation:
                    from: draft
                    to:   pending_for_management
                start_tracking:
                    from: pending_for_management
                    to:   in_progress
                close:
                    from: in_progress
                    to:   finished
                cancel:
                    from: [pending_for_management, in_progress]
                    to:   canceled

订阅者

class RequestForOperationListener implements EventSubscriberInterface

    public function __construct(
        OperationManager $operationManager,
        UserNotifier $userNotifier
    ) 
        $this->operationManager = $operationManager;
        $this->userNotifier = $userNotifier;
    

    public static function getSubscribedEvents()
    
        return [
            'workflow.request_for_operation.guard.request_for_operations' => ['onRequestForOperations'],
            'workflow.request_for_operation.guard.start_rfop_management' => ['onStartRfopManagement'],
            'workflow.request_for_operation.guard.close' => ['onClose'],
            'workflow.request_for_operation.guard.cancel' => ['onCancel'],
        ];
    

    public function onRequestForOperations(GuardEvent $event)
    
        /** @var RequestForOperation $rfop */
        $rfop = $event->getSubject();

        //get all the operations linked to the rfop
        $operations = $rfop->getOperations();
        foreach ($operations as $operation) 
            //set the status of the operation to 'pending_for_management'
            $this->operationManager->applyTransition($operation, 'validate_operation');
            //set the status of the sub-operations to 'pending_for_management'
            foreach ($operation->getChildren() as $subOperation) 
                $this->operationManager->applyTransition($subOperation, 'validate_operation');
            

            //get the users (i.e: managers) linked to the operation and notify them (by mail or whatever)
            $this->notifyAssignedUsers($operation->getUsers(), $operation);
        
    

【问题讨论】:

我花时间仔细研究了 Workflow 类,我注意到调度的事件比文档中提到的要多得多...... 我们有:离开、过渡、进入、宣布(和守卫) 保护事件在工作流::can 函数上调度,其他事件似乎在工作流::apply 上调用(所以也许我必须使用另一个事件来完成我想做的事情)。 你不应该在警卫中应用某些东西。守卫只是为了防止使用基于某些断言的转换 你的问题到底是什么?您收到错误消息吗? 【参考方案1】:

我想我遇到了和你一样的问题,我已经记录了 Symfony 项目的一个错误:https://github.com/symfony/symfony/issues/33105

【讨论】:

以上是关于Symfony workflow::can 通过工作流守卫事件监听器的主要内容,如果未能解决你的问题,请参考以下文章

通过 Symfony/Console 创建不同类型的类

Symfony 3 通过手机阵列登录

通过单元测试访问 Symfony 2 容器?

Symfony2 自定义身份验证:用户登录但未通过身份验证

通过 Symfony2 配置 DBAL 以设置字符集

通过验证更改用户密码( symfony2 )