Skip to content
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

(REF) Extract compiler passes from CRM_Api4_Services #24283

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions CRM/Api4/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,26 +36,6 @@ public static function hook_container($container) {
'registerApiProvider',
[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');

foreach (array_keys($providers) as $provider) {
$gatherer->addMethodCall(
'addSpecProvider',
[new Reference($provider)]
);
}
}

/**
Expand Down
35 changes: 35 additions & 0 deletions Civi/Core/Compiler/EventScannerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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]);
}
}

}
26 changes: 26 additions & 0 deletions Civi/Core/Compiler/SpecProviderPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Civi\Core\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Scan the container for services tagged as 'spec_provider'.
* Register each with the `spec_gatherer`.
*/
class SpecProviderPass implements CompilerPassInterface {

public function process(ContainerBuilder $container) {
$providers = $container->findTaggedServiceIds('spec_provider');
$gatherer = $container->getDefinition('spec_gatherer');

foreach (array_keys($providers) as $provider) {
$gatherer->addMethodCall(
'addSpecProvider',
[new Reference($provider)]
);
}
}

}
4 changes: 4 additions & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Civi\Core;

use Civi\Core\Compiler\EventScannerPass;
use Civi\Core\Compiler\SpecProviderPass;
use Civi\Core\Event\EventScanner;
use Civi\Core\Lock\LockManager;
use Symfony\Component\Config\ConfigCache;
Expand Down Expand Up @@ -90,6 +92,8 @@ public function loadContainer() {
public function createContainer() {
$civicrm_base_path = dirname(dirname(__DIR__));
$container = new ContainerBuilder();
$container->addCompilerPass(new EventScannerPass());
$container->addCompilerPass(new SpecProviderPass());
$container->addCompilerPass(new RegisterListenersPass());
$container->addObjectResource($this);
$container->setParameter('civicrm_base_path', $civicrm_base_path);
Expand Down