Merge pull request #15878 from civicrm/5.20
[civicrm-core.git] / CRM / Extension / System.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This class glues together the various parts of the extension
14 * system.
15 *
16 * @package CRM
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 */
19 class CRM_Extension_System {
20 private static $singleton;
21
22 private $cache = NULL;
23 private $fullContainer = NULL;
24 private $defaultContainer = NULL;
25 private $mapper = NULL;
26 private $manager = NULL;
27 private $browser = NULL;
28 private $downloader = NULL;
29
30 /**
31 * @var CRM_Extension_ClassLoader
32 * */
33 private $classLoader;
34
35 /**
36 * The URL of the remote extensions repository.
37 *
38 * @var string|false
39 */
40 private $_repoUrl = NULL;
41
42 /**
43 * @var array
44 * Construction parameters. These are primarily retained so
45 * that they can influence the cache name.
46 */
47 protected $parameters;
48
49 /**
50 * @param bool $fresh
51 * TRUE to force creation of a new system.
52 *
53 * @return CRM_Extension_System
54 */
55 public static function singleton($fresh = FALSE) {
56 if (!self::$singleton || $fresh) {
57 if (self::$singleton) {
58 self::$singleton = new CRM_Extension_System(self::$singleton->parameters);
59 }
60 else {
61 self::$singleton = new CRM_Extension_System();
62 }
63 }
64 return self::$singleton;
65 }
66
67 /**
68 * @param CRM_Extension_System $singleton
69 * The new, singleton extension system.
70 */
71 public static function setSingleton(CRM_Extension_System $singleton) {
72 self::$singleton = $singleton;
73 }
74
75 /**
76 * Class constructor.
77 *
78 * @param array $parameters
79 * List of configuration values required by the extension system.
80 * Missing values will be guessed based on $config.
81 */
82 public function __construct($parameters = []) {
83 $config = CRM_Core_Config::singleton();
84 $parameters['extensionsDir'] = CRM_Utils_Array::value('extensionsDir', $parameters, $config->extensionsDir);
85 $parameters['extensionsURL'] = CRM_Utils_Array::value('extensionsURL', $parameters, $config->extensionsURL);
86 $parameters['resourceBase'] = CRM_Utils_Array::value('resourceBase', $parameters, $config->resourceBase);
87 $parameters['uploadDir'] = CRM_Utils_Array::value('uploadDir', $parameters, $config->uploadDir);
88 $parameters['userFrameworkBaseURL'] = CRM_Utils_Array::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL);
89 if (!array_key_exists('civicrm_root', $parameters)) {
90 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
91 }
92 if (!array_key_exists('cmsRootPath', $parameters)) {
93 $parameters['cmsRootPath'] = $config->userSystem->cmsRootPath();
94 }
95 if (!array_key_exists('domain_id', $parameters)) {
96 $parameters['domain_id'] = CRM_Core_Config::domainID();
97 }
98 // guaranteed ordering - useful for md5(serialize($parameters))
99 ksort($parameters);
100
101 $this->parameters = $parameters;
102 }
103
104 /**
105 * Get a container which represents all available extensions.
106 *
107 * @return CRM_Extension_Container_Interface
108 */
109 public function getFullContainer() {
110 if ($this->fullContainer === NULL) {
111 $containers = [];
112
113 if ($this->getDefaultContainer()) {
114 $containers['default'] = $this->getDefaultContainer();
115 }
116
117 $containers['civiroot'] = new CRM_Extension_Container_Basic(
118 $this->parameters['civicrm_root'],
119 $this->parameters['resourceBase'],
120 $this->getCache(),
121 'civiroot'
122 );
123
124 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
125 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
126 // TODO: CRM_Extension_Container_Basic( /modules )
127 // TODO: CRM_Extension_Container_Basic( /vendors )
128
129 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
130 if (NULL !== $this->parameters['cmsRootPath']) {
131 $vendorPath = $this->parameters['cmsRootPath'] . DIRECTORY_SEPARATOR . 'vendor';
132 if (is_dir($vendorPath)) {
133 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
134 $vendorPath,
135 CRM_Utils_File::addTrailingSlash($this->parameters['userFrameworkBaseURL'], '/') . 'vendor',
136 $this->getCache(),
137 'cmsvendor'
138 );
139 }
140 }
141
142 if (!defined('CIVICRM_TEST')) {
143 foreach ($containers as $container) {
144 $container->addFilter([__CLASS__, 'isNotTestExtension']);
145 }
146 }
147
148 $this->fullContainer = new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
149 }
150 return $this->fullContainer;
151 }
152
153 /**
154 * Get the container to which new extensions are installed.
155 *
156 * This container should be a particular, writeable directory.
157 *
158 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
159 */
160 public function getDefaultContainer() {
161 if ($this->defaultContainer === NULL) {
162 if ($this->parameters['extensionsDir']) {
163 $this->defaultContainer = new CRM_Extension_Container_Default($this->parameters['extensionsDir'], $this->parameters['extensionsURL'], $this->getCache(), 'default');
164 }
165 else {
166 $this->defaultContainer = FALSE;
167 }
168 }
169 return $this->defaultContainer;
170 }
171
172 /**
173 * Get the service which provides runtime information about extensions.
174 *
175 * @return CRM_Extension_Mapper
176 */
177 public function getMapper() {
178 if ($this->mapper === NULL) {
179 $this->mapper = new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
180 }
181 return $this->mapper;
182 }
183
184 /**
185 * @return \CRM_Extension_ClassLoader
186 */
187 public function getClassLoader() {
188 if ($this->classLoader === NULL) {
189 $this->classLoader = new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager());
190 }
191 return $this->classLoader;
192 }
193
194 /**
195 * Get the service for enabling and disabling extensions.
196 *
197 * @return CRM_Extension_Manager
198 */
199 public function getManager() {
200 if ($this->manager === NULL) {
201 $typeManagers = [
202 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
203 'report' => new CRM_Extension_Manager_Report(),
204 'search' => new CRM_Extension_Manager_Search(),
205 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
206 ];
207 $this->manager = new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
208 }
209 return $this->manager;
210 }
211
212 /**
213 * Get the service for finding remotely-available extensions
214 *
215 * @return CRM_Extension_Browser
216 */
217 public function getBrowser() {
218 if ($this->browser === NULL) {
219 $cacheDir = NULL;
220 if (!empty($this->parameters['uploadDir'])) {
221 $cacheDir = CRM_Utils_File::addTrailingSlash($this->parameters['uploadDir']) . 'cache';
222 }
223 $this->browser = new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
224 }
225 return $this->browser;
226 }
227
228 /**
229 * Get the service for loading code from remotely-available extensions
230 *
231 * @return CRM_Extension_Downloader
232 */
233 public function getDownloader() {
234 if ($this->downloader === NULL) {
235 $basedir = ($this->getDefaultContainer() ? $this->getDefaultContainer()->getBaseDir() : NULL);
236 $this->downloader = new CRM_Extension_Downloader(
237 $this->getManager(),
238 $basedir,
239 // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
240 CRM_Utils_File::tempdir()
241 );
242 }
243 return $this->downloader;
244 }
245
246 /**
247 * @return CRM_Utils_Cache_Interface
248 */
249 public function getCache() {
250 if ($this->cache === NULL) {
251 $cacheGroup = md5(serialize(['ext', $this->parameters, CRM_Utils_System::version()]));
252 // Extension system starts before container. Manage our own cache.
253 $this->cache = CRM_Utils_Cache::create([
254 'name' => $cacheGroup,
255 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'],
256 'prefetch' => TRUE,
257 ]);
258 }
259 return $this->cache;
260 }
261
262 /**
263 * Determine the URL which provides a feed of available extensions.
264 *
265 * @return string|FALSE
266 */
267 public function getRepositoryUrl() {
268 if (empty($this->_repoUrl) && $this->_repoUrl !== FALSE) {
269 $url = Civi::settings()->get('ext_repo_url');
270
271 // boolean false means don't try to check extensions
272 // CRM-10575
273 if ($url === FALSE) {
274 $this->_repoUrl = FALSE;
275 }
276 else {
277 $this->_repoUrl = CRM_Utils_System::evalUrl($url);
278 }
279 }
280 return $this->_repoUrl;
281 }
282
283 /**
284 * Returns a list keyed by extension key
285 *
286 * @return array
287 */
288 public static function getCompatibilityInfo() {
289 if (!isset(Civi::$statics[__CLASS__]['compatibility'])) {
290 Civi::$statics[__CLASS__]['compatibility'] = json_decode(file_get_contents(Civi::paths()->getPath('[civicrm.root]/extension-compatibility.json')), TRUE);
291 }
292 return Civi::$statics[__CLASS__]['compatibility'];
293 }
294
295 public static function isNotTestExtension(CRM_Extension_Info $info) {
296 return (bool) !preg_match('/^test\./', $info->key);
297 }
298
299 /**
300 * Take an extension's raw XML info and add information about the
301 * extension's status on the local system.
302 *
303 * The result format resembles the old CRM_Core_Extensions_Extension.
304 *
305 * @param CRM_Extension_Info $obj
306 *
307 * @return array
308 */
309 public static function createExtendedInfo(CRM_Extension_Info $obj) {
310 $mapper = CRM_Extension_System::singleton()->getMapper();
311 $manager = CRM_Extension_System::singleton()->getManager();
312
313 $extensionRow = (array) $obj;
314 try {
315 $extensionRow['path'] = $mapper->keyToBasePath($obj->key);
316 }
317 catch (CRM_Extension_Exception $e) {
318 $extensionRow['path'] = '';
319 }
320 $extensionRow['status'] = $manager->getStatus($obj->key);
321
322 switch ($extensionRow['status']) {
323 case CRM_Extension_Manager::STATUS_UNINSTALLED:
324 // ts('Uninstalled');
325 $extensionRow['statusLabel'] = '';
326 break;
327
328 case CRM_Extension_Manager::STATUS_DISABLED:
329 $extensionRow['statusLabel'] = ts('Disabled');
330 break;
331
332 case CRM_Extension_Manager::STATUS_INSTALLED:
333 // ts('Installed');
334 $extensionRow['statusLabel'] = ts('Enabled');
335 break;
336
337 case CRM_Extension_Manager::STATUS_DISABLED_MISSING:
338 $extensionRow['statusLabel'] = ts('Disabled (Missing)');
339 break;
340
341 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
342 // ts('Installed');
343 $extensionRow['statusLabel'] = ts('Enabled (Missing)');
344 break;
345
346 default:
347 $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')';
348 }
349 if ($manager->isIncompatible($obj->key)) {
350 $extensionRow['statusLabel'] = ts('Obsolete') . ($extensionRow['statusLabel'] ? (' - ' . $extensionRow['statusLabel']) : '');
351 }
352 return $extensionRow;
353 }
354
355 }