* @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;
[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');
--- /dev/null
+<?php
+namespace Civi\Core\Compiler;
+
+use Civi\Core\Event\EventScanner;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * Scan for services that have the tag 'event_subscriber'.
+ *
+ * Specifically, any class tagged as `event_subscriber` will be scanned for event listeners.
+ * The subscriber should implement a relevant interface, such as:
+ *
+ * - HookInterface: The class uses `hook_*()` methods.
+ * - EventSubscriberInterface: the class provides a `getSubscribedEvents()` method.
+ *
+ * The list of listeners will be extracted stored as part of the container-cache.
+ *
+ * NOTE: This is similar to Symfony's `RegisterListenersPass()` but differs in a few ways:
+ * - Works with both HookInterface and EventSubscriberInterface
+ * - Watches tag 'event_subscriber' (not 'kernel.event_listener' or 'kernel.event_subscriber')
+ */
+class EventScannerPass implements CompilerPassInterface {
+
+ public function process(ContainerBuilder $container) {
+ $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]);
+ }
+ }
+
+}
<?php
namespace Civi\Core;
+use Civi\Core\Compiler\EventScannerPass;
use Civi\Core\Event\EventScanner;
use Civi\Core\Lock\LockManager;
use Symfony\Component\Config\ConfigCache;
public function createContainer() {
$civicrm_base_path = dirname(dirname(__DIR__));
$container = new ContainerBuilder();
+ $container->addCompilerPass(new EventScannerPass());
$container->addCompilerPass(new RegisterListenersPass());
$container->addObjectResource($this);
$container->setParameter('civicrm_base_path', $civicrm_base_path);