3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2018
36 class CRM_Extension_ClassLoader
{
39 * @var CRM_Extension_Mapper
44 * @var CRM_Extension_Container_Interface
49 * @var CRM_Extension_Manager
54 * @var \Composer\Autoload\ClassLoader
59 * CRM_Extension_ClassLoader constructor.
60 * @param \CRM_Extension_Mapper $mapper
61 * @param \CRM_Extension_Container_Interface $container
62 * @param \CRM_Extension_Manager $manager
64 public function __construct(\CRM_Extension_Mapper
$mapper, \CRM_Extension_Container_Interface
$container, \CRM_Extension_Manager
$manager) {
65 $this->mapper
= $mapper;
66 $this->container
= $container;
67 $this->manager
= $manager;
70 public function __destruct() {
75 * Registers this instance as an autoloader.
76 * @return CRM_Extension_ClassLoader
78 public function register() {
79 // In pre-installation environments, don't bother with caching.
80 if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') ||
!defined('CIVICRM_DSN') ||
defined('CIVICRM_TEST') || \CRM_Utils_System
::isInUpgradeMode()) {
81 return $this->buildClassLoader()->register();
84 $file = $this->getCacheFile();
85 if (file_exists($file)) {
86 $loader = require $file;
89 $loader = $this->buildClassLoader();
90 $ser = serialize($loader);
91 file_put_contents($file,
92 sprintf("<?php\nreturn unserialize(%s);", var_export($ser, 1))
95 return $loader->register();
99 * @return \Composer\Autoload\ClassLoader
100 * @throws \CRM_Extension_Exception
103 public function buildClassLoader() {
104 $loader = new \Composer\Autoload\
ClassLoader();
106 $statuses = $this->manager
->getStatuses();
107 foreach ($statuses as $key => $status) {
108 if ($status !== CRM_Extension_Manager
::STATUS_INSTALLED
) {
111 $path = $this->mapper
->keyToBasePath($key);
112 $info = $this->mapper
->keyToInfo($key);
113 if (!empty($info->classloader
)) {
114 foreach ($info->classloader
as $mapping) {
115 switch ($mapping['type']) {
117 $loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']);
120 $result[] = $mapping;
128 public function unregister() {
130 $this->loader
->unregister();
131 $this->loader
= NULL;
135 public function refresh() {
137 $file = $this->getCacheFile();
138 if (file_exists($file)) {
147 protected function getCacheFile() {
148 $envId = \CRM_Core_Config_Runtime
::getId();
149 $file = CIVICRM_TEMPLATE_COMPILEDIR
. "/CachedExtLoader.{$envId}.php";