c00487df5f89ee81520aa06e6e0ae76e0fb49cd3
[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 * $Id$
18 *
19 */
20
21
22 use Symfony\Component\DependencyInjection\Reference;
23 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
24 use Symfony\Component\Config\FileLocator;
25
26 class CRM_Api4_Services {
27
28 /**
29 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
30 */
31 public static function hook_container($container) {
32 $loader = new XmlFileLoader($container, new FileLocator(dirname(dirname(__DIR__))));
33 $loader->load('Civi/Api4/services.xml');
34
35 self::loadServices('Civi\Api4\Service\Spec\Provider', 'spec_provider', $container);
36 self::loadServices('Civi\Api4\Event\Subscriber', 'event_subscriber', $container);
37
38 $container->getDefinition('civi_api_kernel')->addMethodCall(
39 'registerApiProvider',
40 [new Reference('action_object_provider')]
41 );
42
43 // add event subscribers$container->get(
44 $dispatcher = $container->getDefinition('dispatcher');
45 $subscribers = $container->findTaggedServiceIds('event_subscriber');
46
47 foreach (array_keys($subscribers) as $subscriber) {
48 $dispatcher->addMethodCall(
49 'addSubscriber',
50 [new Reference($subscriber)]
51 );
52 }
53
54 // add spec providers
55 $providers = $container->findTaggedServiceIds('spec_provider');
56 $gatherer = $container->getDefinition('spec_gatherer');
57
58 foreach (array_keys($providers) as $provider) {
59 $gatherer->addMethodCall(
60 'addSpecProvider',
61 [new Reference($provider)]
62 );
63 }
64
65 if (defined('CIVICRM_UF') && CIVICRM_UF === 'UnitTests') {
66 $loader->load('tests/phpunit/api/v4/services.xml');
67 }
68 }
69
70 /**
71 * Load all services in a given directory
72 *
73 * @param string $namespace
74 * @param string $tag
75 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
76 */
77 public static function loadServices($namespace, $tag, $container) {
78 $namespace = \CRM_Utils_File::addTrailingSlash($namespace, '\\');
79 $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
80 array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
81 );
82 foreach ($locations as $location) {
83 $path = \CRM_Utils_File::addTrailingSlash(dirname($location)) . str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
84 try {
85 $resource = new \Symfony\Component\Config\Resource\DirectoryResource($path, ';\.php$;');
86 $addResource = FALSE;
87 foreach (glob("$path*.php") as $file) {
88 $matches = [];
89 preg_match('/(\w*)\.php$/', $file, $matches);
90 $serviceName = $namespace . array_pop($matches);
91 $serviceClass = new \ReflectionClass($serviceName);
92 if ($serviceClass->isInstantiable()) {
93 $definition = $container->register(str_replace('\\', '_', $serviceName), $serviceName);
94 $definition->addTag($tag);
95 $addResource = TRUE;
96 }
97 }
98 if ($addResource) {
99 $container->addResource($resource);
100 }
101 }
102 catch (\InvalidArgumentException $e) {
103 //Directory is not found so lets not do anything i suppose.
104 }
105 }
106 }
107
108 }