Commit | Line | Data |
---|---|---|
fa184193 TO |
1 | <?php |
2 | namespace Civi\Core; | |
46bcf597 | 3 | |
0085db83 | 4 | use Civi\Core\Event\SystemInstallEvent; |
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. | |
6f50d29c | 60 | if (!defined('CIVICRM_DSN') || $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 | )) |
c64f69d9 | 125 | ->setFactory([new Reference(self::SELF), 'createAngularManager']); |
16072ce1 | 126 | |
fa184193 | 127 | $container->setDefinition('dispatcher', new Definition( |
762dc04d | 128 | 'Civi\Core\CiviEventDispatcher', |
c64f69d9 | 129 | [new Reference('service_container')] |
fa184193 | 130 | )) |
c64f69d9 | 131 | ->setFactory([new Reference(self::SELF), 'createEventDispatcher']); |
fa184193 | 132 | |
c65db512 | 133 | $container->setDefinition('magic_function_provider', new Definition( |
40787e18 | 134 | 'Civi\API\Provider\MagicFunctionProvider', |
c64f69d9 | 135 | [] |
c65db512 TO |
136 | )); |
137 | ||
0f643fb2 | 138 | $container->setDefinition('civi_api_kernel', new Definition( |
40787e18 | 139 | 'Civi\API\Kernel', |
c64f69d9 | 140 | [new Reference('dispatcher'), new Reference('magic_function_provider')] |
0f643fb2 | 141 | )) |
c64f69d9 | 142 | ->setFactory([new Reference(self::SELF), 'createApiKernel']); |
0f643fb2 | 143 | |
7b4bbb34 | 144 | $container->setDefinition('cxn_reg_client', new Definition( |
40787e18 | 145 | 'Civi\Cxn\Rpc\RegistrationClient', |
c64f69d9 | 146 | [] |
7b4bbb34 | 147 | )) |
5eda8e9b | 148 | ->setFactory('CRM_Cxn_BAO_Cxn::createRegistrationClient'); |
7b4bbb34 | 149 | |
c64f69d9 | 150 | $container->setDefinition('psr_log', new Definition('CRM_Core_Error_Log', [])); |
6e5ad5ee | 151 | |
c64f69d9 | 152 | $basicCaches = [ |
7a19d718 TO |
153 | 'js_strings' => 'js_strings', |
154 | 'community_messages' => 'community_messages', | |
b1fc1ab0 | 155 | 'checks' => 'checks', |
19707a63 | 156 | 'session' => 'CiviCRM Session', |
90cdaa0e | 157 | 'long' => 'long', |
0136044e | 158 | 'groups' => 'contact groups', |
96689db3 | 159 | 'navigation' => 'navigation', |
792fce0b | 160 | 'customData' => 'custom data', |
9cdf85c1 | 161 | 'fields' => 'contact fields', |
81c4c148 | 162 | 'contactTypes' => 'contactTypes', |
eb151aab | 163 | 'metadata' => 'metadata', |
c64f69d9 | 164 | ]; |
7a19d718 | 165 | foreach ($basicCaches as $cacheSvc => $cacheGrp) { |
023bcf83 SL |
166 | $definitionParams = [ |
167 | 'name' => $cacheGrp, | |
168 | 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], | |
169 | ]; | |
170 | // For Caches that we don't really care about the ttl for and/or maybe accessed | |
171 | // fairly often we use the fastArrayDecorator which improves reads and writes, these | |
172 | // caches should also not have concurrency risk. | |
eb151aab | 173 | $fastArrayCaches = ['groups', 'navigation', 'customData', 'fields', 'contactTypes', 'metadata']; |
023bcf83 SL |
174 | if (in_array($cacheSvc, $fastArrayCaches)) { |
175 | $definitionParams['withArray'] = 'fast'; | |
176 | } | |
7a19d718 | 177 | $container->setDefinition("cache.{$cacheSvc}", new Definition( |
a4704404 | 178 | 'CRM_Utils_Cache_Interface', |
023bcf83 | 179 | [$definitionParams] |
a892989e | 180 | ))->setFactory('CRM_Utils_Cache::create'); |
a4704404 | 181 | } |
3a84c0ab | 182 | |
da4effce SL |
183 | // PrevNextCache cannot use memory or array cache at the moment because the |
184 | // Code in CRM_Core_BAO_PrevNextCache assumes that this cache is sql backed. | |
185 | $container->setDefinition("cache.prevNextCache", new Definition( | |
186 | 'CRM_Utils_Cache_Interface', | |
187 | [ | |
188 | [ | |
189 | 'name' => 'CiviCRM Search PrevNextCache', | |
190 | 'type' => ['SqlGroup'], | |
191 | ], | |
192 | ] | |
193 | ))->setFactory('CRM_Utils_Cache::create'); | |
194 | ||
4ed867e0 TO |
195 | $container->setDefinition('sql_triggers', new Definition( |
196 | 'Civi\Core\SqlTriggers', | |
c64f69d9 | 197 | [] |
4ed867e0 TO |
198 | )); |
199 | ||
87e3fe24 TO |
200 | $container->setDefinition('asset_builder', new Definition( |
201 | 'Civi\Core\AssetBuilder', | |
c64f69d9 | 202 | [] |
87e3fe24 TO |
203 | )); |
204 | ||
d89d2545 TO |
205 | $container->setDefinition('themes', new Definition( |
206 | 'Civi\Core\Themes', | |
207 | [] | |
208 | )); | |
209 | ||
247eb841 | 210 | $container->setDefinition('pear_mail', new Definition('Mail')) |
a892989e | 211 | ->setFactory('CRM_Utils_Mail::createMailer'); |
247eb841 | 212 | |
7f835399 TO |
213 | if (empty(\Civi::$statics[__CLASS__]['boot'])) { |
214 | throw new \RuntimeException("Cannot initialize container. Boot services are undefined."); | |
215 | } | |
216 | foreach (\Civi::$statics[__CLASS__]['boot'] as $bootService => $def) { | |
765daeac | 217 | $container->setDefinition($bootService, new Definition())->setSynthetic(TRUE)->setPublic(TRUE); |
83617886 TO |
218 | } |
219 | ||
c8074a93 | 220 | // Expose legacy singletons as services in the container. |
c64f69d9 | 221 | $singletons = [ |
c8074a93 | 222 | 'httpClient' => 'CRM_Utils_HttpClient', |
7b5937fe | 223 | 'cache.default' => 'CRM_Utils_Cache', |
a7c57397 | 224 | 'i18n' => 'CRM_Core_I18n', |
c8074a93 TO |
225 | // Maybe? 'config' => 'CRM_Core_Config', |
226 | // Maybe? 'smarty' => 'CRM_Core_Smarty', | |
c64f69d9 | 227 | ]; |
c8074a93 TO |
228 | foreach ($singletons as $name => $class) { |
229 | $container->setDefinition($name, new Definition( | |
230 | $class | |
231 | )) | |
c64f69d9 | 232 | ->setFactory([$class, 'singleton']); |
c8074a93 | 233 | } |
90cdaa0e | 234 | $container->setAlias('cache.short', 'cache.default'); |
c8074a93 | 235 | |
223ba025 TO |
236 | $container->setDefinition('resources', new Definition( |
237 | 'CRM_Core_Resources', | |
238 | [new Reference('service_container')] | |
c64f69d9 | 239 | ))->setFactory([new Reference(self::SELF), 'createResources']); |
223ba025 | 240 | |
780fd0e3 | 241 | $container->setDefinition('prevnext', new Definition( |
99098349 TO |
242 | 'CRM_Core_PrevNextCache_Interface', |
243 | [new Reference('service_container')] | |
c64f69d9 | 244 | ))->setFactory([new Reference(self::SELF), 'createPrevNextCache']); |
99098349 TO |
245 | |
246 | $container->setDefinition('prevnext.driver.sql', new Definition( | |
780fd0e3 TO |
247 | 'CRM_Core_PrevNextCache_Sql', |
248 | [] | |
249 | )); | |
250 | ||
751f3d98 TO |
251 | $container->setDefinition('prevnext.driver.redis', new Definition( |
252 | 'CRM_Core_PrevNextCache_Redis', | |
253 | [new Reference('cache_config')] | |
254 | )); | |
255 | ||
256 | $container->setDefinition('cache_config', new Definition('ArrayObject')) | |
c64f69d9 | 257 | ->setFactory([new Reference(self::SELF), 'createCacheConfig']); |
751f3d98 | 258 | |
e7a6eae8 SL |
259 | $container->setDefinition('civi.mailing.triggers', new Definition( |
260 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
c64f69d9 CW |
261 | ['civicrm_mailing', 'Mailing'] |
262 | ))->addTag('kernel.event_listener', ['event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo']); | |
e7a6eae8 | 263 | |
e0a667ad TO |
264 | $container->setDefinition('civi.activity.triggers', new Definition( |
265 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
c64f69d9 CW |
266 | ['civicrm_activity', 'Activity'] |
267 | ))->addTag('kernel.event_listener', ['event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo']); | |
e0a667ad TO |
268 | |
269 | $container->setDefinition('civi.case.triggers', new Definition( | |
270 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
c64f69d9 CW |
271 | ['civicrm_case', 'Case'] |
272 | ))->addTag('kernel.event_listener', ['event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo']); | |
e0a667ad | 273 | |
b21ffed7 TO |
274 | $container->setDefinition('civi.case.staticTriggers', new Definition( |
275 | 'Civi\Core\SqlTrigger\StaticTriggers', | |
c64f69d9 CW |
276 | [ |
277 | [ | |
278 | [ | |
279 | 'upgrade_check' => ['table' => 'civicrm_case', 'column' => 'modified_date'], | |
b21ffed7 TO |
280 | 'table' => 'civicrm_case_activity', |
281 | 'when' => 'AFTER', | |
c64f69d9 | 282 | 'event' => ['INSERT'], |
b21ffed7 | 283 | 'sql' => "\nUPDATE civicrm_case SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.case_id;\n", |
c64f69d9 CW |
284 | ], |
285 | [ | |
286 | 'upgrade_check' => ['table' => 'civicrm_case', 'column' => 'modified_date'], | |
b21ffed7 TO |
287 | 'table' => 'civicrm_activity', |
288 | 'when' => 'BEFORE', | |
c64f69d9 | 289 | 'event' => ['UPDATE', 'DELETE'], |
b21ffed7 | 290 | 'sql' => "\nUPDATE 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);\n", |
c64f69d9 CW |
291 | ], |
292 | ], | |
293 | ] | |
b21ffed7 | 294 | )) |
c64f69d9 | 295 | ->addTag('kernel.event_listener', ['event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo']); |
b21ffed7 | 296 | |
43ceab3f TO |
297 | $container->setDefinition('civi_token_compat', new Definition( |
298 | 'Civi\Token\TokenCompatSubscriber', | |
c64f69d9 | 299 | [] |
43ceab3f | 300 | ))->addTag('kernel.event_subscriber'); |
56df2d06 TO |
301 | $container->setDefinition("crm_mailing_action_tokens", new Definition( |
302 | "CRM_Mailing_ActionTokens", | |
c64f69d9 | 303 | [] |
56df2d06 | 304 | ))->addTag('kernel.event_subscriber'); |
43ceab3f | 305 | |
c64f69d9 | 306 | foreach (['Activity', 'Contribute', 'Event', 'Mailing', 'Member'] as $comp) { |
46f5566c TO |
307 | $container->setDefinition("crm_" . strtolower($comp) . "_tokens", new Definition( |
308 | "CRM_{$comp}_Tokens", | |
c64f69d9 | 309 | [] |
46f5566c TO |
310 | ))->addTag('kernel.event_subscriber'); |
311 | } | |
50a23755 | 312 | |
caaeea39 | 313 | \CRM_Api4_Services::hook_container($container); |
aa4343bc | 314 | |
40787e18 TO |
315 | \CRM_Utils_Hook::container($container); |
316 | ||
fa184193 TO |
317 | return $container; |
318 | } | |
319 | ||
16072ce1 TO |
320 | /** |
321 | * @return \Civi\Angular\Manager | |
322 | */ | |
323 | public function createAngularManager() { | |
324 | return new \Civi\Angular\Manager(\CRM_Core_Resources::singleton()); | |
325 | } | |
326 | ||
fa184193 | 327 | /** |
34f3bbd9 | 328 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
43ceab3f | 329 | * @return \Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher |
fa184193 | 330 | */ |
40787e18 | 331 | public function createEventDispatcher($container) { |
762dc04d | 332 | $dispatcher = new CiviEventDispatcher($container); |
c64f69d9 CW |
333 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, ['\Civi\Core\InstallationCanary', 'check']); |
334 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, ['\Civi\Core\DatabaseInitializer', 'initialize']); | |
335 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, ['\Civi\Core\LocalizationInitializer', 'initialize']); | |
336 | $dispatcher->addListener('hook_civicrm_pre', ['\Civi\Core\Event\PreEvent', 'dispatchSubevent'], 100); | |
337 | $dispatcher->addListener('hook_civicrm_post', ['\Civi\Core\Event\PostEvent', 'dispatchSubevent'], 100); | |
338 | $dispatcher->addListener('hook_civicrm_post::Activity', ['\Civi\CCase\Events', 'fireCaseChange']); | |
339 | $dispatcher->addListener('hook_civicrm_post::Case', ['\Civi\CCase\Events', 'fireCaseChange']); | |
340 | $dispatcher->addListener('hook_civicrm_caseChange', ['\Civi\CCase\Events', 'delegateToXmlListeners']); | |
341 | $dispatcher->addListener('hook_civicrm_caseChange', ['\Civi\CCase\SequenceListener', 'onCaseChange_static']); | |
342 | $dispatcher->addListener('hook_civicrm_eventDefs', ['\Civi\Core\CiviEventInspector', 'findBuiltInEvents']); | |
0d0031a0 | 343 | // TODO We need a better code-convention for metadata about non-hook events. |
c64f69d9 CW |
344 | $dispatcher->addListener('hook_civicrm_eventDefs', ['\Civi\API\Events', 'hookEventDefs']); |
345 | $dispatcher->addListener('hook_civicrm_eventDefs', ['\Civi\Core\Event\SystemInstallEvent', 'hookEventDefs']); | |
346 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\Civi\Angular\Page\Modules', 'buildAngularModules']); | |
347 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\CRM_Utils_VisualBundle', 'buildAssetJs']); | |
348 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\CRM_Utils_VisualBundle', 'buildAssetCss']); | |
f22fb451 | 349 | $dispatcher->addListener('hook_civicrm_buildAsset', ['\CRM_Core_Resources', 'renderMenubarStylesheet']); |
303017a1 | 350 | $dispatcher->addListener('hook_civicrm_coreResourceList', ['\CRM_Utils_System', 'appendCoreResources']); |
62c20d1e | 351 | $dispatcher->addListener('hook_civicrm_getAssetUrl', ['\CRM_Utils_System', 'alterAssetUrl']); |
c64f69d9 CW |
352 | $dispatcher->addListener('civi.dao.postInsert', ['\CRM_Core_BAO_RecurringEntity', 'triggerInsert']); |
353 | $dispatcher->addListener('civi.dao.postUpdate', ['\CRM_Core_BAO_RecurringEntity', 'triggerUpdate']); | |
354 | $dispatcher->addListener('civi.dao.postDelete', ['\CRM_Core_BAO_RecurringEntity', 'triggerDelete']); | |
355 | $dispatcher->addListener('hook_civicrm_unhandled_exception', [ | |
9ae2d27b TO |
356 | 'CRM_Core_LegacyErrorHandler', |
357 | 'handleException', | |
c64f69d9 CW |
358 | ], -200); |
359 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Activity_ActionMapping', 'onRegisterActionMappings']); | |
360 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Contact_ActionMapping', 'onRegisterActionMappings']); | |
361 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Contribute_ActionMapping_ByPage', 'onRegisterActionMappings']); | |
362 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Contribute_ActionMapping_ByType', 'onRegisterActionMappings']); | |
363 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Event_ActionMapping', 'onRegisterActionMappings']); | |
364 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Member_ActionMapping', 'onRegisterActionMappings']); | |
46f5566c | 365 | |
fa184193 TO |
366 | return $dispatcher; |
367 | } | |
0f643fb2 | 368 | |
10760fa1 | 369 | /** |
34f3bbd9 | 370 | * @return \Civi\Core\Lock\LockManager |
10760fa1 | 371 | */ |
83617886 | 372 | public static function createLockManager() { |
10760fa1 TO |
373 | // Ideally, downstream implementers could override any definitions in |
374 | // the container. For now, we'll make-do with some define()s. | |
375 | $lm = new LockManager(); | |
376 | $lm | |
c64f69d9 CW |
377 | ->register('/^cache\./', defined('CIVICRM_CACHE_LOCK') ? CIVICRM_CACHE_LOCK : ['CRM_Core_Lock', 'createScopedLock']) |
378 | ->register('/^data\./', defined('CIVICRM_DATA_LOCK') ? CIVICRM_DATA_LOCK : ['CRM_Core_Lock', 'createScopedLock']) | |
379 | ->register('/^worker\.mailing\.send\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : ['CRM_Core_Lock', 'createCivimailLock']) | |
380 | ->register('/^worker\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : ['CRM_Core_Lock', 'createScopedLock']); | |
10760fa1 TO |
381 | |
382 | // Registrations may use complex resolver expressions, but (as a micro-optimization) | |
383 | // the default factory is specified as an array. | |
384 | ||
385 | return $lm; | |
386 | } | |
387 | ||
0f643fb2 TO |
388 | /** |
389 | * @param \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher | |
2a6da8d7 EM |
390 | * @param $magicFunctionProvider |
391 | * | |
0f643fb2 TO |
392 | * @return \Civi\API\Kernel |
393 | */ | |
c65db512 | 394 | public function createApiKernel($dispatcher, $magicFunctionProvider) { |
0a946de2 | 395 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\ChainSubscriber()); |
b55bc593 | 396 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); |
bace5cd9 | 397 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); |
c65db512 | 398 | $dispatcher->addSubscriber($magicFunctionProvider); |
d0c9daa4 | 399 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); |
dcef11bd | 400 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\APIv3SchemaAdapter()); |
c64f69d9 | 401 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\WrapperAdapter([ |
6d3bdc98 TO |
402 | \CRM_Utils_API_HTMLInputCoder::singleton(), |
403 | \CRM_Utils_API_NullOutputCoder::singleton(), | |
404 | \CRM_Utils_API_ReloadOption::singleton(), | |
405 | \CRM_Utils_API_MatchOption::singleton(), | |
c64f69d9 | 406 | ])); |
0661f62b | 407 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); |
82376c19 TO |
408 | $kernel = new \Civi\API\Kernel($dispatcher); |
409 | ||
410 | $reflectionProvider = new \Civi\API\Provider\ReflectionProvider($kernel); | |
411 | $dispatcher->addSubscriber($reflectionProvider); | |
412 | ||
56154d36 TO |
413 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\DynamicFKAuthorization( |
414 | $kernel, | |
415 | 'Attachment', | |
c64f69d9 | 416 | ['create', 'get', 'delete'], |
2e37a19f | 417 | // Given a file ID, determine the entity+table it's attached to. |
56154d36 TO |
418 | 'SELECT if(cf.id,1,0) as is_valid, cef.entity_table, cef.entity_id |
419 | FROM civicrm_file cf | |
420 | LEFT JOIN civicrm_entity_file cef ON cf.id = cef.file_id | |
421 | WHERE cf.id = %1', | |
29468114 TO |
422 | // Get a list of custom fields (field_name,table_name,extends) |
423 | 'SELECT concat("custom_",fld.id) as field_name, | |
424 | grp.table_name as table_name, | |
425 | grp.extends as extends | |
426 | FROM civicrm_custom_field fld | |
427 | INNER JOIN civicrm_custom_group grp ON fld.custom_group_id = grp.id | |
428 | WHERE fld.data_type = "File" | |
09871447 | 429 | ' |
56154d36 TO |
430 | )); |
431 | ||
c64f69d9 | 432 | $kernel->setApiProviders([ |
82376c19 TO |
433 | $reflectionProvider, |
434 | $magicFunctionProvider, | |
c64f69d9 | 435 | ]); |
82376c19 | 436 | |
0f643fb2 TO |
437 | return $kernel; |
438 | } | |
96025800 | 439 | |
223ba025 | 440 | /** |
34f3bbd9 | 441 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
223ba025 TO |
442 | * @return \CRM_Core_Resources |
443 | */ | |
444 | public static function createResources($container) { | |
445 | $sys = \CRM_Extension_System::singleton(); | |
446 | return new \CRM_Core_Resources( | |
447 | $sys->getMapper(), | |
448 | $container->get('cache.js_strings'), | |
449 | \CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode' | |
450 | ); | |
451 | } | |
452 | ||
99098349 | 453 | /** |
34f3bbd9 | 454 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
99098349 TO |
455 | * @return \CRM_Core_PrevNextCache_Interface |
456 | */ | |
457 | public static function createPrevNextCache($container) { | |
e28bf654 TO |
458 | $setting = \Civi::settings()->get('prevNextBackend'); |
459 | if ($setting === 'default') { | |
f76de01c TO |
460 | // For initial release (5.8.x), continue defaulting to SQL. |
461 | $isTransitional = version_compare(\CRM_Utils_System::version(), '5.9.alpha1', '<'); | |
e28bf654 TO |
462 | $cacheDriver = \CRM_Utils_Cache::getCacheDriver(); |
463 | $service = 'prevnext.driver.' . strtolower($cacheDriver); | |
f76de01c | 464 | return $container->has($service) && !$isTransitional |
e28bf654 TO |
465 | ? $container->get($service) |
466 | : $container->get('prevnext.driver.sql'); | |
467 | } | |
468 | else { | |
469 | return $container->get('prevnext.driver.' . $setting); | |
470 | } | |
99098349 TO |
471 | } |
472 | ||
751f3d98 TO |
473 | public static function createCacheConfig() { |
474 | $driver = \CRM_Utils_Cache::getCacheDriver(); | |
475 | $settings = \CRM_Utils_Cache::getCacheSettings($driver); | |
476 | $settings['driver'] = $driver; | |
477 | return new \ArrayObject($settings); | |
478 | } | |
479 | ||
83617886 TO |
480 | /** |
481 | * Get a list of boot services. | |
482 | * | |
483 | * These are services which must be setup *before* the container can operate. | |
484 | * | |
7f835399 | 485 | * @param bool $loadFromDB |
83617886 TO |
486 | * @throws \CRM_Core_Exception |
487 | */ | |
7f835399 | 488 | public static function boot($loadFromDB) { |
56eafc21 | 489 | // Array(string $serviceId => object $serviceInstance). |
c64f69d9 | 490 | $bootServices = []; |
7f835399 | 491 | \Civi::$statics[__CLASS__]['boot'] = &$bootServices; |
d4330c62 | 492 | |
56eafc21 | 493 | $bootServices['runtime'] = $runtime = new \CRM_Core_Config_Runtime(); |
7f835399 | 494 | $runtime->initialize($loadFromDB); |
d4330c62 | 495 | |
b1db062e H |
496 | $bootServices['paths'] = new \Civi\Core\Paths(); |
497 | ||
7f835399 | 498 | $class = $runtime->userFrameworkClass; |
56eafc21 | 499 | $bootServices['userSystem'] = $userSystem = new $class(); |
7f835399 TO |
500 | $userSystem->initialize(); |
501 | ||
502 | $userPermissionClass = 'CRM_Core_Permission_' . $runtime->userFramework; | |
56eafc21 | 503 | $bootServices['userPermissionClass'] = new $userPermissionClass(); |
7f835399 | 504 | |
c64f69d9 | 505 | $bootServices['cache.settings'] = \CRM_Utils_Cache::create([ |
56eafc21 | 506 | 'name' => 'settings', |
c64f69d9 CW |
507 | 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], |
508 | ]); | |
7f835399 | 509 | |
56eafc21 | 510 | $bootServices['settings_manager'] = new \Civi\Core\SettingsManager($bootServices['cache.settings']); |
7f835399 | 511 | |
56eafc21 | 512 | $bootServices['lockManager'] = self::createLockManager(); |
7f835399 TO |
513 | |
514 | if ($loadFromDB && $runtime->dsn) { | |
3a036b15 | 515 | \CRM_Core_DAO::init($runtime->dsn); |
edbcbd96 | 516 | \CRM_Utils_Hook::singleton(TRUE); |
7f835399 | 517 | \CRM_Extension_System::singleton(TRUE); |
4025c773 | 518 | \CRM_Extension_System::singleton(TRUE)->getClassLoader()->register(); |
7f835399 | 519 | |
1b81ed50 | 520 | $runtime->includeCustomPath(); |
521 | ||
7f835399 | 522 | $c = new self(); |
56eafc21 TO |
523 | $container = $c->loadContainer(); |
524 | foreach ($bootServices as $name => $obj) { | |
525 | $container->set($name, $obj); | |
526 | } | |
527 | \Civi::$statics[__CLASS__]['container'] = $container; | |
83617886 | 528 | } |
83617886 TO |
529 | } |
530 | ||
531 | public static function getBootService($name) { | |
56eafc21 | 532 | return \Civi::$statics[__CLASS__]['boot'][$name]; |
83617886 TO |
533 | } |
534 | ||
4d8e83b6 TO |
535 | /** |
536 | * Determine whether the container services are available. | |
537 | * | |
538 | * @return bool | |
539 | */ | |
540 | public static function isContainerBooted() { | |
541 | return isset(\Civi::$statics[__CLASS__]['container']); | |
542 | } | |
543 | ||
fa184193 | 544 | } |