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