Skip to content

[Workflow] Mark registry as internal and deprecate the service #46000

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

Merged
merged 1 commit into from
Aug 12, 2022

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Apr 11, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? yes
Tickets Fix #45315
License MIT
Doc PR

The Registry was added to be able to get a workflow easily from twig templates
without having to specify the workflow name.

Unfortunately, many people thought this was the only way to get a workflow and
we even add some flexibility to retrieve the workflow from the registry.

I think it's a bad idea to inject the registry, it has the same default as
injecting the Container: It's a black box, does not respect SOLID principle and
the demeter law, and make testing harder.

So I decided to mark the registry as internal. Instead people have to inject the
"right" workflow where they need it.

It can be legit to need all workflows, for documentation or for testing.
So, all workflow services are tagged and can be injected with the
following YAML syntax:

!tagged_locator  { tag: workflow, index_by: name }

or PHP syntax:

tagged_locator('workflow', 'name')

Also, two others tags exists for each workflow types

  • workflow.workflow
  • workflow.state_machine

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Makes sense to me.
we should also deprecate the corresponding autowiring alias:
src/Symfony/Bundle/FrameworkBundle/Resources/config/workflow.php: ->alias(Registry::class, 'workflow.registry')

@chalasr chalasr modified the milestones: 6.1, 6.2 Apr 18, 2022
@fabpot
Copy link
Member

fabpot commented Jul 20, 2022

@lyrixx Do you have time to finish this one?

@lyrixx
Copy link
Member Author

lyrixx commented Jul 20, 2022

Yes, I'll do that ASAP, But I'm on holidays for 2 weeks. I'll do that after!

@lyrixx lyrixx requested a review from yceruto as a code owner August 12, 2022 10:18
@lyrixx lyrixx changed the title [Workflow] Mark Registry as internal and add a ServiceLocator [Workflow] Mark registry as internal and deprecate the service Aug 12, 2022
@lyrixx
Copy link
Member Author

lyrixx commented Aug 12, 2022

Hello,

I pushed a new version without the service locator. I also updated the PR description with the new usage

Instead, all workflow services are tagged and can be injected with the
following YAML syntax:

```yaml
!tagged_locator  { tag: workflow, index_by: name }
```

or PHP syntax:

```php
tagged_locator('workflow', 'name')
```

Also, two others tags exists for each workflow types
* `workflow.workflow`
* `workflow.state_machine`
@fabpot
Copy link
Member

fabpot commented Aug 12, 2022

Thank you @lyrixx.

@fabpot fabpot merged commit 15ee67e into symfony:6.2 Aug 12, 2022
@lyrixx lyrixx deleted the w-registry branch August 12, 2022 12:03
fabpot added a commit that referenced this pull request Aug 19, 2022
This PR was merged into the 6.2 branch.

Discussion
----------

[FrameworkBundle] clean up unused variables

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

spotted while reviewing #46000

Commits
-------

ccfb1e5 clean up unused variables
@fabpot fabpot mentioned this pull request Oct 24, 2022
@tacman
Copy link
Contributor

tacman commented Oct 25, 2022

I'm in the category of programmers that injected the workflow registry in order to get a list of workflows in a helper bundle for workflow.

// now deprecated, following https://symfony.com/doc/current/workflow.html#accessing-the-workflow-in-a-class

class WorkflowHelperService
{
    public function __construct(
        private Registry $workflowRegistry,

// in loadExtension of the bundle:
        $builder->autowire( WorkflowHelperService::class)
            ->setArgument('$workflowRegistry', new Reference('workflow.registry'));
       
      
// better way
class WorkflowHelperService
{
    public function __construct(
        private ServiceLocator $locator,

// loadExtension()

        $builder->autowire( WorkflowHelperService::class)
            ->setArgument('$locator', tagged_locator(tag: 'workflow', indexAttribute: 'name' ))
            ->setAutoconfigured(true)
        ;

Using this, I'm not getting any services in the locator, $this->locator->count() is zero.

I imagine I'm not using tagger_locator correctly, or I'm doing this in the wrong spot (I'm doing this in loadExtension of my bundle, perhaps it needs to be done in the compiler pass? If so, could you point me in the right direction on how to do that, I still find the bundle interactions a bit intimidating.

@tacman
Copy link
Contributor

tacman commented Oct 25, 2022

Also, is this the right spot for that question? The issue itself is closed, should I open a new one?

@stof
Copy link
Member

stof commented Oct 25, 2022

You should open a new discussion

lyrixx added a commit to lyrixx/symfony-docs that referenced this pull request Feb 7, 2023
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Feb 21, 2023
This PR was merged into the 6.2 branch.

Discussion
----------

[Workflow] Do not talk about registry

This is not recommended since symfony/symfony#46000

Commits
-------

e169f78 [Workflow] Do not talk about registry
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.

[Workflow] Allow fetching workflow (definition) by name from registry
8 participants