From 15e47dfc813ac443adeb4026c74bb8c8ff99c04e Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 2 Dec 2021 20:50:05 -0800 Subject: [PATCH] (REF) Mixins - Split apart class-loader and mixin-loader --- CRM/Extension/ClassLoader.php | 39 +++++++++++++---------------------- CRM/Extension/Mapper.php | 1 + CRM/Extension/System.php | 20 ++++++++++++++++++ Civi/Core/Container.php | 3 ++- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/CRM/Extension/ClassLoader.php b/CRM/Extension/ClassLoader.php index 403c682f08..a46580651d 100644 --- a/CRM/Extension/ClassLoader.php +++ b/CRM/Extension/ClassLoader.php @@ -63,33 +63,23 @@ class CRM_Extension_ClassLoader { */ public function register() { // In pre-installation environments, don't bother with caching. - $cacheFile = (defined('CIVICRM_DSN') && !defined('CIVICRM_TEST') && !\CRM_Utils_System::isInUpgradeMode()) - ? $this->getCacheFile() : NULL; + if (!defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || \CRM_Utils_System::isInUpgradeMode()) { + $this->loader = $this->buildClassLoader(); + return $this->loader->register(); + } - if (file_exists($cacheFile)) { - [$classLoader, $mixinLoader, $bootCache] = require $cacheFile; - $cacheUpdate = NULL; + $file = $this->getCacheFile(); + if (file_exists($file)) { + $this->loader = require $file; } else { - $classLoader = $this->buildClassLoader(); - $mixinLoader = (new CRM_Extension_MixinScanner($this->mapper, $this->manager, $cacheFile !== NULL))->createLoader(); - $bootCache = new CRM_Extension_BootCache(); - // We don't own Composer\Autoload\ClassLoader, so we clone to prevent register() from potentially leaking data. - // We do own MixinLoader, and we want its state - like $bootCache - to be written. - $cacheUpdate = $cacheFile ? [clone $classLoader, clone $mixinLoader, $bootCache] : NULL; + $this->loader = $this->buildClassLoader(); + $ser = serialize($this->loader); + file_put_contents($file, + sprintf("register(); - $mixinLoader->run($bootCache); - - if ($cacheUpdate !== NULL) { - // Save cache after $mixinLoader has a chance to fill $bootCache. - $export = var_export(serialize($cacheUpdate), 1); - file_put_contents($cacheFile, sprintf("loader = $classLoader; - return $classLoader; + return $this->loader->register(); } /** @@ -172,8 +162,7 @@ class CRM_Extension_ClassLoader { * @return string */ protected function getCacheFile() { - $formatRev = '_2'; - $envId = \CRM_Core_Config_Runtime::getId() . $formatRev; + $envId = \CRM_Core_Config_Runtime::getId(); $file = \Civi::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php"); return $file; } diff --git a/CRM/Extension/Mapper.php b/CRM/Extension/Mapper.php index dd989c8413..f82b9c8285 100644 --- a/CRM/Extension/Mapper.php +++ b/CRM/Extension/Mapper.php @@ -538,6 +538,7 @@ class CRM_Extension_Mapper { } // FIXME: How can code so code wrong be so right? CRM_Extension_System::singleton()->getClassLoader()->refresh(); + \CRM_Extension_System::singleton()->applyMixins(TRUE); } /** diff --git a/CRM/Extension/System.php b/CRM/Extension/System.php index b9bb47dd3b..34cae07f56 100644 --- a/CRM/Extension/System.php +++ b/CRM/Extension/System.php @@ -243,6 +243,26 @@ class CRM_Extension_System { return $this->downloader; } + public function applyMixins($force = FALSE) { + $cache = $this->getCache(); + + $cachedMixinLoader = $force ? NULL : $cache->get('mixinLoader'); + $cachedBootData = $force ? NULL : $cache->get('mixinBoot'); + + $mixinLoader = $cachedMixinLoader ?: (new CRM_Extension_MixinScanner($this->mapper, $this->manager, TRUE))->createLoader(); + $bootData = $cachedBootData ?: new CRM_Extension_BootCache(); + + $mixinLoader->run($bootData); + + if ($cachedMixinLoader === NULL) { + $cache->set('mixinLoader', $mixinLoader, 24 * 60 * 60); + } + if ($cachedBootData === NULL) { + $bootData->lock(); + $cache->set('mixinBoot', $bootData, 24 * 60 * 60); + } + } + /** * @return CRM_Utils_Cache_Interface */ diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 841f318ba5..fed7cca6d1 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -607,7 +607,8 @@ class Container { \CRM_Core_DAO::init($runtime->dsn); \CRM_Utils_Hook::singleton(TRUE); \CRM_Extension_System::singleton(TRUE); - \CRM_Extension_System::singleton(TRUE)->getClassLoader()->register(); + \CRM_Extension_System::singleton()->getClassLoader()->register(); + \CRM_Extension_System::singleton()->applyMixins(); $bootServices['dispatcher.boot']->setDispatchPolicy($mainDispatchPolicy); $runtime->includeCustomPath(); -- 2.25.1