Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Workflow][Twig] add workflow_has_state function #20781

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
wants to merge 1 commit into from

Conversation

Padam87
Copy link
Contributor

@Padam87 Padam87 commented Dec 6, 2016

Q A
Branch? 3.2
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Use case: I needed to create a wizard like visualization for the status, I think this is common enough to be useful for others too.

* @param array $places
* @param Transition[] $transitions
*/
protected function recursiveBacktraceTransitions(&$finished, $places, $transitions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be protected

@stof
Copy link
Member

stof commented Dec 6, 2016

this is clearly lacking tests.

@Padam87
Copy link
Contributor Author

Padam87 commented Dec 6, 2016

I know, I will add tests if you think it is okay to have this in the codebase :)

@Padam87 Padam87 force-pushed the workflow-twig branch 2 times, most recently from de5afa0 to f85b378 Compare December 6, 2016 16:04
@Padam87
Copy link
Contributor Author

Padam87 commented Dec 6, 2016

Are the tests failing because of a fluke ? :S

$workflow = $this->workflowRegistry->get($object, $name);
$marking = $workflow->getMarking($object);

return array_key_exists($state, $marking->getPlaces());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return $marking->has($state);

return array_key_exists($state, $marking->getPlaces());
}

public function isComplete($object, $state, $name = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot determine if a state is completed just because of the active states, this function tracks back and checks.

Use case: http://bootsnipp.com/snippets/featured/form-process-steps

@nicolas-grekas nicolas-grekas modified the milestone: 3.x Dec 7, 2016
@lyrixx
Copy link
Member

lyrixx commented Dec 13, 2016

Hello @Padam87

I'm totally ok for the new twig method workflow_state_active, but please called it workflow_has_place.

I really tried to understand what is the workflow_state_complete, but I failed to. It looks like you think that when you fire the transition from A to B, the marking contains A and B. But actually, the marking contains only B. If I'm wrong, could you give me an example (not the previous screenshot, it did not helped me).

Finally, As I'm ok for the first one, you could split this PR into 2 PR to get it merged faster.

@Padam87
Copy link
Contributor Author

Padam87 commented Dec 13, 2016

I think I will remove workflow_state_complete, because of the difficulties about its usage.

My goal was this: Let's say we have 4 places, A -> B ->C -> D, and we are currently in C.
If we want to render a process steps element in html, we would need to know if A and B are complete (to add an active css class to the element for example). Since the marking store only contains C, it is necessary to track back and get all the places that lead to C. This would of course only work for linear workflows...

@stof
Copy link
Member

stof commented Dec 13, 2016

This looks like very specific to your use case. And the name is not even good, as what you are actually doing is trying to identify the previous states.
So I'm against adding this in core (especially given that it works only for some workflows). Add this in your own project if you need it.

@lyrixx
Copy link
Member

lyrixx commented Dec 19, 2016

Hello @Padam87

Could you take time to finish this one? If you can't and if you agree, I can push directly into your fork to finish it. Just tell me ;)

@lyrixx
Copy link
Member

lyrixx commented Dec 19, 2016

Oups. Sorry. I did not see you updated the PR.

@lyrixx lyrixx changed the title [Workflow][Twig] Active and complete state checks [Workflow][Twig] add workflow_has_state function Dec 19, 2016
Copy link
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

(failures are not related)

@lyrixx
Copy link
Member

lyrixx commented Dec 19, 2016

I have one question: Should it be a test ? ie something like:

{% if (subject is workflow_place 'place name') %}
    yes
{% endif %}

@stof
Copy link
Member

stof commented Dec 19, 2016

yes it should be a test, except that the syntax will be this one:

{% if subject is workflow_place('place name') %}
    yes
{% endif %}

@stof
Copy link
Member

stof commented Dec 19, 2016

though I'm not sure about the naming here. The subject is not the place itself.

@lyrixx
Copy link
Member

lyrixx commented Dec 19, 2016

oups, I forgot the has =>

{% if subject is workflow_has_place('place name') %}
    yes
{% endif %}

@stof
Copy link
Member

stof commented Dec 19, 2016

@lyrixx when writing the name for a test, you should avoid using a verb in it, as is ..._has_.. is quite weird.
what about subject is in_workflow_place('place name') ?

@lyrixx
Copy link
Member

lyrixx commented Dec 19, 2016

@stof: I agree with you it's weird. but in_workflow_place does not follow the same pattern of others workflow functions. Other functions start with workflow. What about workflow_in_place

@stof
Copy link
Member

stof commented Dec 19, 2016

@lyrixx the issue is that reading the source code is confusing then, as the workflow is not in the place; the subject is.
The main requirement for the Twig function/filter is to avoid name clashes with other extensions, which is why I also kept workflow in the name. And while I agree that keeping the differentiator at the beginning is more likely to avoid clashes (if everyone does it, a clash can only happen when reusing the same prefix), it looks really weird here. And I doubt in_ will be used as an extension prefix for something talking about workflow place again internally.

@lyrixx
Copy link
Member

lyrixx commented Dec 19, 2016

Fair enough. Actually, I have not strong preference here. So I will be ok with almost anything ;)

