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