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 * @var CRM_Extension_Mapper
23 * @var CRM_Extension_Container_Interface
28 * @var CRM_Extension_Manager
33 * @var \Composer\Autoload\ClassLoader
38 * CRM_Extension_ClassLoader constructor.
39 * @param \CRM_Extension_Mapper $mapper
40 * @param \CRM_Extension_Container_Interface $container
41 * @param \CRM_Extension_Manager $manager
43 public function __construct(\CRM_Extension_Mapper
$mapper, \CRM_Extension_Container_Interface
$container, \CRM_Extension_Manager
$manager) {
44 $this->mapper
= $mapper;
45 $this->container
= $container;
46 $this->manager
= $manager;
49 public function __destruct() {
54 * Registers this instance as an autoloader.
55 * @return CRM_Extension_ClassLoader
57 public function register() {
58 // In pre-installation environments, don't bother with caching.
59 if (!defined('CIVICRM_DSN') ||
defined('CIVICRM_TEST') || \CRM_Utils_System
::isInUpgradeMode()) {
60 return $this->buildClassLoader()->register();
63 $file = $this->getCacheFile();
64 if (file_exists($file)) {
65 $loader = require $file;
68 $loader = $this->buildClassLoader();
69 $ser = serialize($loader);
70 file_put_contents($file,
71 sprintf("<?php\nreturn unserialize(%s);", var_export($ser, 1))
74 return $loader->register();
78 * @return \Composer\Autoload\ClassLoader
79 * @throws \CRM_Extension_Exception
82 public function buildClassLoader() {
83 $loader = new \Composer\Autoload\
ClassLoader();
85 $statuses = $this->manager
->getStatuses();
86 foreach ($statuses as $key => $status) {
87 if ($status !== CRM_Extension_Manager
::STATUS_INSTALLED
) {
90 $path = $this->mapper
->keyToBasePath($key);
91 $info = $this->mapper
->keyToInfo($key);
92 if (!empty($info->classloader
)) {
93 foreach ($info->classloader
as $mapping) {
94 switch ($mapping['type']) {
96 $loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']);
107 public function unregister() {
109 $this->loader
->unregister();
110 $this->loader
= NULL;
114 public function refresh() {
116 $file = $this->getCacheFile();
117 if (file_exists($file)) {
126 protected function getCacheFile() {
127 $envId = \CRM_Core_Config_Runtime
::getId();
128 $file = \Civi
::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php");