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