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 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
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, '\\'); | |
235e322d | 79 | $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')], |
bd2669ea CW |
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); | |
b1ad92c9 SL |
84 | try { |
85 | $resource = new \Symfony\Component\Config\Resource\DirectoryResource($path, ';\.php$;'); | |
86 | $container->addResource($resource); | |
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 | } | |
19b53e5b C |
96 | } |
97 | } | |
b1ad92c9 SL |
98 | catch (\InvalidArgumentException $e) { |
99 | //Directory is not found so lets not do anything i suppose. | |
100 | } | |
19b53e5b C |
101 | } |
102 | } | |
103 | ||
104 | } |