Revert changes in CRM/Core/BAO{Cache|ConfigSetting}.php CRM/Extension/ClassLoader...
[civicrm-core.git] / CRM / Extension / ClassLoader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36 class CRM_Extension_ClassLoader {
37
38 /**
39 * @var CRM_Extension_Mapper
40 */
41 protected $mapper;
42
43 /**
44 * @var CRM_Extension_Container_Interface
45 */
46 protected $container;
47
48 /**
49 * @var CRM_Extension_Manager
50 */
51 protected $manager;
52
53 /**
54 * @var \Composer\Autoload\ClassLoader
55 */
56 protected $loader;
57
58 /**
59 * CRM_Extension_ClassLoader constructor.
60 * @param \CRM_Extension_Mapper $mapper
61 * @param \CRM_Extension_Container_Interface $container
62 * @param \CRM_Extension_Manager $manager
63 */
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;
68 }
69
70 public function __destruct() {
71 $this->unregister();
72 }
73
74 /**
75 * Registers this instance as an autoloader.
76 * @return CRM_Extension_ClassLoader
77 */
78 public function register() {
79 // In pre-installation environments, don't bother with caching.
80 if (!defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || \CRM_Utils_System::isInUpgradeMode()) {
81 return $this->buildClassLoader()->register();
82 }
83
84 $file = $this->getCacheFile();
85 if (file_exists($file)) {
86 $loader = require $file;
87 }
88 else {
89 $loader = $this->buildClassLoader();
90 $ser = serialize($loader);
91 file_put_contents($file,
92 sprintf("<?php\nreturn unserialize(%s);", var_export($ser, 1))
93 );
94 }
95 return $loader->register();
96 }
97
98 /**
99 * @return \Composer\Autoload\ClassLoader
100 * @throws \CRM_Extension_Exception
101 * @throws \Exception
102 */
103 public function buildClassLoader() {
104 $loader = new \Composer\Autoload\ClassLoader();
105
106 $statuses = $this->manager->getStatuses();
107 foreach ($statuses as $key => $status) {
108 if ($status !== CRM_Extension_Manager::STATUS_INSTALLED) {
109 continue;
110 }
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']) {
116 case 'psr4':
117 $loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']);
118 break;
119 }
120 $result[] = $mapping;
121 }
122 }
123 }
124
125 return $loader;
126 }
127
128 public function unregister() {
129 if ($this->loader) {
130 $this->loader->unregister();
131 $this->loader = NULL;
132 }
133 }
134
135 public function refresh() {
136 $this->unregister();
137 $file = $this->getCacheFile();
138 if (file_exists($file)) {
139 unlink($file);
140 }
141 $this->register();
142 }
143
144 /**
145 * @return string
146 */
147 protected function getCacheFile() {
148 $envId = \CRM_Core_Config_Runtime::getId();
149 $file = \Civi::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php");
150 return $file;
151 }
152
153 }