3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Extension_ClassLoader
15 class CRM_Extension_ClassLoader
{
18 * List of class-loader features that are valid in this version of Civi.
20 * This may be useful for some extensions which enable/disable polyfills based on environment.
22 const FEATURES
= ',psr0,psr4,';
25 * @var CRM_Extension_Mapper
30 * @var CRM_Extension_Container_Interface
35 * @var CRM_Extension_Manager
40 * @var \Composer\Autoload\ClassLoader
45 * CRM_Extension_ClassLoader constructor.
46 * @param \CRM_Extension_Mapper $mapper
47 * @param \CRM_Extension_Container_Interface $container
48 * @param \CRM_Extension_Manager $manager
50 public function __construct(\CRM_Extension_Mapper
$mapper, \CRM_Extension_Container_Interface
$container, \CRM_Extension_Manager
$manager) {
51 $this->mapper
= $mapper;
52 $this->container
= $container;
53 $this->manager
= $manager;
56 public function __destruct() {
61 * Registers this instance as an autoloader.
62 * @return CRM_Extension_ClassLoader
64 public function register() {
65 // In pre-installation environments, don't bother with caching.
66 $cacheFile = (defined('CIVICRM_DSN') && !defined('CIVICRM_TEST') && !\CRM_Utils_System
::isInUpgradeMode())
67 ?
$this->getCacheFile() : NULL;
69 if (file_exists($cacheFile)) {
70 [$classLoader, $mixinLoader, $bootCache] = require $cacheFile;
74 $classLoader = $this->buildClassLoader();
75 $mixinLoader = (new CRM_Extension_MixinScanner($this->mapper
, $this->manager
, $cacheFile !== NULL))->createLoader();
76 $bootCache = new CRM_Extension_BootCache();
77 // We don't own Composer\Autoload\ClassLoader, so we clone to prevent register() from potentially leaking data.
78 // We do own MixinLoader, and we want its state - like $bootCache - to be written.
79 $cacheUpdate = $cacheFile ?
[clone $classLoader, clone $mixinLoader, $bootCache] : NULL;
82 $classLoader->register();
83 $mixinLoader->run($bootCache);
85 if ($cacheUpdate !== NULL) {
86 // Save cache after $mixinLoader has a chance to fill $bootCache.
87 $export = var_export(serialize($cacheUpdate), 1);
88 file_put_contents($cacheFile, sprintf("<?php\nreturn unserialize(%s);", $export));
91 $this->loader
= $classLoader;
96 * @return \Composer\Autoload\ClassLoader
97 * @throws \CRM_Extension_Exception
100 public function buildClassLoader() {
101 $loader = new \Composer\Autoload\
ClassLoader();
103 $statuses = $this->manager
->getStatuses();
104 foreach ($statuses as $key => $status) {
105 if ($status !== CRM_Extension_Manager
::STATUS_INSTALLED
) {
108 self
::loadExtension($loader, $this->mapper
->keyToInfo($key), $this->mapper
->keyToBasePath($key));
114 public function unregister() {
116 $this->loader
->unregister();
117 $this->loader
= NULL;
121 public function refresh() {
123 $file = $this->getCacheFile();
124 if (file_exists($file)) {
131 * Add a newly installed extension to the active classloader.
133 * NOTE: This is intended for use by CRM/Extension subsystem during installation.
135 * @param \CRM_Extension_Info $info
136 * @param string $path
138 public function installExtension(CRM_Extension_Info
$info, string $path): void
{
139 $file = $this->getCacheFile();
140 if (file_exists($file)) {
144 self
::loadExtension($this->loader
, $info, $path);
149 * Read the extension metadata configure a classloader.
151 * @param \Composer\Autoload\ClassLoader $loader
152 * @param \CRM_Extension_Info $info
153 * @param string $path
155 private static function loadExtension(\Composer\Autoload\ClassLoader
$loader, CRM_Extension_Info
$info, string $path): void
{
156 if (!empty($info->classloader
)) {
157 foreach ($info->classloader
as $mapping) {
158 switch ($mapping['type']) {
160 $loader->add($mapping['prefix'], CRM_Utils_File
::addTrailingSlash($path . '/' . $mapping['path']));
164 $loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']);
174 protected function getCacheFile() {
176 $envId = \CRM_Core_Config_Runtime
::getId() . $formatRev;
177 $file = \Civi
::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php");