Commit | Line | Data |
---|---|---|
fa184193 TO |
1 | <?php |
2 | namespace Civi\Core; | |
46bcf597 | 3 | |
8bcc0d86 | 4 | use Civi\API\Provider\ActionObjectProvider; |
0085db83 | 5 | use Civi\Core\Event\SystemInstallEvent; |
10760fa1 | 6 | use Civi\Core\Lock\LockManager; |
fa184193 TO |
7 | use Doctrine\Common\Annotations\AnnotationReader; |
8 | use Doctrine\Common\Annotations\AnnotationRegistry; | |
9 | use Doctrine\Common\Annotations\FileCacheReader; | |
10 | use Doctrine\Common\Cache\FilesystemCache; | |
11 | use Doctrine\ORM\EntityManager; | |
12 | use Doctrine\ORM\Mapping\Driver\AnnotationDriver; | |
13 | use Doctrine\ORM\Tools\Setup; | |
40787e18 | 14 | use Symfony\Component\Config\ConfigCache; |
fa184193 | 15 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
c8074a93 | 16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
fa184193 | 17 | use Symfony\Component\DependencyInjection\Definition; |
40787e18 | 18 | use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
fa184193 | 19 | use Symfony\Component\DependencyInjection\Reference; |
40787e18 TO |
20 | use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; |
21 | use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; | |
fa184193 TO |
22 | |
23 | // TODO use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | |
24 | ||
6550386a EM |
25 | /** |
26 | * Class Container | |
27 | * @package Civi\Core | |
28 | */ | |
fa184193 TO |
29 | class Container { |
30 | ||
31 | const SELF = 'civi_container_factory'; | |
32 | ||
fa184193 | 33 | /** |
04855556 TO |
34 | * @param bool $reset |
35 | * Whether to forcibly rebuild the entire container. | |
fa184193 TO |
36 | * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface |
37 | */ | |
378e2654 | 38 | public static function singleton($reset = FALSE) { |
7f835399 TO |
39 | if ($reset || !isset(\Civi::$statics[__CLASS__]['container'])) { |
40 | self::boot(TRUE); | |
fa184193 | 41 | } |
7f835399 | 42 | return \Civi::$statics[__CLASS__]['container']; |
fa184193 TO |
43 | } |
44 | ||
45 | /** | |
40787e18 TO |
46 | * Find a cached container definition or construct a new one. |
47 | * | |
48 | * There are many weird contexts in which Civi initializes (eg different | |
49 | * variations of multitenancy and different permutations of CMS/CRM bootstrap), | |
50 | * and hook_container may fire a bit differently in each context. To mitigate | |
51 | * risk of leaks between environments, we compute a unique envID | |
52 | * (md5(DB_NAME, HTTP_HOST, SCRIPT_FILENAME, etc)) and use separate caches for | |
53 | * each (eg "templates_c/CachedCiviContainer.$ENVID.php"). | |
54 | * | |
55 | * Constants: | |
56 | * - CIVICRM_CONTAINER_CACHE -- 'always' [default], 'never', 'auto' | |
57 | * - CIVICRM_DSN | |
58 | * - CIVICRM_DOMAIN_ID | |
59 | * - CIVICRM_TEMPLATE_COMPILEDIR | |
60 | * | |
61 | * @return ContainerInterface | |
62 | */ | |
63 | public function loadContainer() { | |
64 | // Note: The container's raison d'etre is to manage construction of other | |
65 | // services. Consequently, we assume a minimal service available -- the classloader | |
66 | // has been setup, and civicrm.settings.php is loaded, but nothing else works. | |
67 | ||
5497e016 | 68 | $cacheMode = defined('CIVICRM_CONTAINER_CACHE') ? CIVICRM_CONTAINER_CACHE : 'auto'; |
40787e18 TO |
69 | |
70 | // In pre-installation environments, don't bother with caching. | |
71 | if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') || !defined('CIVICRM_DSN') || $cacheMode === 'never' || \CRM_Utils_System::isInUpgradeMode()) { | |
12e01332 TO |
72 | $containerBuilder = $this->createContainer(); |
73 | $containerBuilder->compile(); | |
74 | return $containerBuilder; | |
40787e18 TO |
75 | } |
76 | ||
83617886 | 77 | $envId = \CRM_Core_Config_Runtime::getId(); |
40787e18 TO |
78 | $file = CIVICRM_TEMPLATE_COMPILEDIR . "/CachedCiviContainer.{$envId}.php"; |
79 | $containerConfigCache = new ConfigCache($file, $cacheMode === 'auto'); | |
40787e18 TO |
80 | if (!$containerConfigCache->isFresh()) { |
81 | $containerBuilder = $this->createContainer(); | |
82 | $containerBuilder->compile(); | |
83 | $dumper = new PhpDumper($containerBuilder); | |
84 | $containerConfigCache->write( | |
85 | $dumper->dump(array('class' => 'CachedCiviContainer')), | |
86 | $containerBuilder->getResources() | |
87 | ); | |
88 | } | |
89 | ||
90 | require_once $file; | |
91 | $c = new \CachedCiviContainer(); | |
40787e18 TO |
92 | return $c; |
93 | } | |
94 | ||
95 | /** | |
96 | * Construct a new container. | |
97 | * | |
fa184193 | 98 | * @var ContainerBuilder |
77b97be7 | 99 | * @return \Symfony\Component\DependencyInjection\ContainerBuilder |
fa184193 TO |
100 | */ |
101 | public function createContainer() { | |
102 | $civicrm_base_path = dirname(dirname(__DIR__)); | |
103 | $container = new ContainerBuilder(); | |
40787e18 TO |
104 | $container->addCompilerPass(new RegisterListenersPass('dispatcher')); |
105 | $container->addObjectResource($this); | |
fa184193 | 106 | $container->setParameter('civicrm_base_path', $civicrm_base_path); |
40787e18 | 107 | //$container->set(self::SELF, $this); |
762dc04d TO |
108 | |
109 | $container->addResource(new \Symfony\Component\Config\Resource\FileResource(__FILE__)); | |
110 | ||
40787e18 TO |
111 | $container->setDefinition(self::SELF, new Definition( |
112 | 'Civi\Core\Container', | |
113 | array() | |
114 | )); | |
fa184193 | 115 | |
505d8b83 TO |
116 | // TODO Move configuration to an external file; define caching structure |
117 | // if (empty($configDirectories)) { | |
118 | // throw new \Exception(__CLASS__ . ': Missing required properties (civicrmRoot, configDirectories)'); | |
119 | // } | |
120 | // $locator = new FileLocator($configDirectories); | |
121 | // $loaderResolver = new LoaderResolver(array( | |
122 | // new YamlFileLoader($container, $locator) | |
123 | // )); | |
124 | // $delegatingLoader = new DelegatingLoader($loaderResolver); | |
125 | // foreach (array('services.yml') as $file) { | |
126 | // $yamlUserFiles = $locator->locate($file, NULL, FALSE); | |
127 | // foreach ($yamlUserFiles as $file) { | |
128 | // $delegatingLoader->load($file); | |
129 | // } | |
130 | // } | |
fa184193 | 131 | |
16072ce1 | 132 | $container->setDefinition('angular', new Definition( |
40787e18 | 133 | 'Civi\Angular\Manager', |
16072ce1 TO |
134 | array() |
135 | )) | |
d15f7e1c | 136 | ->setFactory(array(new Reference(self::SELF), 'createAngularManager')); |
16072ce1 | 137 | |
fa184193 | 138 | $container->setDefinition('dispatcher', new Definition( |
762dc04d | 139 | 'Civi\Core\CiviEventDispatcher', |
40787e18 | 140 | array(new Reference('service_container')) |
fa184193 | 141 | )) |
d15f7e1c | 142 | ->setFactory(array(new Reference(self::SELF), 'createEventDispatcher')); |
fa184193 | 143 | |
c65db512 | 144 | $container->setDefinition('magic_function_provider', new Definition( |
40787e18 | 145 | 'Civi\API\Provider\MagicFunctionProvider', |
c65db512 TO |
146 | array() |
147 | )); | |
148 | ||
0f643fb2 | 149 | $container->setDefinition('civi_api_kernel', new Definition( |
40787e18 | 150 | 'Civi\API\Kernel', |
c65db512 | 151 | array(new Reference('dispatcher'), new Reference('magic_function_provider')) |
0f643fb2 | 152 | )) |
d15f7e1c | 153 | ->setFactory(array(new Reference(self::SELF), 'createApiKernel')); |
0f643fb2 | 154 | |
7b4bbb34 | 155 | $container->setDefinition('cxn_reg_client', new Definition( |
40787e18 | 156 | 'Civi\Cxn\Rpc\RegistrationClient', |
7b4bbb34 TO |
157 | array() |
158 | )) | |
5eda8e9b | 159 | ->setFactory('CRM_Cxn_BAO_Cxn::createRegistrationClient'); |
7b4bbb34 | 160 | |
6e5ad5ee TO |
161 | $container->setDefinition('psr_log', new Definition('CRM_Core_Error_Log', array())); |
162 | ||
7a19d718 TO |
163 | $basicCaches = array( |
164 | 'js_strings' => 'js_strings', | |
165 | 'community_messages' => 'community_messages', | |
b1fc1ab0 | 166 | 'checks' => 'checks', |
19707a63 | 167 | 'session' => 'CiviCRM Session', |
90cdaa0e | 168 | 'long' => 'long', |
7a19d718 TO |
169 | ); |
170 | foreach ($basicCaches as $cacheSvc => $cacheGrp) { | |
171 | $container->setDefinition("cache.{$cacheSvc}", new Definition( | |
a4704404 TO |
172 | 'CRM_Utils_Cache_Interface', |
173 | array( | |
174 | array( | |
7a19d718 | 175 | 'name' => $cacheGrp, |
a944a143 | 176 | 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'), |
a4704404 TO |
177 | ), |
178 | ) | |
a892989e | 179 | ))->setFactory('CRM_Utils_Cache::create'); |
a4704404 | 180 | } |
3a84c0ab | 181 | |
4ed867e0 TO |
182 | $container->setDefinition('sql_triggers', new Definition( |
183 | 'Civi\Core\SqlTriggers', | |
184 | array() | |
185 | )); | |
186 | ||
87e3fe24 TO |
187 | $container->setDefinition('asset_builder', new Definition( |
188 | 'Civi\Core\AssetBuilder', | |
189 | array() | |
190 | )); | |
191 | ||
247eb841 | 192 | $container->setDefinition('pear_mail', new Definition('Mail')) |
a892989e | 193 | ->setFactory('CRM_Utils_Mail::createMailer'); |
247eb841 | 194 | |
7f835399 TO |
195 | if (empty(\Civi::$statics[__CLASS__]['boot'])) { |
196 | throw new \RuntimeException("Cannot initialize container. Boot services are undefined."); | |
197 | } | |
198 | foreach (\Civi::$statics[__CLASS__]['boot'] as $bootService => $def) { | |
56eafc21 | 199 | $container->setDefinition($bootService, new Definition())->setSynthetic(TRUE); |
83617886 TO |
200 | } |
201 | ||
c8074a93 TO |
202 | // Expose legacy singletons as services in the container. |
203 | $singletons = array( | |
c8074a93 | 204 | 'httpClient' => 'CRM_Utils_HttpClient', |
7b5937fe | 205 | 'cache.default' => 'CRM_Utils_Cache', |
a7c57397 | 206 | 'i18n' => 'CRM_Core_I18n', |
c8074a93 TO |
207 | // Maybe? 'config' => 'CRM_Core_Config', |
208 | // Maybe? 'smarty' => 'CRM_Core_Smarty', | |
209 | ); | |
210 | foreach ($singletons as $name => $class) { | |
211 | $container->setDefinition($name, new Definition( | |
212 | $class | |
213 | )) | |
d15f7e1c | 214 | ->setFactory(array($class, 'singleton')); |
c8074a93 | 215 | } |
90cdaa0e | 216 | $container->setAlias('cache.short', 'cache.default'); |
c8074a93 | 217 | |
223ba025 TO |
218 | $container->setDefinition('resources', new Definition( |
219 | 'CRM_Core_Resources', | |
220 | [new Reference('service_container')] | |
221 | ))->setFactory(array(new Reference(self::SELF), 'createResources')); | |
222 | ||
780fd0e3 | 223 | $container->setDefinition('prevnext', new Definition( |
99098349 TO |
224 | 'CRM_Core_PrevNextCache_Interface', |
225 | [new Reference('service_container')] | |
226 | ))->setFactory(array(new Reference(self::SELF), 'createPrevNextCache')); | |
227 | ||
228 | $container->setDefinition('prevnext.driver.sql', new Definition( | |
780fd0e3 TO |
229 | 'CRM_Core_PrevNextCache_Sql', |
230 | [] | |
231 | )); | |
232 | ||
751f3d98 TO |
233 | $container->setDefinition('prevnext.driver.redis', new Definition( |
234 | 'CRM_Core_PrevNextCache_Redis', | |
235 | [new Reference('cache_config')] | |
236 | )); | |
237 | ||
238 | $container->setDefinition('cache_config', new Definition('ArrayObject')) | |
239 | ->setFactory(array(new Reference(self::SELF), 'createCacheConfig')); | |
240 | ||
e7a6eae8 SL |
241 | $container->setDefinition('civi.mailing.triggers', new Definition( |
242 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
243 | array('civicrm_mailing', 'Mailing') | |
244 | ))->addTag('kernel.event_listener', array('event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo')); | |
245 | ||
e0a667ad TO |
246 | $container->setDefinition('civi.activity.triggers', new Definition( |
247 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
248 | array('civicrm_activity', 'Activity') | |
249 | ))->addTag('kernel.event_listener', array('event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo')); | |
250 | ||
251 | $container->setDefinition('civi.case.triggers', new Definition( | |
252 | 'Civi\Core\SqlTrigger\TimestampTriggers', | |
253 | array('civicrm_case', 'Case') | |
254 | ))->addTag('kernel.event_listener', array('event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo')); | |
255 | ||
b21ffed7 TO |
256 | $container->setDefinition('civi.case.staticTriggers', new Definition( |
257 | 'Civi\Core\SqlTrigger\StaticTriggers', | |
258 | array( | |
259 | array( | |
260 | array( | |
261 | 'upgrade_check' => array('table' => 'civicrm_case', 'column' => 'modified_date'), | |
262 | 'table' => 'civicrm_case_activity', | |
263 | 'when' => 'AFTER', | |
264 | 'event' => array('INSERT'), | |
265 | 'sql' => "\nUPDATE civicrm_case SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.case_id;\n", | |
266 | ), | |
267 | array( | |
268 | 'upgrade_check' => array('table' => 'civicrm_case', 'column' => 'modified_date'), | |
269 | 'table' => 'civicrm_activity', | |
270 | 'when' => 'BEFORE', | |
271 | 'event' => array('UPDATE', 'DELETE'), | |
272 | '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", | |
273 | ), | |
274 | ), | |
275 | ) | |
276 | )) | |
277 | ->addTag('kernel.event_listener', array('event' => 'hook_civicrm_triggerInfo', 'method' => 'onTriggerInfo')); | |
278 | ||
43ceab3f TO |
279 | $container->setDefinition('civi_token_compat', new Definition( |
280 | 'Civi\Token\TokenCompatSubscriber', | |
281 | array() | |
282 | ))->addTag('kernel.event_subscriber'); | |
56df2d06 TO |
283 | $container->setDefinition("crm_mailing_action_tokens", new Definition( |
284 | "CRM_Mailing_ActionTokens", | |
285 | array() | |
286 | ))->addTag('kernel.event_subscriber'); | |
43ceab3f | 287 | |
56df2d06 | 288 | foreach (array('Activity', 'Contribute', 'Event', 'Mailing', 'Member') as $comp) { |
46f5566c TO |
289 | $container->setDefinition("crm_" . strtolower($comp) . "_tokens", new Definition( |
290 | "CRM_{$comp}_Tokens", | |
291 | array() | |
292 | ))->addTag('kernel.event_subscriber'); | |
293 | } | |
50a23755 | 294 | |
aa4343bc TO |
295 | if (\CRM_Utils_Constant::value('CIVICRM_FLEXMAILER_HACK_SERVICES')) { |
296 | \Civi\Core\Resolver::singleton()->call(CIVICRM_FLEXMAILER_HACK_SERVICES, array($container)); | |
297 | } | |
298 | ||
40787e18 TO |
299 | \CRM_Utils_Hook::container($container); |
300 | ||
fa184193 TO |
301 | return $container; |
302 | } | |
303 | ||
16072ce1 TO |
304 | /** |
305 | * @return \Civi\Angular\Manager | |
306 | */ | |
307 | public function createAngularManager() { | |
308 | return new \Civi\Angular\Manager(\CRM_Core_Resources::singleton()); | |
309 | } | |
310 | ||
fa184193 | 311 | /** |
40787e18 | 312 | * @param ContainerInterface $container |
43ceab3f | 313 | * @return \Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher |
fa184193 | 314 | */ |
40787e18 | 315 | public function createEventDispatcher($container) { |
762dc04d | 316 | $dispatcher = new CiviEventDispatcher($container); |
0085db83 | 317 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); |
40d5632a | 318 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); |
ece6501c | 319 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\LocalizationInitializer', 'initialize')); |
c73e3098 TO |
320 | $dispatcher->addListener('hook_civicrm_pre', array('\Civi\Core\Event\PreEvent', 'dispatchSubevent'), 100); |
321 | $dispatcher->addListener('hook_civicrm_post', array('\Civi\Core\Event\PostEvent', 'dispatchSubevent'), 100); | |
708d8fa2 | 322 | $dispatcher->addListener('hook_civicrm_post::Activity', array('\Civi\CCase\Events', 'fireCaseChange')); |
753657ed | 323 | $dispatcher->addListener('hook_civicrm_post::Case', array('\Civi\CCase\Events', 'fireCaseChange')); |
708d8fa2 | 324 | $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\Events', 'delegateToXmlListeners')); |
b019b130 | 325 | $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\SequenceListener', 'onCaseChange_static')); |
47e7c2f8 | 326 | $dispatcher->addListener('hook_civicrm_eventDefs', array('\Civi\Core\CiviEventInspector', 'findBuiltInEvents')); |
0d0031a0 | 327 | // TODO We need a better code-convention for metadata about non-hook events. |
47e7c2f8 TO |
328 | $dispatcher->addListener('hook_civicrm_eventDefs', array('\Civi\API\Events', 'hookEventDefs')); |
329 | $dispatcher->addListener('hook_civicrm_eventDefs', array('\Civi\Core\Event\SystemInstallEvent', 'hookEventDefs')); | |
466e4b29 | 330 | $dispatcher->addListener('hook_civicrm_buildAsset', array('\Civi\Angular\Page\Modules', 'buildAngularModules')); |
1b6476e6 TO |
331 | $dispatcher->addListener('hook_civicrm_buildAsset', array('\CRM_Utils_VisualBundle', 'buildAssetJs')); |
332 | $dispatcher->addListener('hook_civicrm_buildAsset', array('\CRM_Utils_VisualBundle', 'buildAssetCss')); | |
94075464 TO |
333 | $dispatcher->addListener('civi.dao.postInsert', array('\CRM_Core_BAO_RecurringEntity', 'triggerInsert')); |
334 | $dispatcher->addListener('civi.dao.postUpdate', array('\CRM_Core_BAO_RecurringEntity', 'triggerUpdate')); | |
335 | $dispatcher->addListener('civi.dao.postDelete', array('\CRM_Core_BAO_RecurringEntity', 'triggerDelete')); | |
46bcf597 | 336 | $dispatcher->addListener('hook_civicrm_unhandled_exception', array( |
9ae2d27b TO |
337 | 'CRM_Core_LegacyErrorHandler', |
338 | 'handleException', | |
c73e3098 | 339 | ), -200); |
46f5566c TO |
340 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Activity_ActionMapping', 'onRegisterActionMappings')); |
341 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contact_ActionMapping', 'onRegisterActionMappings')); | |
2045389a | 342 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contribute_ActionMapping_ByPage', 'onRegisterActionMappings')); |
b5302d4e | 343 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contribute_ActionMapping_ByType', 'onRegisterActionMappings')); |
46f5566c TO |
344 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Event_ActionMapping', 'onRegisterActionMappings')); |
345 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Member_ActionMapping', 'onRegisterActionMappings')); | |
346 | ||
aa4343bc TO |
347 | if (\CRM_Utils_Constant::value('CIVICRM_FLEXMAILER_HACK_LISTENERS')) { |
348 | \Civi\Core\Resolver::singleton()->call(CIVICRM_FLEXMAILER_HACK_LISTENERS, array($dispatcher)); | |
349 | } | |
350 | ||
fa184193 TO |
351 | return $dispatcher; |
352 | } | |
0f643fb2 | 353 | |
10760fa1 TO |
354 | /** |
355 | * @return LockManager | |
356 | */ | |
83617886 | 357 | public static function createLockManager() { |
10760fa1 TO |
358 | // Ideally, downstream implementers could override any definitions in |
359 | // the container. For now, we'll make-do with some define()s. | |
360 | $lm = new LockManager(); | |
361 | $lm | |
362 | ->register('/^cache\./', defined('CIVICRM_CACHE_LOCK') ? CIVICRM_CACHE_LOCK : array('CRM_Core_Lock', 'createScopedLock')) | |
363 | ->register('/^data\./', defined('CIVICRM_DATA_LOCK') ? CIVICRM_DATA_LOCK : array('CRM_Core_Lock', 'createScopedLock')) | |
364 | ->register('/^worker\.mailing\.send\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : array('CRM_Core_Lock', 'createCivimailLock')) | |
365 | ->register('/^worker\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : array('CRM_Core_Lock', 'createScopedLock')); | |
366 | ||
367 | // Registrations may use complex resolver expressions, but (as a micro-optimization) | |
368 | // the default factory is specified as an array. | |
369 | ||
370 | return $lm; | |
371 | } | |
372 | ||
0f643fb2 TO |
373 | /** |
374 | * @param \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher | |
2a6da8d7 EM |
375 | * @param $magicFunctionProvider |
376 | * | |
0f643fb2 TO |
377 | * @return \Civi\API\Kernel |
378 | */ | |
c65db512 | 379 | public function createApiKernel($dispatcher, $magicFunctionProvider) { |
0a946de2 | 380 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\ChainSubscriber()); |
b55bc593 | 381 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); |
bace5cd9 | 382 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); |
c65db512 | 383 | $dispatcher->addSubscriber($magicFunctionProvider); |
d0c9daa4 | 384 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); |
dcef11bd | 385 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\APIv3SchemaAdapter()); |
6d3bdc98 TO |
386 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\WrapperAdapter(array( |
387 | \CRM_Utils_API_HTMLInputCoder::singleton(), | |
388 | \CRM_Utils_API_NullOutputCoder::singleton(), | |
389 | \CRM_Utils_API_ReloadOption::singleton(), | |
390 | \CRM_Utils_API_MatchOption::singleton(), | |
391 | ))); | |
0661f62b | 392 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); |
82376c19 TO |
393 | $kernel = new \Civi\API\Kernel($dispatcher); |
394 | ||
395 | $reflectionProvider = new \Civi\API\Provider\ReflectionProvider($kernel); | |
396 | $dispatcher->addSubscriber($reflectionProvider); | |
397 | ||
56154d36 TO |
398 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\DynamicFKAuthorization( |
399 | $kernel, | |
400 | 'Attachment', | |
401 | array('create', 'get', 'delete'), | |
2e37a19f | 402 | // Given a file ID, determine the entity+table it's attached to. |
56154d36 TO |
403 | 'SELECT if(cf.id,1,0) as is_valid, cef.entity_table, cef.entity_id |
404 | FROM civicrm_file cf | |
405 | LEFT JOIN civicrm_entity_file cef ON cf.id = cef.file_id | |
406 | WHERE cf.id = %1', | |
29468114 TO |
407 | // Get a list of custom fields (field_name,table_name,extends) |
408 | 'SELECT concat("custom_",fld.id) as field_name, | |
409 | grp.table_name as table_name, | |
410 | grp.extends as extends | |
411 | FROM civicrm_custom_field fld | |
412 | INNER JOIN civicrm_custom_group grp ON fld.custom_group_id = grp.id | |
413 | WHERE fld.data_type = "File" | |
414 | ', | |
e3e66815 | 415 | array('civicrm_activity', 'civicrm_mailing', 'civicrm_contact', 'civicrm_grant') |
56154d36 TO |
416 | )); |
417 | ||
82376c19 TO |
418 | $kernel->setApiProviders(array( |
419 | $reflectionProvider, | |
420 | $magicFunctionProvider, | |
421 | )); | |
422 | ||
0f643fb2 TO |
423 | return $kernel; |
424 | } | |
96025800 | 425 | |
223ba025 TO |
426 | /** |
427 | * @param ContainerInterface $container | |
428 | * @return \CRM_Core_Resources | |
429 | */ | |
430 | public static function createResources($container) { | |
431 | $sys = \CRM_Extension_System::singleton(); | |
432 | return new \CRM_Core_Resources( | |
433 | $sys->getMapper(), | |
434 | $container->get('cache.js_strings'), | |
435 | \CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode' | |
436 | ); | |
437 | } | |
438 | ||
99098349 TO |
439 | /** |
440 | * @param ContainerInterface $container | |
441 | * @return \CRM_Core_PrevNextCache_Interface | |
442 | */ | |
443 | public static function createPrevNextCache($container) { | |
e28bf654 TO |
444 | $setting = \Civi::settings()->get('prevNextBackend'); |
445 | if ($setting === 'default') { | |
f76de01c TO |
446 | // For initial release (5.8.x), continue defaulting to SQL. |
447 | $isTransitional = version_compare(\CRM_Utils_System::version(), '5.9.alpha1', '<'); | |
e28bf654 TO |
448 | $cacheDriver = \CRM_Utils_Cache::getCacheDriver(); |
449 | $service = 'prevnext.driver.' . strtolower($cacheDriver); | |
f76de01c | 450 | return $container->has($service) && !$isTransitional |
e28bf654 TO |
451 | ? $container->get($service) |
452 | : $container->get('prevnext.driver.sql'); | |
453 | } | |
454 | else { | |
455 | return $container->get('prevnext.driver.' . $setting); | |
456 | } | |
99098349 TO |
457 | } |
458 | ||
751f3d98 TO |
459 | public static function createCacheConfig() { |
460 | $driver = \CRM_Utils_Cache::getCacheDriver(); | |
461 | $settings = \CRM_Utils_Cache::getCacheSettings($driver); | |
462 | $settings['driver'] = $driver; | |
463 | return new \ArrayObject($settings); | |
464 | } | |
465 | ||
83617886 TO |
466 | /** |
467 | * Get a list of boot services. | |
468 | * | |
469 | * These are services which must be setup *before* the container can operate. | |
470 | * | |
7f835399 | 471 | * @param bool $loadFromDB |
83617886 TO |
472 | * @throws \CRM_Core_Exception |
473 | */ | |
7f835399 | 474 | public static function boot($loadFromDB) { |
56eafc21 | 475 | // Array(string $serviceId => object $serviceInstance). |
7f835399 TO |
476 | $bootServices = array(); |
477 | \Civi::$statics[__CLASS__]['boot'] = &$bootServices; | |
d4330c62 | 478 | |
56eafc21 | 479 | $bootServices['runtime'] = $runtime = new \CRM_Core_Config_Runtime(); |
7f835399 | 480 | $runtime->initialize($loadFromDB); |
d4330c62 | 481 | |
56eafc21 | 482 | $bootServices['paths'] = new \Civi\Core\Paths(); |
d4330c62 | 483 | |
7f835399 | 484 | $class = $runtime->userFrameworkClass; |
56eafc21 | 485 | $bootServices['userSystem'] = $userSystem = new $class(); |
7f835399 TO |
486 | $userSystem->initialize(); |
487 | ||
488 | $userPermissionClass = 'CRM_Core_Permission_' . $runtime->userFramework; | |
56eafc21 | 489 | $bootServices['userPermissionClass'] = new $userPermissionClass(); |
7f835399 | 490 | |
56eafc21 TO |
491 | $bootServices['cache.settings'] = \CRM_Utils_Cache::create(array( |
492 | 'name' => 'settings', | |
493 | 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'), | |
494 | )); | |
7f835399 | 495 | |
56eafc21 | 496 | $bootServices['settings_manager'] = new \Civi\Core\SettingsManager($bootServices['cache.settings']); |
7f835399 | 497 | |
56eafc21 | 498 | $bootServices['lockManager'] = self::createLockManager(); |
7f835399 TO |
499 | |
500 | if ($loadFromDB && $runtime->dsn) { | |
3a036b15 | 501 | \CRM_Core_DAO::init($runtime->dsn); |
edbcbd96 | 502 | \CRM_Utils_Hook::singleton(TRUE); |
7f835399 | 503 | \CRM_Extension_System::singleton(TRUE); |
4025c773 | 504 | \CRM_Extension_System::singleton(TRUE)->getClassLoader()->register(); |
7f835399 | 505 | |
1b81ed50 | 506 | $runtime->includeCustomPath(); |
507 | ||
7f835399 | 508 | $c = new self(); |
56eafc21 TO |
509 | $container = $c->loadContainer(); |
510 | foreach ($bootServices as $name => $obj) { | |
511 | $container->set($name, $obj); | |
512 | } | |
513 | \Civi::$statics[__CLASS__]['container'] = $container; | |
83617886 | 514 | } |
83617886 TO |
515 | } |
516 | ||
517 | public static function getBootService($name) { | |
56eafc21 | 518 | return \Civi::$statics[__CLASS__]['boot'][$name]; |
83617886 TO |
519 | } |
520 | ||
4d8e83b6 TO |
521 | /** |
522 | * Determine whether the container services are available. | |
523 | * | |
524 | * @return bool | |
525 | */ | |
526 | public static function isContainerBooted() { | |
527 | return isset(\Civi::$statics[__CLASS__]['container']); | |
528 | } | |
529 | ||
fa184193 | 530 | } |