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 * This class glues together the various parts of the extension
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Extension_System
{
20 private static $singleton;
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 private $mixinLoader = NULL;
32 * @var CRM_Extension_ClassLoader
37 * The URL of the remote extensions repository.
41 private $_repoUrl = NULL;
45 * Construction parameters. These are primarily retained so
46 * that they can influence the cache name.
48 protected $parameters;
52 * TRUE to force creation of a new system.
54 * @return CRM_Extension_System
56 public static function singleton($fresh = FALSE) {
57 if (!self
::$singleton ||
$fresh) {
58 if (self
::$singleton) {
59 self
::$singleton = new CRM_Extension_System(self
::$singleton->parameters
);
62 self
::$singleton = new CRM_Extension_System();
65 return self
::$singleton;
69 * @param CRM_Extension_System $singleton
70 * The new, singleton extension system.
72 public static function setSingleton(CRM_Extension_System
$singleton) {
73 self
::$singleton = $singleton;
79 * @param array $parameters
80 * List of configuration values required by the extension system.
81 * Missing values will be guessed based on $config.
83 public function __construct($parameters = []) {
84 $config = CRM_Core_Config
::singleton();
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
);
88 $parameters['uploadDir'] = CRM_Utils_Array
::value('uploadDir', $parameters, $config->uploadDir
);
89 $parameters['userFrameworkBaseURL'] = CRM_Utils_Array
::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL
);
90 if (!array_key_exists('civicrm_root', $parameters)) {
91 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
93 if (!array_key_exists('cmsRootPath', $parameters)) {
94 $parameters['cmsRootPath'] = $config->userSystem
->cmsRootPath();
96 if (!array_key_exists('domain_id', $parameters)) {
97 $parameters['domain_id'] = CRM_Core_Config
::domainID();
99 // guaranteed ordering - useful for md5(serialize($parameters))
102 $this->parameters
= $parameters;
106 * Get a container which represents all available extensions.
108 * @return CRM_Extension_Container_Interface
110 public function getFullContainer() {
111 if ($this->fullContainer
=== NULL) {
114 if ($this->getDefaultContainer()) {
115 $containers['default'] = $this->getDefaultContainer();
118 $containers['civiroot'] = new CRM_Extension_Container_Basic(
119 $this->parameters
['civicrm_root'],
120 $this->parameters
['resourceBase'],
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 )
130 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
131 if (NULL !== $this->parameters
['cmsRootPath']) {
132 $vendorPath = $this->parameters
['cmsRootPath'] . DIRECTORY_SEPARATOR
. 'vendor';
133 if (is_dir($vendorPath)) {
134 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
136 CRM_Utils_File
::addTrailingSlash($this->parameters
['userFrameworkBaseURL'], '/') . 'vendor',
143 if (!defined('CIVICRM_TEST')) {
144 foreach ($containers as $container) {
145 $container->addFilter([__CLASS__
, 'isNotTestExtension']);
149 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
151 return $this->fullContainer
;
155 * Get the container to which new extensions are installed.
157 * This container should be a particular, writeable directory.
159 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
161 public function getDefaultContainer() {
162 if ($this->defaultContainer
=== NULL) {
163 if ($this->parameters
['extensionsDir']) {
164 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
167 $this->defaultContainer
= FALSE;
170 return $this->defaultContainer
;
174 * Get the service which provides runtime information about extensions.
176 * @return CRM_Extension_Mapper
178 public function getMapper() {
179 if ($this->mapper
=== NULL) {
180 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
182 return $this->mapper
;
186 * @return \CRM_Extension_ClassLoader
188 public function getClassLoader() {
189 if ($this->classLoader
=== NULL) {
190 $this->classLoader
= new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager());
192 return $this->classLoader
;
196 * Get the service for enabling and disabling extensions.
198 * @return CRM_Extension_Manager
200 public function getManager() {
201 if ($this->manager
=== NULL) {
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()),
208 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
210 return $this->manager
;
214 * Get the service for finding remotely-available extensions
216 * @return CRM_Extension_Browser
218 public function getBrowser() {
219 if ($this->browser
=== NULL) {
221 if (!empty($this->parameters
['uploadDir'])) {
222 $cacheDir = CRM_Utils_File
::addTrailingSlash($this->parameters
['uploadDir']) . 'cache';
224 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
226 return $this->browser
;
230 * Get the service for loading code from remotely-available extensions
232 * @return CRM_Extension_Downloader
234 public function getDownloader() {
235 if ($this->downloader
=== NULL) {
236 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
237 $this->downloader
= new CRM_Extension_Downloader(
240 // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
241 CRM_Utils_File
::tempdir()
244 return $this->downloader
;
248 * @return CRM_Extension_MixinLoader;
250 public function getMixinLoader() {
251 if ($this->mixinLoader
=== NULL) {
252 $this->mixinLoader
= new CRM_Extension_MixinLoader();
254 return $this->mixinLoader
;
258 * @return CRM_Utils_Cache_Interface
260 public function getCache() {
261 if ($this->cache
=== NULL) {
262 $cacheGroup = md5(serialize(['ext', $this->parameters
, CRM_Utils_System
::version()]));
263 // Extension system starts before container. Manage our own cache.
264 $this->cache
= CRM_Utils_Cache
::create([
265 'name' => $cacheGroup,
266 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'],
274 * Determine the URL which provides a feed of available extensions.
276 * @return string|FALSE
278 public function getRepositoryUrl() {
279 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
280 $url = Civi
::settings()->get('ext_repo_url');
282 // boolean false means don't try to check extensions
284 if ($url === FALSE) {
285 $this->_repoUrl
= FALSE;
288 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
291 return $this->_repoUrl
;
295 * Returns a list keyed by extension key
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);
303 return Civi
::$statics[__CLASS__
]['compatibility'];
306 public static function isNotTestExtension(CRM_Extension_Info
$info) {
307 return (bool) !preg_match('/^test\./', $info->key
);
311 * Take an extension's raw XML info and add information about the
312 * extension's status on the local system.
314 * The result format resembles the old CRM_Core_Extensions_Extension.
316 * @param CRM_Extension_Info $obj
320 public static function createExtendedInfo(CRM_Extension_Info
$obj) {
321 $mapper = CRM_Extension_System
::singleton()->getMapper();
322 $manager = CRM_Extension_System
::singleton()->getManager();
324 $extensionRow = (array) $obj;
326 $extensionRow['path'] = $mapper->keyToBasePath($obj->key
);
328 catch (CRM_Extension_Exception
$e) {
329 $extensionRow['path'] = '';
331 $extensionRow['status'] = $manager->getStatus($obj->key
);
333 switch ($extensionRow['status']) {
334 case CRM_Extension_Manager
::STATUS_UNINSTALLED
:
335 // ts('Uninstalled');
336 $extensionRow['statusLabel'] = '';
339 case CRM_Extension_Manager
::STATUS_DISABLED
:
340 $extensionRow['statusLabel'] = ts('Disabled');
343 case CRM_Extension_Manager
::STATUS_INSTALLED
:
345 $extensionRow['statusLabel'] = ts('Enabled');
348 case CRM_Extension_Manager
::STATUS_DISABLED_MISSING
:
349 $extensionRow['statusLabel'] = ts('Disabled (Missing)');
352 case CRM_Extension_Manager
::STATUS_INSTALLED_MISSING
:
354 $extensionRow['statusLabel'] = ts('Enabled (Missing)');
358 $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')';
360 if ($manager->isIncompatible($obj->key
)) {
361 $extensionRow['statusLabel'] = ts('Obsolete') . ($extensionRow['statusLabel'] ?
(' - ' . $extensionRow['statusLabel']) : '');
363 return $extensionRow;