Commit | Line | Data |
---|---|---|
fa184193 TO |
1 | <?php |
2 | namespace Civi\Core; | |
46bcf597 | 3 | |
c8d84653 | 4 | use Civi\Core\Event\EventScanner; |
10760fa1 | 5 | use Civi\Core\Lock\LockManager; |
40787e18 | 6 | use Symfony\Component\Config\ConfigCache; |
fa184193 TO |
7 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
8 | use Symfony\Component\DependencyInjection\Definition; | |
40787e18 | 9 | use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
fa184193 | 10 | use Symfony\Component\DependencyInjection\Reference; |
40787e18 | 11 | use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; |
fa184193 TO |
12 | |
13 | // TODO use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | |
14 | ||
6550386a EM |
15 | /** |
16 | * Class Container | |
17 | * @package Civi\Core | |
18 | */ | |
fa184193 TO |
19 | class Container { |
20 | ||
21 | const SELF = 'civi_container_factory'; | |
22 | ||
fa184193 | 23 | /** |
04855556 TO |
24 | * @param bool $reset |
25 | * Whether to forcibly rebuild the entire container. | |
fa184193 TO |
26 | * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface |
27 | */ | |
378e2654 | 28 | public static function singleton($reset = FALSE) { |
7f835399 TO |
29 | if ($reset || !isset(\Civi::$statics[__CLASS__]['container'])) { |
30 | self::boot(TRUE); | |
fa184193 | 31 | } |
7f835399 | 32 | return \Civi::$statics[__CLASS__]['container']; |
fa184193 TO |
33 | } |
34 | ||
35 | /** | |
40787e18 TO |
36 | * Find a cached container definition or construct a new one. |
37 | * | |
38 | * There are many weird contexts in which Civi initializes (eg different | |
39 | * variations of multitenancy and different permutations of CMS/CRM bootstrap), | |
40 | * and hook_container may fire a bit differently in each context. To mitigate | |
41 | * risk of leaks between environments, we compute a unique envID | |
42 | * (md5(DB_NAME, HTTP_HOST, SCRIPT_FILENAME, etc)) and use separate caches for | |
43 | * each (eg "templates_c/CachedCiviContainer.$ENVID.php"). | |
44 | * | |
45 | * Constants: | |
46 | * - CIVICRM_CONTAINER_CACHE -- 'always' [default], 'never', 'auto' | |
47 | * - CIVICRM_DSN | |
48 | * - CIVICRM_DOMAIN_ID | |
40787e18 | 49 | * |
34f3bbd9 | 50 | * @return \Symfony\Component\DependencyInjection\ContainerInterface |
40787e18 TO |
51 | */ |
52 | public function loadContainer() { | |
53 | // Note: The container's raison d'etre is to manage construction of other | |
54 | // services. Consequently, we assume a minimal service available -- the classloader | |
55 | // has been setup, and civicrm.settings.php is loaded, but nothing else works. | |
56 | ||
5497e016 | 57 | $cacheMode = defined('CIVICRM_CONTAINER_CACHE') ? CIVICRM_CONTAINER_CACHE : 'auto'; |
40787e18 TO |
58 | |
59 | // In pre-installation environments, don't bother with caching. | |
43a2114e | 60 | if (!defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || CIVICRM_UF === 'UnitTests' || $cacheMode === 'never' || \CRM_Utils_System::isInUpgradeMode()) { |
12e01332 TO |
61 | $containerBuilder = $this->createContainer(); |
62 | $containerBuilder->compile(); | |
63 | return $containerBuilder; | |
40787e18 TO |
64 | } |
65 | ||
83617886 | 66 | $envId = \CRM_Core_Config_Runtime::getId(); |
6f50d29c | 67 | $file = \Civi::paths()->getPath("[civicrm.compile]/CachedCiviContainer.{$envId}.php"); |
40787e18 | 68 | $containerConfigCache = new ConfigCache($file, $cacheMode === 'auto'); |
40787e18 TO |
69 | if (!$containerConfigCache->isFresh()) { |
70 | $containerBuilder = $this->createContainer(); | |
71 | $containerBuilder->compile(); | |
72 | $dumper = new PhpDumper($containerBuilder); | |
73 | $containerConfigCache->write( | |
c64f69d9 | 74 | $dumper->dump(['class' => 'CachedCiviContainer']), |
40787e18 TO |
75 | $containerBuilder->getResources() |
76 | ); | |
77 | } | |
78 | ||
79 | require_once $file; | |
80 | $c = new \CachedCiviContainer(); | |
40787e18 TO |
81 | return $c; |
82 | } | |
83 | ||
84 | /** | |
85 | * Construct a new container. | |
86 | * | |
34f3bbd9 | 87 | * @var \Symfony\Component\DependencyInjection\ContainerBuilder |
77b97be7 | 88 | * @return \Symfony\Component\DependencyInjection\ContainerBuilder |
fa184193 TO |
89 | */ |
90 | public function createContainer() { | |
91 | $civicrm_base_path = dirname(dirname(__DIR__)); | |
92 | $container = new ContainerBuilder(); | |
40787e18 TO |
93 | $container->addCompilerPass(new RegisterListenersPass('dispatcher')); |
94 | $container->addObjectResource($this); | |
fa184193 | 95 | $container->setParameter('civicrm_base_path', $civicrm_base_path); |
40787e18 | 96 | //$container->set(self::SELF, $this); |
762dc04d TO |
97 | |
98 | $container->addResource(new \Symfony\Component\Config\Resource\FileResource(__FILE__)); | |
99 | ||
40787e18 TO |
100 | $container->setDefinition(self::SELF, new Definition( |
101 | 'Civi\Core\Container', | |
c64f69d9 | 102 | [] |
40787e18 | 103 | )); |
fa184193 | 104 | |
505d8b83 TO |
105 | // TODO Move configuration to an external file; define caching structure |
106 | // if (empty($configDirectories)) { | |
107 | // throw new \Exception(__CLASS__ . ': Missing required properties (civicrmRoot, configDirectories)'); | |
108 | // } | |
109 | // $locator = new FileLocator($configDirectories); | |
110 | // $loaderResolver = new LoaderResolver(array( | |
111 | // new YamlFileLoader($container, $locator) | |
112 | // )); | |
113 | // $delegatingLoader = new DelegatingLoader($loaderResolver); | |
114 | // foreach (array('services.yml') as $file) { | |
115 | // $yamlUserFiles = $locator->locate($file, NULL, FALSE); | |
116 | // foreach ($yamlUserFiles as $file) { | |
117 | // $delegatingLoader->load($file); | |
118 | // } | |
119 | // } | |
fa184193 | 120 | |
16072ce1 | 121 | $container->setDefinition('angular', new Definition( |
40787e18 | 122 | 'Civi\Angular\Manager', |
c64f69d9 | 123 | [] |
16072ce1 | 124 | )) |
3c006cb8 | 125 | ->setFactory([new Reference(self::SELF), 'createAngularManager'])->setPublic(TRUE); |
16072ce1 | 126 | |
9bd30577 CW |
127 | $container->setDefinition('angularjs.loader', new Definition('Civi\Angular\AngularLoader', [])) |
128 | ->setPublic(TRUE); | |
129 | ||
fa184193 | 130 | $container->setDefinition('dispatcher', new Definition( |
762dc04d | 131 | 'Civi\Core\CiviEventDispatcher', |
42ccedc7 | 132 | [] |
fa184193 | 133 | )) |
3c006cb8 | 134 | ->setFactory([new Reference(self::SELF), 'createEventDispatcher'])->setPublic(TRUE); |
fa184193 | 135 | |
c65db512 | 136 | $container->setDefinition('magic_function_provider', new Definition( |
40787e18 | 137 | 'Civi\API\Provider\MagicFunctionProvider', |
c64f69d9 | 138 | [] |
3c006cb8 | 139 | ))->setPublic(TRUE); |
c65db512 | 140 | |
0f643fb2 | 141 | $container->setDefinition('civi_api_kernel', new Definition( |
40787e18 | 142 | 'Civi\API\Kernel', |
c64f69d9 | 143 | [new Reference('dispatcher'), new Reference('magic_function_provider')] |
0f643fb2 | 144 | )) |
3c006cb8 | 145 | ->setFactory([new Reference(self::SELF), 'createApiKernel'])->setPublic(TRUE); |
0f643fb2 | 146 | |
7b4bbb34 | 147 | $container->setDefinition('cxn_reg_client', new Definition( |
40787e18 | 148 | 'Civi\Cxn\Rpc\RegistrationClient', |
c64f69d9 | 149 | [] |
7b4bbb34 | 150 | )) |
3c006cb8 | 151 | ->setFactory('CRM_Cxn_BAO_Cxn::createRegistrationClient')->setPublic(TRUE); |
7b4bbb34 | 152 | |
3c006cb8 | 153 | $container->setDefinition('psr_log', new Definition('CRM_Core_Error_Log', []))->setPublic(TRUE); |
c213eb51 TO |
154 | $container->setDefinition('psr_log_manager', new Definition('Civi\Core\LogManager', []))->setPublic(TRUE); |
155 | // With the default log-manager, you may overload a channel by defining a service, e.g. | |
156 | // $container->setDefinition('log.ipn', new Definition('CRM_Core_Error_Log', []))->setPublic(TRUE); | |
6e5ad5ee | 157 | |
c64f69d9 | 158 | $basicCaches = [ |
7a19d718 TO |
159 | 'js_strings' => 'js_strings', |
160 | 'community_messages' => 'community_messages', | |
b1fc1ab0 | 161 | 'checks' => 'checks', |
19707a63 | 162 | 'session' => 'CiviCRM Session', |
90cdaa0e | 163 | 'long' => 'long', |
0136044e | 164 | 'groups' => 'contact groups', |
96689db3 | 165 | 'navigation' => 'navigation', |
792fce0b | 166 | 'customData' => 'custom data', |
9cdf85c1 | 167 | 'fields' => 'contact fields', |
81c4c148 | 168 | 'contactTypes' => 'contactTypes', |
eb151aab | 169 | 'metadata' => 'metadata', |
c64f69d9 | 170 | ]; |
a9538f25 TO |
171 | $verSuffixCaches = ['metadata']; |
172 | $verSuffix = '_' . preg_replace(';[^0-9a-z_];', '_', \CRM_Utils_System::version()); | |
7a19d718 | 173 | foreach ($basicCaches as $cacheSvc => $cacheGrp) { |
023bcf83 | 174 | $definitionParams = [ |
a9538f25 | 175 | 'name' => $cacheGrp . (in_array($cacheGrp, $verSuffixCaches) ? $verSuffix : ''), |
023bcf83 SL |
176 | 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], |
177 | ]; | |
178 | // For Caches that we don't really care about the ttl for and/or maybe accessed | |
179 | // fairly often we use the fastArrayDecorator which improves reads and writes, these | |
180 | // caches should also not have concurrency risk. | |
eb151aab | 181 | $fastArrayCaches = ['groups', 'navigation', 'customData', 'fields', 'contactTypes', 'metadata']; |
023bcf83 SL |
182 | if (in_array($cacheSvc, $fastArrayCaches)) { |
183 | $definitionParams['withArray'] = 'fast'; | |
184 | } | |
7a19d718 | 185 | $container->setDefinition("cache.{$cacheSvc}", new Definition( |
a4704404 | 186 | 'CRM_Utils_Cache_Interface', |
023bcf83 | 187 | [$definitionParams] |
3c006cb8 | 188 | ))->setFactory('CRM_Utils_Cache::create')->setPublic(TRUE); |
a4704404 | 189 | } |
3a84c0ab | 190 | |
da4effce SL |
191 | // PrevNextCache cannot use memory or array cache at the moment because the |
192 | // Code in CRM_Core_BAO_PrevNextCache assumes that this cache is sql backed. | |
193 | $container->setDefinition("cache.prevNextCache", new Definition( | |
194 | 'CRM_Utils_Cache_Interface', | |
195 | [ | |
196 | [ | |
197 | 'name' => 'CiviCRM Search PrevNextCache', | |
198 | 'type' => ['SqlGroup'], | |
199 | ], | |
200 | ] | |
3c006cb8 | 201 | ))->setFactory('CRM_Utils_Cache::create')->setPublic(TRUE); |
da4effce | 202 | |
5736060a ML |
203 | // Memcache is limited to 1 MB by default, and since this is not read often |
204 | // it does not make much sense in Redis either. | |
205 | $container->setDefinition('cache.extension_browser', new Definition( | |
206 | 'CRM_Utils_Cache_Interface', | |
207 | [ | |
208 | [ | |
209 | 'name' => 'extension_browser', | |
210 | 'type' => ['SqlGroup', 'ArrayCache'], | |
211 | ], | |
212 | ] | |
213 | ))->setFactory('CRM_Utils_Cache::create')->setPublic(TRUE); | |
214 | ||
4ed867e0 TO |
215 | $container->setDefinition('sql_triggers', new Definition( |
216 | 'Civi\Core\SqlTriggers', | |
c64f69d9 | 217 | [] |
3c006cb8 | 218 | ))->setPublic(TRUE); |
4ed867e0 | 219 | |
87e3fe24 TO |
220 | $container->setDefinition('asset_builder', new Definition( |
221 | 'Civi\Core\AssetBuilder', | |
c64f69d9 | 222 | [] |
3c006cb8 | 223 | ))->setPublic(TRUE); |
87e3fe24 | 224 | |
d89d2545 TO |
225 | $container->setDefinition('themes', new Definition( |
226 | 'Civi\Core\Themes', | |
227 | [] | |
3c006cb8 | 228 | ))->setPublic(TRUE); |
386fe6c2 EM |
229 | |
230 | $container->setDefinition('format', new Definition( | |
231 | '\Civi\Core\Format', | |
232 | [] | |
233 | ))->setPublic(TRUE); | |
d89d2545 | 234 | |
6e7adedc | 235 | $container->setDefinition('bundle.bootstrap3', new Definition('CRM_Core_Resources_Bundle', ['bootstrap3'])) |
3c86501c | 236 | ->setFactory('CRM_Core_Resources_Common::createBootstrap3Bundle')->setPublic(TRUE); |
6e7adedc | 237 | |
b2d8361e | 238 | $container->setDefinition('bundle.coreStyles', new Definition('CRM_Core_Resources_Bundle', ['coreStyles'])) |
3c86501c | 239 | ->setFactory('CRM_Core_Resources_Common::createStyleBundle')->setPublic(TRUE); |
b2d8361e TO |
240 | |
241 | $container->setDefinition('bundle.coreResources', new Definition('CRM_Core_Resources_Bundle', ['coreResources'])) | |
3c86501c | 242 | ->setFactory('CRM_Core_Resources_Common::createFullBundle')->setPublic(TRUE); |
b2d8361e | 243 | |
247eb841 | 244 | $container->setDefinition('pear_mail', new Definition('Mail')) |
3c86501c | 245 | ->setFactory('CRM_Utils_Mail::createMailer')->setPublic(TRUE); |
247eb841 | 246 | |
7dfe650d | 247 | $container->setDefinition('crypto.registry', new Definition('Civi\Crypto\CryptoRegistry')) |
23fa0118 | 248 | ->setFactory('Civi\Crypto\CryptoRegistry::createDefaultRegistry')->setPublic(TRUE); |
281eacd8 | 249 | |
7c5110c3 TO |
250 | $container->setDefinition('crypto.token', new Definition('Civi\Crypto\CryptoToken', [])) |
251 | ->setPublic(TRUE); | |
8d3452c4 | 252 | |
9c976d32 TO |
253 | $container->setDefinition('crypto.jwt', new Definition('Civi\Crypto\CryptoJwt', [])) |
254 | ->setPublic(TRUE); | |
255 | ||
7f835399 | 256 | if (empty(\Civi::$statics[__CLASS__]['boot'])) { |
272dfdc3 | 257 | throw new \RuntimeException('Cannot initialize container. Boot services are undefined.'); |
7f835399 TO |
258 | } |
259 | foreach (\Civi::$statics[__CLASS__]['boot'] as $bootService => $def) { | |
765daeac | 260 | $container->setDefinition($bootService, new Definition())->setSynthetic(TRUE)->setPublic(TRUE); |
83617886 TO |
261 | } |
262 | ||
c8074a93 | 263 | // Expose legacy singletons as services in the container. |
c64f69d9 | 264 | $singletons = [ |
c8074a93 | 265 | 'httpClient' => 'CRM_Utils_HttpClient', |
7b5937fe | 266 | 'cache.default' => 'CRM_Utils_Cache', |
a7c57397 | 267 | 'i18n' => 'CRM_Core_I18n', |
c8074a93 TO |
268 | // Maybe? 'config' => 'CRM_Core_Config', |
269 | // Maybe? 'smarty' => 'CRM_Core_Smarty', | |
c64f69d9 | 270 | ]; |
c8074a93 TO |
271 | foreach ($singletons as $name => $class) { |
272 | $container->setDefinition($name, new Definition( | |
273 | $class | |
274 | )) | |
3c006cb8 | 275 | ->setFactory([$class, 'singleton'])->setPublic(TRUE); |
c8074a93 | 276 | } |
e5c6b3cd | 277 | $container->setAlias('cache.short', 'cache.default')->setPublic(TRUE); |
c8074a93 | 278 | |
88d93265 | 279 | $container->setDefinition('civi.pipe', new Definition( |
25804d7a | 280 | 'Civi\Pipe\PipeSession', |
1e88220a | 281 | [] |
88d93265 | 282 | ))->setPublic(TRUE)->setShared(FALSE); |
1e88220a | 283 | |
223ba025 TO |
284 | $container->setDefinition('resources', new Definition( |
285 | 'CRM_Core_Resources', | |
286 | [new Reference('service_container')] | |
3c006cb8 | 287 | ))->setFactory([new Reference(self::SELF), 'createResources'])->setPublic(TRUE); |
223ba025 | 288 | |
bbcf0f46 TO |
289 | $container->setDefinition('resources.js_strings', new Definition( |
290 | 'CRM_Core_Resources_Strings', | |
291 | [new Reference('cache.js_strings')] | |
292 | ))->setPublic(TRUE); | |
293 | ||
780fd0e3 | 294 | $container->setDefinition('prevnext', new Definition( |
99098349 TO |
295 | 'CRM_Core_PrevNextCache_Interface', |
296 | [new Reference('service_container')] | |
3c006cb8 | 297 | ))->setFactory([new Reference(self::SELF), 'createPrevNextCache'])->setPublic(TRUE); |
99098349 TO |
298 | |
299 | $container->setDefinition('prevnext.driver.sql', new Definition( | |
780fd0e3 TO |
300 | 'CRM_Core_PrevNextCache_Sql', |
301 | [] | |
3c006cb8 | 302 | ))->setPublic(TRUE); |
780fd0e3 | 303 | |
751f3d98 TO |
304 | $container->setDefinition('prevnext.driver.redis', new Definition( |
305 | 'CRM_Core_PrevNextCache_Redis', | |
306 | [new Reference('cache_config')] | |
3c006cb8 | 307 | ))->setPublic(TRUE); |
751f3d98 TO |
308 | |
309 | $container->setDefinition('cache_config', new Definition('ArrayObject')) | |
3c006cb8 | 310 | ->setFactory([new Reference(self::SELF), 'createCacheConfig'])->setPublic(TRUE); |
751f3d98 | 311 | |
e0a667ad TO |
312 | $container->setDefinition('civi.activity.triggers', new Definition( |
313 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
c64f69d9 | 314 | ['civicrm_activity', 'Activity'] |
3c006cb8 | 315 | ))->addTag('kernel.event_listener', ['event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo'])->setPublic(TRUE); |
e0a667ad TO |
316 | |
317 | $container->setDefinition('civi.case.triggers', new Definition( | |
318 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
c64f69d9 | 319 | ['civicrm_case', 'Case'] |
3c006cb8 | 320 | ))->addTag('kernel.event_listener', ['event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo'])->setPublic(TRUE); |
e0a667ad | 321 | |
b21ffed7 TO |
322 | $container->setDefinition('civi.case.staticTriggers', new Definition( |
323 | 'Civi\Core\SqlTrigger\StaticTriggers', | |
c64f69d9 CW |
324 | [ |
325 | [ | |
326 | [ | |
327 | 'upgrade_check' => ['table' => 'civicrm_case', 'column' => 'modified_date'], | |
b21ffed7 TO |
328 | 'table' => 'civicrm_case_activity', |
329 | 'when' => 'AFTER', | |
c64f69d9 | 330 | 'event' => ['INSERT'], |
f0401a44 | 331 | 'sql' => "UPDATE civicrm_case SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.case_id;", |
c64f69d9 CW |
332 | ], |
333 | [ | |
334 | 'upgrade_check' => ['table' => 'civicrm_case', 'column' => 'modified_date'], | |
b21ffed7 TO |
335 | 'table' => 'civicrm_activity', |
336 | 'when' => 'BEFORE', | |
c64f69d9 | 337 | 'event' => ['UPDATE', 'DELETE'], |
f0401a44 | 338 | 'sql' => "UPDATE civicrm_case SET modified_date = CURRENT_TIMESTAMP WHERE id IN (SELECT ca.case_id FROM civicrm_case_activity ca WHERE ca.activity_id = OLD.id);", |
c64f69d9 CW |
339 | ], |
340 | ], | |
341 | ] | |
b21ffed7 | 342 | )) |
3c006cb8 | 343 | ->addTag('kernel.event_listener', ['event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo'])->setPublic(TRUE); |
b21ffed7 | 344 | |
43ceab3f TO |
345 | $container->setDefinition('civi_token_compat', new Definition( |
346 | 'Civi\Token\TokenCompatSubscriber', | |
c64f69d9 | 347 | [] |
3c006cb8 | 348 | ))->addTag('kernel.event_subscriber')->setPublic(TRUE); |
56df2d06 | 349 | $container->setDefinition("crm_mailing_action_tokens", new Definition( |
0f4031da | 350 | 'CRM_Mailing_ActionTokens', |
c64f69d9 | 351 | [] |
3c006cb8 | 352 | ))->addTag('kernel.event_subscriber')->setPublic(TRUE); |
43ceab3f | 353 | |
a7f29bf4 | 354 | foreach (['Activity', 'Contact', 'Contribute', 'Event', 'Mailing', 'Member', 'Case'] as $comp) { |
0f4031da | 355 | $container->setDefinition('crm_' . strtolower($comp) . '_tokens', new Definition( |
46f5566c | 356 | "CRM_{$comp}_Tokens", |
c64f69d9 | 357 | [] |
3c006cb8 | 358 | ))->addTag('kernel.event_subscriber')->setPublic(TRUE); |
46f5566c | 359 | } |
fa75f064 TO |
360 | $container->setDefinition('civi_token_impliedcontext', new Definition( |
361 | 'Civi\Token\ImpliedContextSubscriber', | |
362 | [] | |
363 | ))->addTag('kernel.event_subscriber')->setPublic(TRUE); | |
b7472bd6 EM |
364 | $container->setDefinition('crm_participant_tokens', new Definition( |
365 | 'CRM_Event_ParticipantTokens', | |
366 | [] | |
367 | ))->addTag('kernel.event_subscriber')->setPublic(TRUE); | |
0f4031da EM |
368 | $container->setDefinition('crm_contribution_recur_tokens', new Definition( |
369 | 'CRM_Contribute_RecurTokens', | |
370 | [] | |
371 | ))->addTag('kernel.event_subscriber')->setPublic(TRUE); | |
3c78698e EM |
372 | $container->setDefinition('crm_domain_tokens', new Definition( |
373 | 'CRM_Core_DomainTokens', | |
374 | [] | |
375 | ))->addTag('kernel.event_subscriber')->setPublic(TRUE); | |
50a23755 | 376 | |
c8d84653 TO |
377 | $dispatcherDefn = $container->getDefinition('dispatcher'); |
378 | foreach (\CRM_Core_DAO_AllCoreTables::getBaoClasses() as $baoEntity => $baoClass) { | |
379 | $listenerMap = EventScanner::findListeners($baoClass, $baoEntity); | |
380 | if ($listenerMap) { | |
381 | $file = (new \ReflectionClass($baoClass))->getFileName(); | |
382 | $container->addResource(new \Symfony\Component\Config\Resource\FileResource($file)); | |
383 | $dispatcherDefn->addMethodCall('addListenerMap', [$baoClass, $listenerMap]); | |
384 | } | |
385 | } | |
386 | ||
caaeea39 | 387 | \CRM_Api4_Services::hook_container($container); |
aa4343bc | 388 | |
40787e18 TO |
389 | \CRM_Utils_Hook::container($container); |
390 | ||
fa184193 TO |
391 | return $container; |
392 | } | |
393 | ||
16072ce1 TO |
394 | /** |
395 | * @return \Civi\Angular\Manager | |
396 | */ | |
397 | public function createAngularManager() { | |
398 | return new \Civi\Angular\Manager(\CRM_Core_Resources::singleton()); | |
399 | } | |
400 | ||
fa184193 | 401 | /** |
3c006cb8 | 402 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
fa184193 | 403 | */ |
42ccedc7 TO |
404 | public function createEventDispatcher() { |
405 | // Continue building on the original dispatcher created during bootstrap. | |
9bd30577 | 406 | /** @var CiviEventDispatcher $dispatcher */ |
42ccedc7 | 407 | $dispatcher = static::getBootService('dispatcher.boot'); |
ecb0ae5d | 408 | |
cc69f300 TO |
409 | // Sometimes, you have a generic event ('hook_pre') and wish to fire more targeted aliases ('hook_pre::MyEntity') to allow shorter subscriber lists. |
410 | $aliasEvent = function($eventName, $fieldName) { | |
411 | return function($e) use ($eventName, $fieldName) { | |
412 | \Civi::dispatcher()->dispatch($eventName . "::" . $e->{$fieldName}, $e); | |
413 | }; | |
414 | }; | |
4bf92107 TO |
415 | $aliasMethodEvent = function($eventName, $methodName) { |
416 | return function($e) use ($eventName, $methodName) { | |
417 | \Civi::dispatcher()->dispatch($eventName . "::" . $e->{$methodName}(), $e); | |
418 | }; | |
419 | }; | |
420 | ||
421 | $dispatcher->addListener('civi.api4.validate', $aliasMethodEvent('civi.api4.validate', 'getEntityName'), 100); | |
af4cccf7 | 422 | $dispatcher->addListener('civi.api4.authorizeRecord', $aliasMethodEvent('civi.api4.authorizeRecord', 'getEntityName'), 100); |
304fc59f | 423 | $dispatcher->addListener('civi.api4.entityTypes', ['\Civi\Api4\Provider\CustomEntityProvider', 'addCustomEntities'], 100); |
cc69f300 | 424 | |
1d440215 TO |
425 | $dispatcher->addListener('civi.core.install', ['\Civi\Core\InstallationCanary', 'check']); |
426 | $dispatcher->addListener('civi.core.install', ['\Civi\Core\DatabaseInitializer', 'initialize']); | |
427 | $dispatcher->addListener('civi.core.install', ['\Civi\Core\LocalizationInitializer', 'initialize']); | |
74effac4 | 428 | $dispatcher->addListener('hook_civicrm_post', ['\CRM_Core_Transaction', 'addPostCommit'], -1000); |
cc69f300 | 429 | $dispatcher->addListener('hook_civicrm_pre', $aliasEvent('hook_civicrm_pre', 'entity'), 100); |
9d4c4ffd | 430 | $dispatcher->addListener('civi.dao.preDelete', ['\CRM_Core_BAO_EntityTag', 'preDeleteOtherEntity']); |
cc69f300 | 431 | $dispatcher->addListener('hook_civicrm_post', $aliasEvent('hook_civicrm_post', 'entity'), 100); |
c64f69d9 CW |
432 | $dispatcher->addListener('hook_civicrm_post::Activity', ['\Civi\CCase\Events', 'fireCaseChange']); |
433 | $dispatcher->addListener('hook_civicrm_post::Case', ['\Civi\CCase\Events', 'fireCaseChange']); | |
434 | $dispatcher->addListener('hook_civicrm_caseChange', ['\Civi\CCase\Events', 'delegateToXmlListeners']); | |
435 | $dispatcher->addListener('hook_civicrm_caseChange', ['\Civi\CCase\SequenceListener', 'onCaseChange_static']); | |
f1aeb0ba | 436 | $dispatcher->addListener('hook_civicrm_cryptoRotateKey', ['\Civi\Crypto\RotateKeys', 'rotateSmtp']); |
c64f69d9 | 437 | $dispatcher->addListener('hook_civicrm_eventDefs', ['\Civi\Core\CiviEventInspector', 'findBuiltInEvents']); |
0d0031a0 | 438 | // TODO We need a better code-convention for metadata about non-hook events. |
c64f69d9 CW |
439 | $dispatcher->addListener('hook_civicrm_eventDefs', ['\Civi\API\Events', 'hookEventDefs']); |
440 | $dispatcher->addListener('hook_civicrm_eventDefs', ['\Civi\Core\Event\SystemInstallEvent', 'hookEventDefs']); | |
441 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\Civi\Angular\Page\Modules', 'buildAngularModules']); | |
9bd30577 | 442 | $dispatcher->addListenerService('civi.region.render', ['angularjs.loader', 'onRegionRender']); |
c64f69d9 CW |
443 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\CRM_Utils_VisualBundle', 'buildAssetJs']); |
444 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\CRM_Utils_VisualBundle', 'buildAssetCss']); | |
f22fb451 | 445 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\CRM_Core_Resources', 'renderMenubarStylesheet']); |
2daeb956 | 446 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\CRM_Core_Resources', 'renderL10nJs']); |
303017a1 | 447 | $dispatcher->addListener('hook_civicrm_coreResourceList', ['\CRM_Utils_System', 'appendCoreResources']); |
62c20d1e | 448 | $dispatcher->addListener('hook_civicrm_getAssetUrl', ['\CRM_Utils_System', 'alterAssetUrl']); |
28e28c36 | 449 | $dispatcher->addListener('hook_civicrm_alterExternUrl', ['\CRM_Utils_System', 'migrateExternUrl'], 1000); |
54043949 CW |
450 | // Not a BAO class so it can't implement hookInterface |
451 | $dispatcher->addListener('hook_civicrm_post', ['CRM_Utils_Recent', 'on_hook_civicrm_post']); | |
29541977 TO |
452 | $dispatcher->addListener('hook_civicrm_permissionList', ['CRM_Core_Permission_List', 'findConstPermissions'], 975); |
453 | $dispatcher->addListener('hook_civicrm_permissionList', ['CRM_Core_Permission_List', 'findCiviPermissions'], 950); | |
454 | $dispatcher->addListener('hook_civicrm_permissionList', ['CRM_Core_Permission_List', 'findCmsPermissions'], 925); | |
455 | ||
d357f225 | 456 | $dispatcher->addListener('hook_civicrm_postSave_civicrm_domain', ['\CRM_Core_BAO_Domain', 'onPostSave']); |
c64f69d9 | 457 | $dispatcher->addListener('hook_civicrm_unhandled_exception', [ |
9ae2d27b TO |
458 | 'CRM_Core_LegacyErrorHandler', |
459 | 'handleException', | |
c64f69d9 | 460 | ], -200); |
bc2feeb1 TO |
461 | $dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Activity_ActionMapping', 'onRegisterActionMappings']); |
462 | $dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Contact_ActionMapping', 'onRegisterActionMappings']); | |
463 | $dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Contribute_ActionMapping_ByPage', 'onRegisterActionMappings']); | |
464 | $dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Contribute_ActionMapping_ByType', 'onRegisterActionMappings']); | |
465 | $dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Event_ActionMapping', 'onRegisterActionMappings']); | |
466 | $dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Member_ActionMapping', 'onRegisterActionMappings']); | |
46f5566c | 467 | |
fa184193 TO |
468 | return $dispatcher; |
469 | } | |
0f643fb2 | 470 | |
10760fa1 | 471 | /** |
34f3bbd9 | 472 | * @return \Civi\Core\Lock\LockManager |
10760fa1 | 473 | */ |
83617886 | 474 | public static function createLockManager() { |
10760fa1 TO |
475 | // Ideally, downstream implementers could override any definitions in |
476 | // the container. For now, we'll make-do with some define()s. | |
477 | $lm = new LockManager(); | |
478 | $lm | |
c64f69d9 CW |
479 | ->register('/^cache\./', defined('CIVICRM_CACHE_LOCK') ? CIVICRM_CACHE_LOCK : ['CRM_Core_Lock', 'createScopedLock']) |
480 | ->register('/^data\./', defined('CIVICRM_DATA_LOCK') ? CIVICRM_DATA_LOCK : ['CRM_Core_Lock', 'createScopedLock']) | |
481 | ->register('/^worker\.mailing\.send\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : ['CRM_Core_Lock', 'createCivimailLock']) | |
482 | ->register('/^worker\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : ['CRM_Core_Lock', 'createScopedLock']); | |
10760fa1 TO |
483 | |
484 | // Registrations may use complex resolver expressions, but (as a micro-optimization) | |
485 | // the default factory is specified as an array. | |
486 | ||
487 | return $lm; | |
488 | } | |
489 | ||
0f643fb2 TO |
490 | /** |
491 | * @param \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher | |
2a6da8d7 EM |
492 | * @param $magicFunctionProvider |
493 | * | |
0f643fb2 TO |
494 | * @return \Civi\API\Kernel |
495 | */ | |
c65db512 | 496 | public function createApiKernel($dispatcher, $magicFunctionProvider) { |
0a946de2 | 497 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\ChainSubscriber()); |
b55bc593 | 498 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); |
bace5cd9 | 499 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); |
c65db512 | 500 | $dispatcher->addSubscriber($magicFunctionProvider); |
d0c9daa4 | 501 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); |
dcef11bd | 502 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\APIv3SchemaAdapter()); |
c64f69d9 | 503 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\WrapperAdapter([ |
6d3bdc98 TO |
504 | \CRM_Utils_API_HTMLInputCoder::singleton(), |
505 | \CRM_Utils_API_NullOutputCoder::singleton(), | |
506 | \CRM_Utils_API_ReloadOption::singleton(), | |
507 | \CRM_Utils_API_MatchOption::singleton(), | |
c64f69d9 | 508 | ])); |
2aafb0fc | 509 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\DebugSubscriber()); |
82376c19 TO |
510 | $kernel = new \Civi\API\Kernel($dispatcher); |
511 | ||
512 | $reflectionProvider = new \Civi\API\Provider\ReflectionProvider($kernel); | |
513 | $dispatcher->addSubscriber($reflectionProvider); | |
514 | ||
56154d36 TO |
515 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\DynamicFKAuthorization( |
516 | $kernel, | |
517 | 'Attachment', | |
c64f69d9 | 518 | ['create', 'get', 'delete'], |
2e37a19f | 519 | // Given a file ID, determine the entity+table it's attached to. |
56154d36 TO |
520 | 'SELECT if(cf.id,1,0) as is_valid, cef.entity_table, cef.entity_id |
521 | FROM civicrm_file cf | |
522 | LEFT JOIN civicrm_entity_file cef ON cf.id = cef.file_id | |
523 | WHERE cf.id = %1', | |
29468114 TO |
524 | // Get a list of custom fields (field_name,table_name,extends) |
525 | 'SELECT concat("custom_",fld.id) as field_name, | |
526 | grp.table_name as table_name, | |
527 | grp.extends as extends | |
528 | FROM civicrm_custom_field fld | |
529 | INNER JOIN civicrm_custom_group grp ON fld.custom_group_id = grp.id | |
530 | WHERE fld.data_type = "File" | |
09871447 | 531 | ' |
56154d36 TO |
532 | )); |
533 | ||
c64f69d9 | 534 | $kernel->setApiProviders([ |
82376c19 TO |
535 | $reflectionProvider, |
536 | $magicFunctionProvider, | |
c64f69d9 | 537 | ]); |
82376c19 | 538 | |
0f643fb2 TO |
539 | return $kernel; |
540 | } | |
96025800 | 541 | |
223ba025 | 542 | /** |
34f3bbd9 | 543 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
223ba025 TO |
544 | * @return \CRM_Core_Resources |
545 | */ | |
546 | public static function createResources($container) { | |
547 | $sys = \CRM_Extension_System::singleton(); | |
548 | return new \CRM_Core_Resources( | |
549 | $sys->getMapper(), | |
bbcf0f46 | 550 | new \CRM_Core_Resources_Strings($container->get('cache.js_strings')), |
223ba025 TO |
551 | \CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode' |
552 | ); | |
553 | } | |
554 | ||
99098349 | 555 | /** |
34f3bbd9 | 556 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
99098349 TO |
557 | * @return \CRM_Core_PrevNextCache_Interface |
558 | */ | |
559 | public static function createPrevNextCache($container) { | |
e28bf654 | 560 | $setting = \Civi::settings()->get('prevNextBackend'); |
272dfdc3 | 561 | if (!$setting || $setting === 'default') { |
e28bf654 TO |
562 | $cacheDriver = \CRM_Utils_Cache::getCacheDriver(); |
563 | $service = 'prevnext.driver.' . strtolower($cacheDriver); | |
272dfdc3 | 564 | return $container->has($service) |
e28bf654 TO |
565 | ? $container->get($service) |
566 | : $container->get('prevnext.driver.sql'); | |
567 | } | |
272dfdc3 | 568 | return $container->get('prevnext.driver.' . $setting); |
99098349 TO |
569 | } |
570 | ||
272dfdc3 | 571 | /** |
572 | * @return \ArrayObject | |
573 | */ | |
751f3d98 TO |
574 | public static function createCacheConfig() { |
575 | $driver = \CRM_Utils_Cache::getCacheDriver(); | |
576 | $settings = \CRM_Utils_Cache::getCacheSettings($driver); | |
577 | $settings['driver'] = $driver; | |
578 | return new \ArrayObject($settings); | |
579 | } | |
580 | ||
83617886 TO |
581 | /** |
582 | * Get a list of boot services. | |
583 | * | |
584 | * These are services which must be setup *before* the container can operate. | |
585 | * | |
7f835399 | 586 | * @param bool $loadFromDB |
83617886 TO |
587 | * @throws \CRM_Core_Exception |
588 | */ | |
7f835399 | 589 | public static function boot($loadFromDB) { |
56eafc21 | 590 | // Array(string $serviceId => object $serviceInstance). |
c64f69d9 | 591 | $bootServices = []; |
7f835399 | 592 | \Civi::$statics[__CLASS__]['boot'] = &$bootServices; |
d4330c62 | 593 | |
56eafc21 | 594 | $bootServices['runtime'] = $runtime = new \CRM_Core_Config_Runtime(); |
7f835399 | 595 | $runtime->initialize($loadFromDB); |
d4330c62 | 596 | |
b1db062e H |
597 | $bootServices['paths'] = new \Civi\Core\Paths(); |
598 | ||
42ccedc7 | 599 | $bootServices['dispatcher.boot'] = new CiviEventDispatcher(); |
f38bc674 | 600 | $bootServices['dispatcher.boot']->addListener('civi.queue.runTask.start', ['CRM_Upgrade_DispatchPolicy', 'onRunTask']); |
27e05c5c TO |
601 | |
602 | // Quality control: There should be no pre-boot hooks because they make it harder to understand/support/refactor. | |
603 | // If a pre-boot hook sneaks in, we'll raise an error. | |
604 | $bootDispatchPolicy = [ | |
605 | '/^hook_/' => 'not-ready', | |
606 | '/^civi\./' => 'run', | |
607 | ]; | |
27e05c5c | 608 | $bootServices['dispatcher.boot']->setDispatchPolicy($bootDispatchPolicy); |
42ccedc7 | 609 | |
7f835399 | 610 | $class = $runtime->userFrameworkClass; |
56eafc21 | 611 | $bootServices['userSystem'] = $userSystem = new $class(); |
7f835399 TO |
612 | $userSystem->initialize(); |
613 | ||
614 | $userPermissionClass = 'CRM_Core_Permission_' . $runtime->userFramework; | |
56eafc21 | 615 | $bootServices['userPermissionClass'] = new $userPermissionClass(); |
7f835399 | 616 | |
c64f69d9 | 617 | $bootServices['cache.settings'] = \CRM_Utils_Cache::create([ |
56eafc21 | 618 | 'name' => 'settings', |
c64f69d9 CW |
619 | 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], |
620 | ]); | |
7f835399 | 621 | |
56eafc21 | 622 | $bootServices['settings_manager'] = new \Civi\Core\SettingsManager($bootServices['cache.settings']); |
7f835399 | 623 | |
56eafc21 | 624 | $bootServices['lockManager'] = self::createLockManager(); |
7f835399 TO |
625 | |
626 | if ($loadFromDB && $runtime->dsn) { | |
3a036b15 | 627 | \CRM_Core_DAO::init($runtime->dsn); |
edbcbd96 | 628 | \CRM_Utils_Hook::singleton(TRUE); |
7f835399 | 629 | \CRM_Extension_System::singleton(TRUE); |
15e47dfc | 630 | \CRM_Extension_System::singleton()->getClassLoader()->register(); |
8aff4734 | 631 | \CRM_Extension_System::singleton()->getMixinLoader()->run(); |
ebd1137d | 632 | \CRM_Utils_Hook::singleton()->commonBuildModuleList('civicrm_boot'); |
ffb92029 | 633 | $bootServices['dispatcher.boot']->setDispatchPolicy(\CRM_Core_Config::isUpgradeMode() ? \CRM_Upgrade_DispatchPolicy::pick() : NULL); |
7f835399 | 634 | |
1b81ed50 | 635 | $runtime->includeCustomPath(); |
636 | ||
7f835399 | 637 | $c = new self(); |
56eafc21 TO |
638 | $container = $c->loadContainer(); |
639 | foreach ($bootServices as $name => $obj) { | |
640 | $container->set($name, $obj); | |
641 | } | |
642 | \Civi::$statics[__CLASS__]['container'] = $container; | |
acccf150 TO |
643 | // Ensure all container-based serivces have a chance to add their listeners. |
644 | // Without this, it's a matter of happenstance (dependent upon particular page-request/configuration/etc). | |
e103005b | 645 | $container->get('dispatcher'); |
646 | ||
83617886 | 647 | } |
27e05c5c | 648 | else { |
ffb92029 | 649 | $bootServices['dispatcher.boot']->setDispatchPolicy(\CRM_Core_Config::isUpgradeMode() ? \CRM_Upgrade_DispatchPolicy::pick() : NULL); |
27e05c5c | 650 | } |
83617886 TO |
651 | } |
652 | ||
272dfdc3 | 653 | /** |
654 | * @param string $name | |
655 | * | |
656 | * @return mixed | |
657 | */ | |
83617886 | 658 | public static function getBootService($name) { |
56eafc21 | 659 | return \Civi::$statics[__CLASS__]['boot'][$name]; |
83617886 TO |
660 | } |
661 | ||
4d8e83b6 TO |
662 | /** |
663 | * Determine whether the container services are available. | |
664 | * | |
665 | * @return bool | |
666 | */ | |
667 | public static function isContainerBooted() { | |
668 | return isset(\Civi::$statics[__CLASS__]['container']); | |
669 | } | |
670 | ||
fa184193 | 671 | } |