Skip to content

[DIC] Decorator + TaggedLocator does not play well #53174

Closed
@jmuxfeldt

Description

@jmuxfeldt

Symfony version(s) affected

6.4.x

Description

according to the docs, you can use a serviceLocator to load a workflow using a taggedlocater.
#[TaggedLocator('workflow')], e.g.
The WorkflowDebugPass.php introduced in 6.4 however prefixes all the workflow keys with 'debug.' when kernel->isDebug() and this makes it unwieldy to get a workflow by key.

How to reproduce

The following class allows me to load a workflow dynamically, without specifically injecting a certain workflow. It avoids injecting the registry, as recommended in the Symfony docs. This still works in 6.3, but no longer in 6.4.

#WorkflowLoader.php

WARNING: this no longer works as of Symfony 6.4

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\Workflow\WorkflowInterface;
use RuntimeException;

class WorkflowLoader
{
    public function __construct(
        #[TaggedLocator('workflow')] private readonly ContainerInterface $serviceLocator
    )
    {
    }

    public function getWorkflow($workflowName): WorkflowInterface
    {
        try {
            if (!$workflow = $this->serviceLocator->get('state_machine.' . $workflowName)) {
                $workflow = $this->serviceLocator->get('workflow.' . $workflowName);
            }
        } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
            throw new RuntimeException("Process Workflow $workflowName could not be loaded. \n" . $e->getMessage());
        }
        return $workflow;
    }
}

Possible Solution

As a workaround, I can test for the environment in my class, but I think the WorkflowDebugPass should not be called automatically, but only by specific configuration. It is also should be documented.

In general, It would be great to be able to lazyload Workflows dynamically. If you don't know which workflow you need in advance, then a service can't know which one to inject.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions