/** * Registers this instance as an autoloader. * @return $this */ public function register() { // In pre-installation environments, don't bother with caching. if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') || !defined('CIVICRM_DSN') || \CRM_Utils_System::isInUpgradeMode()) { return $this->buildClassLoader()->register(); } $envId = \CRM_Core_Config_Runtime::getId(); $file = CIVICRM_TEMPLATE_COMPILEDIR . "/CachedExtLoader.{$envId}.php"; if (file_exists($file)) { $loader = (require $file); } else { $loader = $this->buildClassLoader(); $ser = serialize($loader); file_put_contents($file, sprintf("<?php\nreturn unserialize(%s);", var_export($ser, 1))); } return $loader->register(); }
/** * Find a cached container definition or construct a new one. * * There are many weird contexts in which Civi initializes (eg different * variations of multitenancy and different permutations of CMS/CRM bootstrap), * and hook_container may fire a bit differently in each context. To mitigate * risk of leaks between environments, we compute a unique envID * (md5(DB_NAME, HTTP_HOST, SCRIPT_FILENAME, etc)) and use separate caches for * each (eg "templates_c/CachedCiviContainer.$ENVID.php"). * * Constants: * - CIVICRM_CONTAINER_CACHE -- 'always' [default], 'never', 'auto' * - CIVICRM_DSN * - CIVICRM_DOMAIN_ID * - CIVICRM_TEMPLATE_COMPILEDIR * * @return ContainerInterface */ public function loadContainer() { // Note: The container's raison d'etre is to manage construction of other // services. Consequently, we assume a minimal service available -- the classloader // has been setup, and civicrm.settings.php is loaded, but nothing else works. $cacheMode = defined('CIVICRM_CONTAINER_CACHE') ? CIVICRM_CONTAINER_CACHE : 'always'; // In pre-installation environments, don't bother with caching. if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') || !defined('CIVICRM_DSN') || $cacheMode === 'never' || \CRM_Utils_System::isInUpgradeMode()) { return $this->createContainer(); } $envId = \CRM_Core_Config_Runtime::getId(); $file = CIVICRM_TEMPLATE_COMPILEDIR . "/CachedCiviContainer.{$envId}.php"; $containerConfigCache = new ConfigCache($file, $cacheMode === 'auto'); if (!$containerConfigCache->isFresh()) { $containerBuilder = $this->createContainer(); $containerBuilder->compile(); $dumper = new PhpDumper($containerBuilder); $containerConfigCache->write($dumper->dump(array('class' => 'CachedCiviContainer')), $containerBuilder->getResources()); } require_once $file; $c = new \CachedCiviContainer(); $c->set('service_container', $c); return $c; }
/** * @return string */ protected function getCacheFile() { $envId = \CRM_Core_Config_Runtime::getId(); $file = CIVICRM_TEMPLATE_COMPILEDIR . "/CachedExtLoader.{$envId}.php"; return $file; }