Skip to content

Commit 773871b

Browse files
committed
feature #22234 [DI] Introducing autoconfigure: automatic _instanceof configuration (weaverryan)
This PR was squashed before being merged into the 3.3-dev branch (closes #22234). Discussion ---------- [DI] Introducing autoconfigure: automatic _instanceof configuration | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes (mostly, a continuation of a new feature) | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#7538 This is a proposal to allow the user to opt into some automatic `_instanceof` config. Suppose I want to auto-tag all of my voters and event subscribers ```yml # current services: _defaults: autowire: true _instanceof: Symfony\Component\Security\Core\Authorization\Voter\VoterInterface: tags: [security.voter] Symfony\Component\EventDispatcher\EventSubscriberInterface: tags: [kernel.event_subscriber] # services using the above tags AppBundle\Security\PostVoter: ~ AppBundle\EventListener\CheckRequirementsSubscriber: ~ ``` If I'm registering a service with a class that implements `VoterInterface`, when would I ever *not* want that to be tagged with `security.voter`? Here's the proposed code: ```yml # proposed services: _defaults: autowire: true autoconfigure: true # services using the auto_configure_instanceof functionality AppBundle\Security\PostVoter: ~ AppBundle\EventListener\CheckRequirementsSubscriber: ~ ``` The user must opt into this and it only applies locally to this configuration file. It works because each enabled bundle would have the opportunity to add one or more "automatic instanceof" definitions - e.g. SecurityBundle would add the `security.voter` instanceof config, FrameworkBundle would add the `kernel.event_subscriber` instanceof config, etc. For another example, you can check out the proposed changes to `symfony-demo` - symfony/demo#483 - the `_instanceof` section is pretty heavy: https://github.com/hhamon/symfony-demo/blob/81694ac21e63bebe7ecfa22fa689766f2f38ccd5/app/config/services.yml#L20 Thanks! Commits ------- 18627bf9f6 [DI] Introducing autoconfigure: automatic _instanceof configuration
2 parents f1237ea + 8181887 commit 773871b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

DependencyInjection/SecurityExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
2020
use Symfony\Component\DependencyInjection\ChildDefinition;
2121
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
22-
use Symfony\Component\DependencyInjection\Definition;
2322
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2423
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2524
use Symfony\Component\DependencyInjection\ContainerBuilder;
2625
use Symfony\Component\DependencyInjection\Reference;
2726
use Symfony\Component\Config\FileLocator;
2827
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
28+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
2929

3030
/**
3131
* SecurityExtension.
@@ -110,6 +110,9 @@ public function load(array $configs, ContainerBuilder $container)
110110
$this->aclLoad($config['acl'], $container);
111111
}
112112

113+
$container->registerForAutoconfiguration(VoterInterface::class)
114+
->addTag('security.voter');
115+
113116
if (PHP_VERSION_ID < 70000) {
114117
// add some required classes for compilation
115118
$this->addClassesToCompile(array(

0 commit comments

Comments
 (0)