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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 class CRM_Extension_ClassLoader
{
23 * @var CRM_Extension_Mapper
28 * @var CRM_Extension_Container_Interface
33 * @var CRM_Extension_Manager
38 * @var \Composer\Autoload\ClassLoader
43 * CRM_Extension_ClassLoader constructor.
44 * @param \CRM_Extension_Mapper $mapper
45 * @param \CRM_Extension_Container_Interface $container
46 * @param \CRM_Extension_Manager $manager
48 public function __construct(\CRM_Extension_Mapper
$mapper, \CRM_Extension_Container_Interface
$container, \CRM_Extension_Manager
$manager) {
49 $this->mapper
= $mapper;
50 $this->container
= $container;
51 $this->manager
= $manager;
54 public function __destruct() {
59 * Registers this instance as an autoloader.
60 * @return CRM_Extension_ClassLoader
62 public function register() {
63 // In pre-installation environments, don't bother with caching.
64 if (!defined('CIVICRM_DSN') ||
defined('CIVICRM_TEST') || \CRM_Utils_System
::isInUpgradeMode()) {
65 return $this->buildClassLoader()->register();
68 $file = $this->getCacheFile();
69 if (file_exists($file)) {
70 $loader = require $file;
73 $loader = $this->buildClassLoader();
74 $ser = serialize($loader);
75 file_put_contents($file,
76 sprintf("<?php\nreturn unserialize(%s);", var_export($ser, 1))
79 return $loader->register();
83 * @return \Composer\Autoload\ClassLoader
84 * @throws \CRM_Extension_Exception
87 public function buildClassLoader() {
88 $loader = new \Composer\Autoload\
ClassLoader();
90 $statuses = $this->manager
->getStatuses();
91 foreach ($statuses as $key => $status) {
92 if ($status !== CRM_Extension_Manager
::STATUS_INSTALLED
) {
95 $path = $this->mapper
->keyToBasePath($key);
96 $info = $this->mapper
->keyToInfo($key);
97 if (!empty($info->classloader
)) {
98 foreach ($info->classloader
as $mapping) {
99 switch ($mapping['type']) {
101 $loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']);
104 $result[] = $mapping;
112 public function unregister() {
114 $this->loader
->unregister();
115 $this->loader
= NULL;
119 public function refresh() {
121 $file = $this->getCacheFile();
122 if (file_exists($file)) {
131 protected function getCacheFile() {
132 $envId = \CRM_Core_Config_Runtime
::getId();
133 $file = \Civi
::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php");