From a99fd2b893325f81c74d815b6075d7ffbbcc2669 Mon Sep 17 00:00:00 2001 From: Carlos Pereira De Amorim Date: Fri, 28 Aug 2020 23:28:38 +0200 Subject: [PATCH 1/4] added the usage of workflow injection --- workflow.rst | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/workflow.rst b/workflow.rst index 21ae4192d58..a77b4e9329b 100644 --- a/workflow.rst +++ b/workflow.rst @@ -265,6 +265,39 @@ registry in the constructor:: } } +Or use the typed + named parameter injection:: + + use App\Entity\BlogPost; + use Symfony\Component\Workflow\WorkflowInterface; + + class MyClass + { + private $blogPostWorkflow; + + public function __construct(WorkflowInterface $blogPostWorkflow) + { + $this->blogPostWorkflow = $blogPostWorkflow; + } + + public function toReview(BlogPost $post) + { + // Update the currentState on the post + try { + $this->blogPostWorkflow->apply($post, 'to_review'); + } catch (LogicException $exception) { + // ... + } + // ... + } + } + +.. tip:: + + You can find the list of available services with the following command:: + + php bin/console debug:autowiring workflow + + Using Events ------------ @@ -665,7 +698,7 @@ of domain logic in your templates: ``workflow_has_marked_place()`` Returns ``true`` if the marking of the given object has the given state. - + ``workflow_transition_blockers()`` Returns :class:`Symfony\\Component\\Workflow\\TransitionBlockerList` for the given transition. @@ -700,7 +733,7 @@ The following example shows these functions in action: {% if 'reviewed' in workflow_marked_places(post) %} Reviewed {% endif %} - + {# Loop through the transition blockers #} {% for blocker in workflow_transition_blockers(post, 'publish') %} {{ blocker.message }} @@ -869,7 +902,7 @@ In a :ref:`flash message ` in your controller:: // $transition = ...; (an instance of Transition) - // $workflow is a Workflow instance retrieved from the Registry (see above) + // $workflow is a Workflow instance retrieved from the Registry or injected directly (see above) $title = $workflow->getMetadataStore()->getMetadata('title', $transition); $this->addFlash('info', "You have successfully applied the transition with title: '$title'"); From f9afb77432db4c2dae3e1fc174961634f29b6c56 Mon Sep 17 00:00:00 2001 From: Carlos Pereira De Amorim Date: Fri, 28 Aug 2020 23:32:05 +0200 Subject: [PATCH 2/4] added the usage of workflow injection --- workflow.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/workflow.rst b/workflow.rst index a77b4e9329b..4241423afe8 100644 --- a/workflow.rst +++ b/workflow.rst @@ -293,10 +293,7 @@ Or use the typed + named parameter injection:: .. tip:: - You can find the list of available services with the following command:: - - php bin/console debug:autowiring workflow - + You can find the list of available services with the following command ``php bin/console debug:autowiring workflow`` Using Events ------------ From 9e10527c89ce86ce48e2f9eb04783de0e65709fb Mon Sep 17 00:00:00 2001 From: Carlos Pereira De Amorim Date: Tue, 1 Sep 2020 14:33:45 +0200 Subject: [PATCH 3/4] added the usage of workflow injection --- workflow.rst | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/workflow.rst b/workflow.rst index 4241423afe8..3875892506e 100644 --- a/workflow.rst +++ b/workflow.rst @@ -858,25 +858,23 @@ Then you can access this metadata in your controller as follows:: // src/App/Controller/BlogPostController.php use App\Entity\BlogPost; - use Symfony\Component\Workflow\Registry; + use Symfony\Component\Workflow\WorkflowInterface; // ... - public function myAction(Registry $registry, BlogPost $post) + public function myAction(WorkflowInterface $blogPostWorkflow, BlogPost $post) { - $workflow = $registry->get($post); - - $title = $workflow + $title = $blogPostWorkflow ->getMetadataStore() ->getWorkflowMetadata()['title'] ?? 'Default title' ; - $maxNumOfWords = $workflow + $maxNumOfWords = $blogPostWorkflow ->getMetadataStore() ->getPlaceMetadata('draft')['max_num_of_words'] ?? 500 ; - $aTransition = $workflow->getDefinition()->getTransitions()[0]; - $priority = $workflow + $aTransition = $blogPostWorkflow->getDefinition()->getTransitions()[0]; + $priority = $blogPostWorkflow ->getMetadataStore() ->getTransitionMetadata($aTransition)['priority'] ?? 0 ; From 9d5a9187ea553201a9f27deb6756f50146da18a8 Mon Sep 17 00:00:00 2001 From: Carlos Pereira De Amorim Date: Tue, 1 Sep 2020 14:37:45 +0200 Subject: [PATCH 4/4] added the usage of workflow injection --- workflow.rst | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/workflow.rst b/workflow.rst index 3875892506e..f914e327276 100644 --- a/workflow.rst +++ b/workflow.rst @@ -236,28 +236,25 @@ what actions are allowed on a blog post:: Accessing the Workflow in a Class --------------------------------- -To access workflow inside a class, use dependency injection and inject the -registry in the constructor:: +To access workflow inside a class, use dependency injection and inject the typed + named parameter injection in the constructor:: use App\Entity\BlogPost; - use Symfony\Component\Workflow\Registry; + use Symfony\Component\Workflow\WorkflowInterface; class MyClass { - private $workflowRegistry; + private $blogPublishingWorkflow; - public function __construct(Registry $workflowRegistry) + public function __construct(WorkflowInterface $blogPublishingWorkflow) { - $this->workflowRegistry = $workflowRegistry; + $this->blogPublishingWorkflow = $blogPublishingWorkflow; } public function toReview(BlogPost $post) { - $workflow = $this->workflowRegistry->get($post); - // Update the currentState on the post try { - $workflow->apply($post, 'to_review'); + $this->blogPublishingWorkflow->apply($post, 'to_review'); } catch (LogicException $exception) { // ... } @@ -265,25 +262,28 @@ registry in the constructor:: } } -Or use the typed + named parameter injection:: + +Or use the registry:: use App\Entity\BlogPost; - use Symfony\Component\Workflow\WorkflowInterface; + use Symfony\Component\Workflow\Registry; class MyClass { - private $blogPostWorkflow; + private $workflowRegistry; - public function __construct(WorkflowInterface $blogPostWorkflow) + public function __construct(Registry $workflowRegistry) { - $this->blogPostWorkflow = $blogPostWorkflow; + $this->workflowRegistry = $workflowRegistry; } public function toReview(BlogPost $post) { + $blogPublishingWorkflow = $this->workflowRegistry->get($post); + // Update the currentState on the post try { - $this->blogPostWorkflow->apply($post, 'to_review'); + $blogPublishingWorkflow->apply($post, 'to_review'); } catch (LogicException $exception) { // ... } @@ -293,7 +293,7 @@ Or use the typed + named parameter injection:: .. tip:: - You can find the list of available services with the following command ``php bin/console debug:autowiring workflow`` + You can find the list of available services with the following command ``php bin/console debug:autowiring workflow`` Using Events ------------ @@ -861,20 +861,20 @@ Then you can access this metadata in your controller as follows:: use Symfony\Component\Workflow\WorkflowInterface; // ... - public function myAction(WorkflowInterface $blogPostWorkflow, BlogPost $post) + public function myAction(WorkflowInterface $blogPublishingWorkflow, BlogPost $post) { - $title = $blogPostWorkflow + $title = $blogPublishingWorkflow ->getMetadataStore() ->getWorkflowMetadata()['title'] ?? 'Default title' ; - $maxNumOfWords = $blogPostWorkflow + $maxNumOfWords = $blogPublishingWorkflow ->getMetadataStore() ->getPlaceMetadata('draft')['max_num_of_words'] ?? 500 ; - $aTransition = $blogPostWorkflow->getDefinition()->getTransitions()[0]; - $priority = $blogPostWorkflow + $aTransition = $blogPublishingWorkflow->getDefinition()->getTransitions()[0]; + $priority = $blogPublishingWorkflow ->getMetadataStore() ->getTransitionMetadata($aTransition)['priority'] ?? 0 ;