Skip to content

Conversation

javiereguiluz
Copy link
Member

Fixes #20876.

This makes the same changes we did in #21382 when explaining the #[Target] attribute and then it addresses the issue explained in #20876.

@javiereguiluz javiereguiluz added this to the 6.4 milestone Sep 26, 2025
@javiereguiluz javiereguiluz self-assigned this Sep 26, 2025
@javiereguiluz javiereguiluz removed their assignment Sep 30, 2025
@javiereguiluz
Copy link
Member Author

Pinging @lyrixx in case he has some time to review this change. Thanks!

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.

Thanks for taking care of this PR.

Workflows can also be injected thanks to their name and the
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\Target`
attribute::
**(2) Use the ``#[Target]`` attribute**
Copy link
Member

Choose a reason for hiding this comment

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

IMHO the target is much much better. What about putting this one before the "Use a specific argument name" ?

workflow.rst Outdated
class MyClass
{
public function __construct(
#[AutowireLocator('blog_publishing', 'user_registration')]
Copy link
Member

@lyrixx lyrixx Oct 1, 2025

Choose a reason for hiding this comment

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

🤔 This does not work, does't it?

According to the PHPDoc :

1. A tag name or an explicit list of service ids
2. The name of the attribute that defines the key referencing each service in the locator

You can use the full service name: <workflow_type>.<worflow_name>. Example state_machine.blog_publishing (Note: I did not check if it's a state machine or a workflow, in your example)

But this this is kinda useless IMHO.


The whole point it to inject them all, and get lazy loading:

    public function __construct(
        #[AutowireLocator('workflow')] // <-- this is not the workflow name, but the tag name
        private ServiceLocator $workflows,
    ) {
    }

    #[Route(path: '/foobar')]
    public function foobar()
    {
        dd($this->workflows->get('workflow.article')); // <-- this is the service ID. See above
    }

But, IMHO, this is a bit boring to prefix by workflow. or state_machine.

That's why I prefer:

    public function __construct(
        //                      This is the tag name
        //                     /          This is index attribute, see https://github.com/symfony/symfony/blob/74fc896a8e13930a27037f7c97c76ca353ae1a51/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L1201-L1202
        //                   /           /
        #[AutowireLocator('workflow', 'name')]
        private ServiceLocator $workflows,
    ) {
    }

    #[Route(path: '/foobar')]
    public function foobar()
    {
        dd($this->workflows->get('article')); // Now this is the workflow name (without any prefix)
        dd($this->workflows->get('task')); // Same same
    }

And, if you wan to get only workflow, or only state machines:

public function __construct(
        #[AutowireLocator('workflow.workflow', 'name')]
        private ServiceLocator $workflows,
        #[AutowireLocator('workflow.state_machine', 'name')]
        private ServiceLocator $stateMachines,
    ) {
    }

    #[Route(path: '/foobar')]
    public function foobar()
    {
        dd($this->workflows, $this->stateMachines);
    }
image

All my examples work, and have been tested with https://github.com/lyrixx/SFLive-Paris2016-Workflow ; I also tested all feature, and they are available in 6.4 too

EDITS

  1. I'm not sure it actually works. I'm investigating !
  2. it do works on 6.4, but not on 7.4. So there is a bug somewhere. Will fix it!
  3. okay, no bugs. I need ALL the app to be in 7.4 (even the DIC to get symfony/symfony@8b79ff5)

@javiereguiluz
Copy link
Member Author

Grég, thanks for such a fantastic review 🙇

I made all the changes and fixes that you pointed out. Thanks!

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.

Nice 👍🏼

I still prefer the "target" option over the "specific argument name", but it's good to me

@javiereguiluz
Copy link
Member Author

Yes, #[Target] is better, but I preferred to keep consistency with the other examples of the docs.

Let's do something. Let's change this in 8.0 branch after the Symfony 8.0 release and let's show only the #[Target] config while adding the other way in a small note for each example.

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.

👍🏼

Thanks for taking care of this PR!

@javiereguiluz javiereguiluz merged commit 897c87a into symfony:6.4 Oct 2, 2025
3 checks passed
@javiereguiluz javiereguiluz deleted the fix_20876 branch October 2, 2025 15:49
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.

3 participants