-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle][Workflow] Attach the workflow's configuration to the workflow
tag
#51227
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
Conversation
That's a bit strange to me, I'm not sure we ever did that. Usually tag attributes are used in a compiler pass. Shouldn't we do the same here? What's the more extended use case for this? |
It looks like you missed the PR description :D The example show a usage of tag attribute in a compiler pass And the use case is described in the PR description too. But to go a bit further class Kernel extends BaseKernel implements CompilerPassInterface
{
use MicroKernelTrait;
public function process(ContainerBuilder $container)
{
$classesToWorkflow = [];
foreach ($container->findTaggedServiceIds('workflow.workflow') as $id => $attributes) {
$config = $container->getDefinition($id)->getTag('workflow')[0];
foreach ($config['supports'] ?? [] as $class) {
$classesToWorkflow[$class] = new Reference($id);
}
}
$container->register(MyRegistry::class, MyRegistry::class)
->setArguments([$classesToWorkflow])
;
}
}
|
Doesn't your registry duplicate the code we have in the |
I have a big issue with the registry: People use it to get one workflow. In the documentation we used to tell to do : $workflowRegistry->get($subject)->apply($subject, 'foobar'); And this is wrong: people must inject the right Workflow directly (law of demeter, performance, etc) That's why I decided to make it internal initially. The documentation has been updated since (I just checked, and there is still one occurrence, cc @alexandre-daubois) But there is still one issue. If you have generic controller / service where you cannot know in advance which workflow to use, you have to use a ServiceLocator. There are all tagged, by name, so it should be easy... BUT, sometime, (cf this discussion) the name is not enough. With this PR, people will have full power to build their own registry. It'll be faster, cleaner, and build exactly for each use case. Even with the current Registry and its |
…urrences (alexandre-daubois) This PR was merged into the 5.4 branch. Discussion ---------- [Workflow] Remove registry workflow retrieval occurrences Follow-up of symfony/symfony#51227 (comment), `@lyrixx` proposed to remove "registry retrieval" occurrences in the documentation. Actually, the `Alternatively, use the registry...` part isn't present already in the 6.3 documentation. Commits ------- aea773c [Workflow] Remove registry workflow retrieval occurrences
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, thanks for the explanations. I guess I would prefer wiring the registry (ours) in a compiler pass instead of doing it in FWB, but that can be done separately.
Mouais, I'm not convinced. |
I disagree. Registry needs instantiated workflow to work. It means no lazy loading. Then you can't access That's two pain points that show the current way to work doesn't work well. |
4788d19
to
75c8463
Compare
Rebased, and conflict fixed |
ee79817
to
2527507
Compare
Hello, This PR is ready and I have rebased it. @fabpot I understand your concern. But I have a real use case where I need to fetch a workflow dynamically based on some entity properties. We'll have a LOT of workflows - basically one by client (B to B to C), and every Finally, the diff is very minimal, and does not really impact the framework. Everything is thrown after the compilation. I don't really how it could hurt 🤓 |
The issue is that if we start exposing this config on the attribute with the intent of allowing projects to read it, we must provide BC on the structure of this config (with no way at all to ever deprecate anything in it as it is not accessed through a dedicated API in which we can trigger deprecations) |
Hmm, Got it 👍🏼 We could document that the shape is not BC? |
@lyrixx but then, how would you use it ? |
Basically with something like this: # config/packages/workflow.yaml
framework:
workflows:
my_workflow_name:
metadata:
features: [repair, wash, dry] // src/Kernel.php
class Kernel extends BaseKernel implements CompilerPassInterface
{
use MicroKernelTrait;
public function process(ContainerBuilder $container)
{
$configurations = [];
foreach ($container->findTaggedServiceIds('workflow') as $id => $config) {
$configurations[$id] = $config[0]['config'];
}
$container
->getDefinition(Selector::class)
->setArgument('$configurations', $configurations)
;
}
} <?php
// src/Workflow/Selector.php
namespace App\Workflow;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
class Selector
{
public function __construct(
#[TaggedLocator('workflow')]
private readonly ContainerInterface $container,
private readonly array $configurations,
) {
}
public function getWorkflow(/** Article $article */)
{
foreach ($this->configurations as $id => $config) {
// $features = $article->getWorkflowFeatures();
$featuresRequired = ['repair', 'wash', 'dry'];
sort($featuresRequired);
$featuresAvailable = $config['metadata']['features'] ?? [];
sort($featuresAvailable);
if ($featuresRequired === $featuresAvailable) {
return $this->container->get($id);
}
}
throw new \RuntimeException('No workflow found');
}
} and finally public function index(Selector $selector, Article $article): Response
{
$w = $selector->getWorkflow($article);
dd($w); |
682b35f
to
91c1f10
Compare
Would you accept this PR if I inject in the tag only the workflow metadata? It'll be enough. |
cc @fabpot |
91c1f10
to
2fd3770
Compare
PR rebased, and updated |
2fd3770
to
8df4992
Compare
8df4992
to
887a3c0
Compare
Since the registry is deprecated, users have to build their own registry when a service locator is not enough.
However, some information can be missing, like the
supports
configuration option.In this PR, I add the whole configuration to the tag, so everyone can build exactly what they need.
The config is added only to the
workflow
tag, not sub tags. To get it: