4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 use Civi\Core\Event\EventScanner
;
20 use Symfony\Component\DependencyInjection\Reference
;
21 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader
;
22 use Symfony\Component\Config\FileLocator
;
24 class CRM_Api4_Services
{
27 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
29 public static function hook_container($container) {
30 $loader = new XmlFileLoader($container, new FileLocator(dirname(dirname(__DIR__
))));
31 $loader->load('Civi/Api4/services.xml');
33 self
::loadServices('Civi\Api4\Service\Spec\Provider', 'spec_provider', $container);
34 self
::loadServices('Civi\Api4\Event\Subscriber', 'event_subscriber', $container);
36 $container->getDefinition('civi_api_kernel')->addMethodCall(
37 'registerApiProvider',
38 [new Reference('action_object_provider')]
41 // add event subscribers$container->get(
42 $dispatcher = $container->getDefinition('dispatcher');
43 $subscribers = $container->findTaggedServiceIds('event_subscriber');
45 foreach (array_keys($subscribers) as $subscriber) {
46 $listenerMap = EventScanner
::findListeners($container->findDefinition($subscriber)->getClass());
47 $dispatcher->addMethodCall('addSubscriberServiceMap', [$subscriber, $listenerMap]);
51 $providers = $container->findTaggedServiceIds('spec_provider');
52 $gatherer = $container->getDefinition('spec_gatherer');
54 foreach (array_keys($providers) as $provider) {
55 $gatherer->addMethodCall(
57 [new Reference($provider)]
63 * Load all services in a given directory
65 * @param string $namespace
67 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
69 public static function loadServices($namespace, $tag, $container) {
70 $namespace = \CRM_Utils_File
::addTrailingSlash($namespace, '\\');
71 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
72 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
74 foreach ($locations as $location) {
75 $path = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . str_replace('\\', DIRECTORY_SEPARATOR
, $namespace);
76 if (!file_exists($path) ||
!is_dir($path)) {
77 $resource = new \Symfony\Component\Config\
Resource\
FileExistenceResource($path);
78 $container->addResource($resource);
81 $resource = new \Symfony\Component\Config\
Resource\
DirectoryResource($path, ';\.php$;');
82 foreach (glob("$path*.php") as $file) {
84 preg_match('/(\w*)\.php$/', $file, $matches);
85 $serviceName = $namespace . array_pop($matches);
86 $serviceClass = new \
ReflectionClass($serviceName);
87 if ($serviceClass->isInstantiable()) {
88 $definition = $container->register(str_replace('\\', '_', $serviceName), $serviceName);
89 $definition->addTag($tag);
90 $definition->setPublic(TRUE);
93 $container->addResource($resource);