4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
38 use Symfony\Component\DependencyInjection\Reference
;
39 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader
;
40 use Symfony\Component\Config\FileLocator
;
42 class CRM_Api4_Services
{
45 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
47 public static function hook_container($container) {
48 $loader = new XmlFileLoader($container, new FileLocator(dirname(dirname(__DIR__
))));
49 $loader->load('Civi/Api4/services.xml');
51 self
::loadServices('Civi\Api4\Service\Spec\Provider', 'spec_provider', $container);
52 self
::loadServices('Civi\Api4\Event\Subscriber', 'event_subscriber', $container);
54 $container->getDefinition('civi_api_kernel')->addMethodCall(
55 'registerApiProvider',
56 [new Reference('action_object_provider')]
59 // add event subscribers$container->get(
60 $dispatcher = $container->getDefinition('dispatcher');
61 $subscribers = $container->findTaggedServiceIds('event_subscriber');
63 foreach (array_keys($subscribers) as $subscriber) {
64 $dispatcher->addMethodCall(
66 [new Reference($subscriber)]
71 $providers = $container->findTaggedServiceIds('spec_provider');
72 $gatherer = $container->getDefinition('spec_gatherer');
74 foreach (array_keys($providers) as $provider) {
75 $gatherer->addMethodCall(
77 [new Reference($provider)]
81 if (defined('CIVICRM_UF') && CIVICRM_UF
=== 'UnitTests') {
82 $loader->load('tests/phpunit/api/v4/services.xml');
87 * Load all services in a given directory
89 * @param string $namespace
91 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
93 public static function loadServices($namespace, $tag, $container) {
94 $namespace = \CRM_Utils_File
::addTrailingSlash($namespace, '\\');
95 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
96 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
98 foreach ($locations as $location) {
99 $path = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . str_replace('\\', DIRECTORY_SEPARATOR
, $namespace);
100 $container->addResource(new \Symfony\Component\Config\
Resource\
DirectoryResource($path, ';\.php$;'));
101 foreach (glob("$path*.php") as $file) {
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);