mapper = $mapper; $this->container = $container; $this->manager = $manager; } public function __destruct() { $this->unregister(); } /** * Registers this instance as an autoloader. * @return CRM_Extension_ClassLoader */ public function register() { // In pre-installation environments, don't bother with caching. if (!defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || \CRM_Utils_System::isInUpgradeMode()) { return $this->buildClassLoader()->register(); } $file = $this->getCacheFile(); if (file_exists($file)) { $loader = require $file; } else { $loader = $this->buildClassLoader(); $ser = serialize($loader); file_put_contents($file, sprintf("register(); } /** * @return \Composer\Autoload\ClassLoader * @throws \CRM_Extension_Exception * @throws \Exception */ public function buildClassLoader() { $loader = new \Composer\Autoload\ClassLoader(); $statuses = $this->manager->getStatuses(); foreach ($statuses as $key => $status) { if ($status !== CRM_Extension_Manager::STATUS_INSTALLED) { continue; } $path = $this->mapper->keyToBasePath($key); $info = $this->mapper->keyToInfo($key); if (!empty($info->classloader)) { foreach ($info->classloader as $mapping) { switch ($mapping['type']) { case 'psr4': $loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']); break; } $result[] = $mapping; } } } return $loader; } public function unregister() { if ($this->loader) { $this->loader->unregister(); $this->loader = NULL; } } public function refresh() { $this->unregister(); $file = $this->getCacheFile(); if (file_exists($file)) { unlink($file); } $this->register(); } /** * @return string */ protected function getCacheFile() { $envId = \CRM_Core_Config_Runtime::getId(); $file = \Civi::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php"); return $file; } }