47b428403148fd0300727d46b8c16dfbbb07057a
[civicrm-core.git] / CRM / Api4 / Services.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 use Symfony\Component\DependencyInjection\Reference;
21 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
22 use Symfony\Component\Config\FileLocator;
23
24 class CRM_Api4_Services {
25
26 /**
27 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
28 */
29 public static function hook_container($container) {
30 $loader = new XmlFileLoader($container, new FileLocator(dirname(dirname(__DIR__))));
31 $loader->load('Civi/Api4/services.xml');
32
33 self::loadServices('Civi\Api4\Service\Spec\Provider', 'spec_provider', $container);
34 self::loadServices('Civi\Api4\Event\Subscriber', 'event_subscriber', $container);
35
36 $container->getDefinition('civi_api_kernel')->addMethodCall(
37 'registerApiProvider',
38 [new Reference('action_object_provider')]
39 );
40
41 // add event subscribers$container->get(
42 $dispatcher = $container->getDefinition('dispatcher');
43 $subscribers = $container->findTaggedServiceIds('event_subscriber');
44
45 foreach (array_keys($subscribers) as $subscriber) {
46 $getSubscribedEvents = [$container->findDefinition($subscriber)->getClass(), 'getSubscribedEvents'];
47 $dispatcher->addMethodCall(
48 'addSubscriberServiceMap',
49 [$subscriber, $getSubscribedEvents()]
50 );
51 }
52
53 // add spec providers
54 $providers = $container->findTaggedServiceIds('spec_provider');
55 $gatherer = $container->getDefinition('spec_gatherer');
56
57 foreach (array_keys($providers) as $provider) {
58 $gatherer->addMethodCall(
59 'addSpecProvider',
60 [new Reference($provider)]
61 );
62 }
63
64 if (defined('CIVICRM_UF') && CIVICRM_UF === 'UnitTests') {
65 $loader->load('tests/phpunit/api/v4/services.xml');
66 }
67 }
68
69 /**
70 * Load all services in a given directory
71 *
72 * @param string $namespace
73 * @param string $tag
74 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
75 */
76 public static function loadServices($namespace, $tag, $container) {
77 $namespace = \CRM_Utils_File::addTrailingSlash($namespace, '\\');
78 $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
79 array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
80 );
81 foreach ($locations as $location) {
82 $path = \CRM_Utils_File::addTrailingSlash(dirname($location)) . str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
83 if (!file_exists($path) || !is_dir($path)) {
84 $resource = new \Symfony\Component\Config\Resource\FileExistenceResource($path);
85 $container->addResource($resource);
86 }
87 else {
88 $resource = new \Symfony\Component\Config\Resource\DirectoryResource($path, ';\.php$;');
89 foreach (glob("$path*.php") as $file) {
90 $matches = [];
91 preg_match('/(\w*)\.php$/', $file, $matches);
92 $serviceName = $namespace . array_pop($matches);
93 $serviceClass = new \ReflectionClass($serviceName);
94 if ($serviceClass->isInstantiable()) {
95 $definition = $container->register(str_replace('\\', '_', $serviceName), $serviceName);
96 $definition->addTag($tag);
97 $definition->setPublic(TRUE);
98 }
99 }
100 $container->addResource($resource);
101 }
102 }
103 }
104
105 }