From: Tim Otten Date: Mon, 15 Aug 2022 23:32:04 +0000 (-0700) Subject: (REF) Define EventScannerPass (extracted from CRM_Api4_Services) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c354cc12e344659cf52e2562dae8a9d9237d34f2;p=civicrm-core.git (REF) Define EventScannerPass (extracted from CRM_Api4_Services) The `CRM_Api4_Services` implementation has a secret behavior that applies to all services - it detects the tag 'event_subscriber' and registers with the dispatcher. This kind of behavior is often implemented as "compiler pass" and given its own documentation. --- diff --git a/CRM/Api4/Services.php b/CRM/Api4/Services.php index f5581efd0f..34f64e0239 100644 --- a/CRM/Api4/Services.php +++ b/CRM/Api4/Services.php @@ -16,7 +16,6 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ -use Civi\Core\Event\EventScanner; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\Config\FileLocator; @@ -38,15 +37,6 @@ class CRM_Api4_Services { [new Reference('action_object_provider')] ); - // add event subscribers$container->get( - $dispatcher = $container->getDefinition('dispatcher'); - $subscribers = $container->findTaggedServiceIds('event_subscriber'); - - foreach (array_keys($subscribers) as $subscriber) { - $listenerMap = EventScanner::findListeners($container->findDefinition($subscriber)->getClass()); - $dispatcher->addMethodCall('addSubscriberServiceMap', [$subscriber, $listenerMap]); - } - // add spec providers $providers = $container->findTaggedServiceIds('spec_provider'); $gatherer = $container->getDefinition('spec_gatherer'); diff --git a/Civi/Core/Compiler/EventScannerPass.php b/Civi/Core/Compiler/EventScannerPass.php new file mode 100644 index 0000000000..9ad002add8 --- /dev/null +++ b/Civi/Core/Compiler/EventScannerPass.php @@ -0,0 +1,35 @@ +getDefinition('dispatcher'); + $subscribers = $container->findTaggedServiceIds('event_subscriber'); + + foreach (array_keys($subscribers) as $subscriber) { + $listenerMap = EventScanner::findListeners($container->findDefinition($subscriber)->getClass()); + $dispatcher->addMethodCall('addSubscriberServiceMap', [$subscriber, $listenerMap]); + } + } + +} diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index d0b70f120e..8cc7d2c0cf 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -1,6 +1,7 @@ addCompilerPass(new EventScannerPass()); $container->addCompilerPass(new RegisterListenersPass()); $container->addObjectResource($this); $container->setParameter('civicrm_base_path', $civicrm_base_path);