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 | ||
68 | $cacheMode = defined('CIVICRM_CONTAINER_CACHE') ? CIVICRM_CONTAINER_CACHE : 'always'; | |
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()) { | |
72 | return $this->createContainer(); | |
73 | } | |
74 | ||
83617886 | 75 | $envId = \CRM_Core_Config_Runtime::getId(); |
40787e18 TO |
76 | $file = CIVICRM_TEMPLATE_COMPILEDIR . "/CachedCiviContainer.{$envId}.php"; |
77 | $containerConfigCache = new ConfigCache($file, $cacheMode === 'auto'); | |
40787e18 TO |
78 | if (!$containerConfigCache->isFresh()) { |
79 | $containerBuilder = $this->createContainer(); | |
80 | $containerBuilder->compile(); | |
81 | $dumper = new PhpDumper($containerBuilder); | |
82 | $containerConfigCache->write( | |
83 | $dumper->dump(array('class' => 'CachedCiviContainer')), | |
84 | $containerBuilder->getResources() | |
85 | ); | |
86 | } | |
87 | ||
88 | require_once $file; | |
89 | $c = new \CachedCiviContainer(); | |
90 | $c->set('service_container', $c); | |
91 | return $c; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Construct a new container. | |
96 | * | |
fa184193 | 97 | * @var ContainerBuilder |
77b97be7 | 98 | * @return \Symfony\Component\DependencyInjection\ContainerBuilder |
fa184193 TO |
99 | */ |
100 | public function createContainer() { | |
101 | $civicrm_base_path = dirname(dirname(__DIR__)); | |
102 | $container = new ContainerBuilder(); | |
40787e18 TO |
103 | $container->addCompilerPass(new RegisterListenersPass('dispatcher')); |
104 | $container->addObjectResource($this); | |
fa184193 | 105 | $container->setParameter('civicrm_base_path', $civicrm_base_path); |
40787e18 TO |
106 | //$container->set(self::SELF, $this); |
107 | $container->setDefinition(self::SELF, new Definition( | |
108 | 'Civi\Core\Container', | |
109 | array() | |
110 | )); | |
fa184193 | 111 | |
505d8b83 TO |
112 | // TODO Move configuration to an external file; define caching structure |
113 | // if (empty($configDirectories)) { | |
114 | // throw new \Exception(__CLASS__ . ': Missing required properties (civicrmRoot, configDirectories)'); | |
115 | // } | |
116 | // $locator = new FileLocator($configDirectories); | |
117 | // $loaderResolver = new LoaderResolver(array( | |
118 | // new YamlFileLoader($container, $locator) | |
119 | // )); | |
120 | // $delegatingLoader = new DelegatingLoader($loaderResolver); | |
121 | // foreach (array('services.yml') as $file) { | |
122 | // $yamlUserFiles = $locator->locate($file, NULL, FALSE); | |
123 | // foreach ($yamlUserFiles as $file) { | |
124 | // $delegatingLoader->load($file); | |
125 | // } | |
126 | // } | |
fa184193 | 127 | |
16072ce1 | 128 | $container->setDefinition('angular', new Definition( |
40787e18 | 129 | 'Civi\Angular\Manager', |
16072ce1 TO |
130 | array() |
131 | )) | |
132 | ->setFactoryService(self::SELF)->setFactoryMethod('createAngularManager'); | |
133 | ||
fa184193 | 134 | $container->setDefinition('dispatcher', new Definition( |
40787e18 TO |
135 | 'Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher', |
136 | array(new Reference('service_container')) | |
fa184193 TO |
137 | )) |
138 | ->setFactoryService(self::SELF)->setFactoryMethod('createEventDispatcher'); | |
139 | ||
c65db512 | 140 | $container->setDefinition('magic_function_provider', new Definition( |
40787e18 | 141 | 'Civi\API\Provider\MagicFunctionProvider', |
c65db512 TO |
142 | array() |
143 | )); | |
144 | ||
0f643fb2 | 145 | $container->setDefinition('civi_api_kernel', new Definition( |
40787e18 | 146 | 'Civi\API\Kernel', |
c65db512 | 147 | array(new Reference('dispatcher'), new Reference('magic_function_provider')) |
0f643fb2 TO |
148 | )) |
149 | ->setFactoryService(self::SELF)->setFactoryMethod('createApiKernel'); | |
150 | ||
7b4bbb34 | 151 | $container->setDefinition('cxn_reg_client', new Definition( |
40787e18 | 152 | 'Civi\Cxn\Rpc\RegistrationClient', |
7b4bbb34 TO |
153 | array() |
154 | )) | |
9ae2d27b | 155 | ->setFactoryClass('CRM_Cxn_BAO_Cxn')->setFactoryMethod('createRegistrationClient'); |
7b4bbb34 | 156 | |
6e5ad5ee TO |
157 | $container->setDefinition('psr_log', new Definition('CRM_Core_Error_Log', array())); |
158 | ||
83617886 | 159 | foreach (array('js_strings', 'community_messages') as $cacheName) { |
a4704404 TO |
160 | $container->setDefinition("cache.{$cacheName}", new Definition( |
161 | 'CRM_Utils_Cache_Interface', | |
162 | array( | |
163 | array( | |
164 | 'name' => $cacheName, | |
a944a143 | 165 | 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'), |
a4704404 TO |
166 | ), |
167 | ) | |
168 | ))->setFactoryClass('CRM_Utils_Cache')->setFactoryMethod('create'); | |
169 | } | |
3a84c0ab | 170 | |
4ed867e0 TO |
171 | $container->setDefinition('sql_triggers', new Definition( |
172 | 'Civi\Core\SqlTriggers', | |
173 | array() | |
174 | )); | |
175 | ||
247eb841 TO |
176 | $container->setDefinition('pear_mail', new Definition('Mail')) |
177 | ->setFactoryClass('CRM_Utils_Mail')->setFactoryMethod('createMailer'); | |
178 | ||
7f835399 TO |
179 | if (empty(\Civi::$statics[__CLASS__]['boot'])) { |
180 | throw new \RuntimeException("Cannot initialize container. Boot services are undefined."); | |
181 | } | |
182 | foreach (\Civi::$statics[__CLASS__]['boot'] as $bootService => $def) { | |
56eafc21 | 183 | $container->setDefinition($bootService, new Definition())->setSynthetic(TRUE); |
83617886 TO |
184 | } |
185 | ||
c8074a93 TO |
186 | // Expose legacy singletons as services in the container. |
187 | $singletons = array( | |
188 | 'resources' => 'CRM_Core_Resources', | |
189 | 'httpClient' => 'CRM_Utils_HttpClient', | |
7b5937fe | 190 | 'cache.default' => 'CRM_Utils_Cache', |
a7c57397 | 191 | 'i18n' => 'CRM_Core_I18n', |
c8074a93 TO |
192 | // Maybe? 'config' => 'CRM_Core_Config', |
193 | // Maybe? 'smarty' => 'CRM_Core_Smarty', | |
194 | ); | |
195 | foreach ($singletons as $name => $class) { | |
196 | $container->setDefinition($name, new Definition( | |
197 | $class | |
198 | )) | |
199 | ->setFactoryClass($class)->setFactoryMethod('singleton'); | |
200 | } | |
201 | ||
43ceab3f TO |
202 | $container->setDefinition('civi_token_compat', new Definition( |
203 | 'Civi\Token\TokenCompatSubscriber', | |
204 | array() | |
205 | ))->addTag('kernel.event_subscriber'); | |
206 | ||
2045389a | 207 | foreach (array('Activity', 'Contribute', 'Event', 'Member') as $comp) { |
46f5566c TO |
208 | $container->setDefinition("crm_" . strtolower($comp) . "_tokens", new Definition( |
209 | "CRM_{$comp}_Tokens", | |
210 | array() | |
211 | ))->addTag('kernel.event_subscriber'); | |
212 | } | |
50a23755 | 213 | |
40787e18 TO |
214 | \CRM_Utils_Hook::container($container); |
215 | ||
fa184193 TO |
216 | return $container; |
217 | } | |
218 | ||
16072ce1 TO |
219 | /** |
220 | * @return \Civi\Angular\Manager | |
221 | */ | |
222 | public function createAngularManager() { | |
223 | return new \Civi\Angular\Manager(\CRM_Core_Resources::singleton()); | |
224 | } | |
225 | ||
fa184193 | 226 | /** |
40787e18 | 227 | * @param ContainerInterface $container |
43ceab3f | 228 | * @return \Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher |
fa184193 | 229 | */ |
40787e18 TO |
230 | public function createEventDispatcher($container) { |
231 | $dispatcher = new ContainerAwareEventDispatcher($container); | |
0085db83 | 232 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); |
40d5632a | 233 | $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); |
708d8fa2 | 234 | $dispatcher->addListener('hook_civicrm_post::Activity', array('\Civi\CCase\Events', 'fireCaseChange')); |
753657ed | 235 | $dispatcher->addListener('hook_civicrm_post::Case', array('\Civi\CCase\Events', 'fireCaseChange')); |
708d8fa2 | 236 | $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\Events', 'delegateToXmlListeners')); |
b019b130 | 237 | $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\SequenceListener', 'onCaseChange_static')); |
8498c2b7 | 238 | $dispatcher->addListener('DAO::post-insert', array('\CRM_Core_BAO_RecurringEntity', 'triggerInsert')); |
239 | $dispatcher->addListener('DAO::post-update', array('\CRM_Core_BAO_RecurringEntity', 'triggerUpdate')); | |
240 | $dispatcher->addListener('DAO::post-delete', array('\CRM_Core_BAO_RecurringEntity', 'triggerDelete')); | |
46bcf597 | 241 | $dispatcher->addListener('hook_civicrm_unhandled_exception', array( |
9ae2d27b TO |
242 | 'CRM_Core_LegacyErrorHandler', |
243 | 'handleException', | |
244 | )); | |
46f5566c TO |
245 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Activity_ActionMapping', 'onRegisterActionMappings')); |
246 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contact_ActionMapping', 'onRegisterActionMappings')); | |
2045389a | 247 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contribute_ActionMapping_ByPage', 'onRegisterActionMappings')); |
b5302d4e | 248 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contribute_ActionMapping_ByType', 'onRegisterActionMappings')); |
46f5566c TO |
249 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Event_ActionMapping', 'onRegisterActionMappings')); |
250 | $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Member_ActionMapping', 'onRegisterActionMappings')); | |
251 | ||
fa184193 TO |
252 | return $dispatcher; |
253 | } | |
0f643fb2 | 254 | |
10760fa1 TO |
255 | /** |
256 | * @return LockManager | |
257 | */ | |
83617886 | 258 | public static function createLockManager() { |
10760fa1 TO |
259 | // Ideally, downstream implementers could override any definitions in |
260 | // the container. For now, we'll make-do with some define()s. | |
261 | $lm = new LockManager(); | |
262 | $lm | |
263 | ->register('/^cache\./', defined('CIVICRM_CACHE_LOCK') ? CIVICRM_CACHE_LOCK : array('CRM_Core_Lock', 'createScopedLock')) | |
264 | ->register('/^data\./', defined('CIVICRM_DATA_LOCK') ? CIVICRM_DATA_LOCK : array('CRM_Core_Lock', 'createScopedLock')) | |
265 | ->register('/^worker\.mailing\.send\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : array('CRM_Core_Lock', 'createCivimailLock')) | |
266 | ->register('/^worker\./', defined('CIVICRM_WORK_LOCK') ? CIVICRM_WORK_LOCK : array('CRM_Core_Lock', 'createScopedLock')); | |
267 | ||
268 | // Registrations may use complex resolver expressions, but (as a micro-optimization) | |
269 | // the default factory is specified as an array. | |
270 | ||
271 | return $lm; | |
272 | } | |
273 | ||
0f643fb2 TO |
274 | /** |
275 | * @param \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher | |
2a6da8d7 EM |
276 | * @param $magicFunctionProvider |
277 | * | |
0f643fb2 TO |
278 | * @return \Civi\API\Kernel |
279 | */ | |
c65db512 | 280 | public function createApiKernel($dispatcher, $magicFunctionProvider) { |
0a946de2 | 281 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\ChainSubscriber()); |
b55bc593 | 282 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); |
bace5cd9 | 283 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); |
c65db512 | 284 | $dispatcher->addSubscriber($magicFunctionProvider); |
d0c9daa4 | 285 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); |
dcef11bd | 286 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\APIv3SchemaAdapter()); |
6d3bdc98 TO |
287 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\WrapperAdapter(array( |
288 | \CRM_Utils_API_HTMLInputCoder::singleton(), | |
289 | \CRM_Utils_API_NullOutputCoder::singleton(), | |
290 | \CRM_Utils_API_ReloadOption::singleton(), | |
291 | \CRM_Utils_API_MatchOption::singleton(), | |
292 | ))); | |
0661f62b | 293 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); |
82376c19 TO |
294 | $kernel = new \Civi\API\Kernel($dispatcher); |
295 | ||
296 | $reflectionProvider = new \Civi\API\Provider\ReflectionProvider($kernel); | |
297 | $dispatcher->addSubscriber($reflectionProvider); | |
298 | ||
56154d36 TO |
299 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\DynamicFKAuthorization( |
300 | $kernel, | |
301 | 'Attachment', | |
302 | array('create', 'get', 'delete'), | |
2e37a19f | 303 | // Given a file ID, determine the entity+table it's attached to. |
56154d36 TO |
304 | 'SELECT if(cf.id,1,0) as is_valid, cef.entity_table, cef.entity_id |
305 | FROM civicrm_file cf | |
306 | LEFT JOIN civicrm_entity_file cef ON cf.id = cef.file_id | |
307 | WHERE cf.id = %1', | |
29468114 TO |
308 | // Get a list of custom fields (field_name,table_name,extends) |
309 | 'SELECT concat("custom_",fld.id) as field_name, | |
310 | grp.table_name as table_name, | |
311 | grp.extends as extends | |
312 | FROM civicrm_custom_field fld | |
313 | INNER JOIN civicrm_custom_group grp ON fld.custom_group_id = grp.id | |
314 | WHERE fld.data_type = "File" | |
315 | ', | |
e3e66815 | 316 | array('civicrm_activity', 'civicrm_mailing', 'civicrm_contact', 'civicrm_grant') |
56154d36 TO |
317 | )); |
318 | ||
82376c19 TO |
319 | $kernel->setApiProviders(array( |
320 | $reflectionProvider, | |
321 | $magicFunctionProvider, | |
322 | )); | |
323 | ||
0f643fb2 TO |
324 | return $kernel; |
325 | } | |
96025800 | 326 | |
83617886 TO |
327 | /** |
328 | * Get a list of boot services. | |
329 | * | |
330 | * These are services which must be setup *before* the container can operate. | |
331 | * | |
7f835399 | 332 | * @param bool $loadFromDB |
83617886 TO |
333 | * @throws \CRM_Core_Exception |
334 | */ | |
7f835399 | 335 | public static function boot($loadFromDB) { |
56eafc21 | 336 | // Array(string $serviceId => object $serviceInstance). |
7f835399 TO |
337 | $bootServices = array(); |
338 | \Civi::$statics[__CLASS__]['boot'] = &$bootServices; | |
d4330c62 | 339 | |
56eafc21 | 340 | $bootServices['runtime'] = $runtime = new \CRM_Core_Config_Runtime(); |
7f835399 | 341 | $runtime->initialize($loadFromDB); |
d4330c62 | 342 | |
56eafc21 | 343 | $bootServices['paths'] = new \Civi\Core\Paths(); |
d4330c62 | 344 | |
7f835399 | 345 | $class = $runtime->userFrameworkClass; |
56eafc21 | 346 | $bootServices['userSystem'] = $userSystem = new $class(); |
7f835399 TO |
347 | $userSystem->initialize(); |
348 | ||
349 | $userPermissionClass = 'CRM_Core_Permission_' . $runtime->userFramework; | |
56eafc21 | 350 | $bootServices['userPermissionClass'] = new $userPermissionClass(); |
7f835399 | 351 | |
56eafc21 TO |
352 | $bootServices['cache.settings'] = \CRM_Utils_Cache::create(array( |
353 | 'name' => 'settings', | |
354 | 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'), | |
355 | )); | |
7f835399 | 356 | |
56eafc21 | 357 | $bootServices['settings_manager'] = new \Civi\Core\SettingsManager($bootServices['cache.settings']); |
7f835399 | 358 | |
56eafc21 | 359 | $bootServices['lockManager'] = self::createLockManager(); |
7f835399 TO |
360 | |
361 | if ($loadFromDB && $runtime->dsn) { | |
3a036b15 | 362 | \CRM_Core_DAO::init($runtime->dsn); |
edbcbd96 | 363 | \CRM_Utils_Hook::singleton(TRUE); |
7f835399 | 364 | \CRM_Extension_System::singleton(TRUE); |
4025c773 | 365 | \CRM_Extension_System::singleton(TRUE)->getClassLoader()->register(); |
7f835399 | 366 | |
1b81ed50 | 367 | $runtime->includeCustomPath(); |
368 | ||
7f835399 | 369 | $c = new self(); |
56eafc21 TO |
370 | $container = $c->loadContainer(); |
371 | foreach ($bootServices as $name => $obj) { | |
372 | $container->set($name, $obj); | |
373 | } | |
374 | \Civi::$statics[__CLASS__]['container'] = $container; | |
83617886 | 375 | } |
83617886 TO |
376 | } |
377 | ||
378 | public static function getBootService($name) { | |
56eafc21 | 379 | return \Civi::$statics[__CLASS__]['boot'][$name]; |
83617886 TO |
380 | } |
381 | ||
fa184193 | 382 | } |