@javiereguiluz
Copy link
Member

Trying to find a good name for this is very challenging. Would it make any sense to call it workflow_has_marked() or workflow_is_marked() ?

{% if workflow_has_marked(subject, 'place name') %}
    ...
{% endif %}

{% if workflow_is_marked(subject, 'place name') %}
    ...
{% endif %}

@xabbuh
Copy link
Member

xabbuh commented Dec 22, 2016

@javiereguiluz Your suggestions look weird when being used as tests as there will be two is then. So far, in_workflow_place sounds like the best suggestion to me.

@javiereguiluz
Copy link
Member

@xabbuh shouldn't be workflow_in_place() ? In any case, why do you want to use it as a test. Can't you make the function accept two arguments?

{# weird #}
{% if subject is workflow_in_place('aaa') %}

{# ok? #}
{% if workflow_in_place(subject, 'aaa') %}

@xabbuh
Copy link
Member

xabbuh commented Dec 22, 2016

Why do you want to use a function for this? What you are actually doing here is testing for a condition.

@javiereguiluz
Copy link
Member

To be honest, I'm not sure. I just consider better to use a function because it makes Twig code easier to understand. Besides, all the current functions are called workflow_* so it's mandatory to keep using that to make things easier to learn.

@fabpot
Copy link
Member

fabpot commented Dec 22, 2016 via email

@lyrixx
Copy link
Member

lyrixx commented Dec 26, 2016

👍

@@ -45,6 +46,14 @@ public function getEnabledTransitions($object, $name = null)
return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object);
}

public function hasPlace($object, $state, $name = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, it's weird to call the variable state when the twig function, php method call it place.
Could you update it please?

$workflow = $this->workflowRegistry->get($object, $name);
$marking = $workflow->getMarking($object);

return $marking->has($state);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All others implementations fit on one line.

return $this->workflowRegistry->get($object, $name)->getMarking()->has($state);

{
$subject = new \stdClass();

$marking = new Marking(array('ordered' => true, 'waiting_for_payment' => true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should be 1 and not true


$marking = new Marking(array('ordered' => true, 'waiting_for_payment' => true));

$workflow = $this->getMock(Workflow::class, array(), array(), '', false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a previous PR, fab change all occurrences of getMock to use getMockBuiler...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I would use concrete implementation instead of mock.
I will be simpler.

@lyrixx
Copy link
Member

lyrixx commented Jan 12, 2017

Closing in favor of #20781

@lyrixx lyrixx closed this Jan 12, 2017
fabpot added a commit that referenced this pull request Jan 12, 2017
…unction (Padam87, lyrixx)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[TwigBridge][Worklow] Added a new workflow_has_place function

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20781
| License       | MIT
| Doc PR        |

---

This PR follow up #20781

I also increased the Coverage of the WorkflowExtension and finally I added an extra commit that is not related to clean-up 2 unused properties

Commits
-------

108c89d [TwigBridge] Removed unused class property
77f820e [TwigBridge][Workflow] Added more tests on WorkflowExtension
efe500d [TwigBridge][Workflow] Fixed code and tests
4d0cc68 [TwigBridge][Workflow] Added workflow_has_place twig function
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants