Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2019 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2019 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | use Symfony\Component\DependencyInjection\Reference; |
39 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | |
40 | use Symfony\Component\Config\FileLocator; | |
41 | ||
42 | class CRM_Api4_Services { | |
43 | ||
44 | /** | |
45 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | |
46 | */ | |
47 | public static function hook_container($container) { | |
48 | $loader = new XmlFileLoader($container, new FileLocator(dirname(dirname(__DIR__)))); | |
49 | $loader->load('Civi/Api4/services.xml'); | |
50 | ||
51 | self::loadServices('Civi\Api4\Service\Spec\Provider', 'spec_provider', $container); | |
52 | self::loadServices('Civi\Api4\Event\Subscriber', 'event_subscriber', $container); | |
53 | ||
54 | $container->getDefinition('civi_api_kernel')->addMethodCall( | |
55 | 'registerApiProvider', | |
56 | [new Reference('action_object_provider')] | |
57 | ); | |
58 | ||
59 | // add event subscribers$container->get( | |
60 | $dispatcher = $container->getDefinition('dispatcher'); | |
61 | $subscribers = $container->findTaggedServiceIds('event_subscriber'); | |
62 | ||
63 | foreach (array_keys($subscribers) as $subscriber) { | |
64 | $dispatcher->addMethodCall( | |
65 | 'addSubscriber', | |
66 | [new Reference($subscriber)] | |
67 | ); | |
68 | } | |
69 | ||
70 | // add spec providers | |
71 | $providers = $container->findTaggedServiceIds('spec_provider'); | |
72 | $gatherer = $container->getDefinition('spec_gatherer'); | |
73 | ||
74 | foreach (array_keys($providers) as $provider) { | |
75 | $gatherer->addMethodCall( | |
76 | 'addSpecProvider', | |
77 | [new Reference($provider)] | |
78 | ); | |
79 | } | |
80 | ||
81 | if (defined('CIVICRM_UF') && CIVICRM_UF === 'UnitTests') { | |
82 | $loader->load('tests/phpunit/api/v4/services.xml'); | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * Load all services in a given directory | |
88 | * | |
89 | * @param string $namespace | |
90 | * @param string $tag | |
91 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | |
92 | */ | |
93 | public static function loadServices($namespace, $tag, $container) { | |
94 | $namespace = \CRM_Utils_File::addTrailingSlash($namespace, '\\'); | |
235e322d | 95 | $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')], |
bd2669ea CW |
96 | array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath') |
97 | ); | |
98 | foreach ($locations as $location) { | |
99 | $path = \CRM_Utils_File::addTrailingSlash(dirname($location)) . str_replace('\\', DIRECTORY_SEPARATOR, $namespace); | |
c1e9641d | 100 | $container->addResource(new \Symfony\Component\Config\Resource\DirectoryResource($path, ';\.php$;')); |
19b53e5b C |
101 | foreach (glob("$path*.php") as $file) { |
102 | $matches = []; | |
103 | preg_match('/(\w*).php/', $file, $matches); | |
104 | $serviceName = $namespace . array_pop($matches); | |
105 | $serviceClass = new \ReflectionClass($serviceName); | |
106 | if ($serviceClass->isInstantiable()) { | |
107 | $definition = $container->register(str_replace('\\', '_', $serviceName), $serviceName); | |
108 | $definition->addTag($tag); | |
109 | } | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | } |