Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
bc77d7c0 TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
7a4a2f65 | 19 | use Civi\Core\Event\EventScanner; |
19b53e5b C |
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) { | |
7a4a2f65 TO |
46 | $listenerMap = EventScanner::findListeners($container->findDefinition($subscriber)->getClass()); |
47 | $dispatcher->addMethodCall('addSubscriberServiceMap', [$subscriber, $listenerMap]); | |
19b53e5b C |
48 | } |
49 | ||
50 | // add spec providers | |
51 | $providers = $container->findTaggedServiceIds('spec_provider'); | |
52 | $gatherer = $container->getDefinition('spec_gatherer'); | |
53 | ||
54 | foreach (array_keys($providers) as $provider) { | |
55 | $gatherer->addMethodCall( | |
56 | 'addSpecProvider', | |
57 | [new Reference($provider)] | |
58 | ); | |
59 | } | |
60 | ||
c199b7ca EM |
61 | if (defined('CIVICRM_UF') && CIVICRM_UF === 'UnitTests' |
62 | && file_exists('tests/phpunit/api/v4/services.xml') | |
63 | ) { | |
19b53e5b C |
64 | $loader->load('tests/phpunit/api/v4/services.xml'); |
65 | } | |
66 | } | |
67 | ||
68 | /** | |
69 | * Load all services in a given directory | |
70 | * | |
71 | * @param string $namespace | |
72 | * @param string $tag | |
73 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | |
74 | */ | |
75 | public static function loadServices($namespace, $tag, $container) { | |
76 | $namespace = \CRM_Utils_File::addTrailingSlash($namespace, '\\'); | |
235e322d | 77 | $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')], |
bd2669ea CW |
78 | array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath') |
79 | ); | |
80 | foreach ($locations as $location) { | |
81 | $path = \CRM_Utils_File::addTrailingSlash(dirname($location)) . str_replace('\\', DIRECTORY_SEPARATOR, $namespace); | |
5f16ea7b TO |
82 | if (!file_exists($path) || !is_dir($path)) { |
83 | $resource = new \Symfony\Component\Config\Resource\FileExistenceResource($path); | |
84 | $container->addResource($resource); | |
5f16ea7b | 85 | } |
3733aeb0 | 86 | else { |
b1ad92c9 | 87 | $resource = new \Symfony\Component\Config\Resource\DirectoryResource($path, ';\.php$;'); |
b1ad92c9 SL |
88 | foreach (glob("$path*.php") as $file) { |
89 | $matches = []; | |
733f2b35 | 90 | preg_match('/(\w*)\.php$/', $file, $matches); |
b1ad92c9 SL |
91 | $serviceName = $namespace . array_pop($matches); |
92 | $serviceClass = new \ReflectionClass($serviceName); | |
93 | if ($serviceClass->isInstantiable()) { | |
94 | $definition = $container->register(str_replace('\\', '_', $serviceName), $serviceName); | |
95 | $definition->addTag($tag); | |
b646e91a | 96 | $definition->setPublic(TRUE); |
b1ad92c9 | 97 | } |
19b53e5b | 98 | } |
5f16ea7b | 99 | $container->addResource($resource); |
19b53e5b C |
100 | } |
101 | } | |
102 | } | |
103 | ||
104 | } |