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;
31 * @var CRM_Extension_ClassLoader
36 * The URL of the remote extensions repository.
40 private $_repoUrl = NULL;
44 * Construction parameters. These are primarily retained so
45 * that they can influence the cache name.
47 protected $parameters;
51 * TRUE to force creation of a new system.
53 * @return CRM_Extension_System
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
);
61 self
::$singleton = new CRM_Extension_System();
64 return self
::$singleton;
68 * @param CRM_Extension_System $singleton
69 * The new, singleton extension system.
71 public static function setSingleton(CRM_Extension_System
$singleton) {
72 self
::$singleton = $singleton;
78 * @param array $parameters
79 * List of configuration values required by the extension system.
80 * Missing values will be guessed based on $config.
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'];
92 if (!array_key_exists('cmsRootPath', $parameters)) {
93 $parameters['cmsRootPath'] = $config->userSystem
->cmsRootPath();
95 if (!array_key_exists('domain_id', $parameters)) {
96 $parameters['domain_id'] = CRM_Core_Config
::domainID();
98 // guaranteed ordering - useful for md5(serialize($parameters))
101 $this->parameters
= $parameters;
105 * Get a container which represents all available extensions.
107 * @return CRM_Extension_Container_Interface
109 public function getFullContainer() {
110 if ($this->fullContainer
=== NULL) {
113 if ($this->getDefaultContainer()) {
114 $containers['default'] = $this->getDefaultContainer();
117 $containers['civiroot'] = new CRM_Extension_Container_Basic(
118 $this->parameters
['civicrm_root'],
119 $this->parameters
['resourceBase'],
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 )
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(
135 CRM_Utils_File
::addTrailingSlash($this->parameters
['userFrameworkBaseURL'], '/') . 'vendor',
142 if (!defined('CIVICRM_TEST')) {
143 foreach ($containers as $container) {
144 $container->addFilter([__CLASS__
, 'isNotTestExtension']);
148 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
150 return $this->fullContainer
;
154 * Get the container to which new extensions are installed.
156 * This container should be a particular, writeable directory.
158 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
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');
166 $this->defaultContainer
= FALSE;
169 return $this->defaultContainer
;
173 * Get the service which provides runtime information about extensions.
175 * @return CRM_Extension_Mapper
177 public function getMapper() {
178 if ($this->mapper
=== NULL) {
179 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
181 return $this->mapper
;
185 * @return \CRM_Extension_ClassLoader
187 public function getClassLoader() {
188 if ($this->classLoader
=== NULL) {
189 $this->classLoader
= new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager());
191 return $this->classLoader
;
195 * Get the service for enabling and disabling extensions.
197 * @return CRM_Extension_Manager
199 public function getManager() {
200 if ($this->manager
=== NULL) {
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()),
207 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
209 return $this->manager
;
213 * Get the service for finding remotely-available extensions
215 * @return CRM_Extension_Browser
217 public function getBrowser() {
218 if ($this->browser
=== NULL) {
220 if (!empty($this->parameters
['uploadDir'])) {
221 $cacheDir = CRM_Utils_File
::addTrailingSlash($this->parameters
['uploadDir']) . 'cache';
223 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
225 return $this->browser
;
229 * Get the service for loading code from remotely-available extensions
231 * @return CRM_Extension_Downloader
233 public function getDownloader() {
234 if ($this->downloader
=== NULL) {
235 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
236 $this->downloader
= new CRM_Extension_Downloader(
239 // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
240 CRM_Utils_File
::tempdir()
243 return $this->downloader
;
247 * @return CRM_Utils_Cache_Interface
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'],
263 * Determine the URL which provides a feed of available extensions.
265 * @return string|FALSE
267 public function getRepositoryUrl() {
268 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
269 $url = Civi
::settings()->get('ext_repo_url');
271 // boolean false means don't try to check extensions
273 if ($url === FALSE) {
274 $this->_repoUrl
= FALSE;
277 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
280 return $this->_repoUrl
;
284 * Returns a list keyed by extension key
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);
292 return Civi
::$statics[__CLASS__
]['compatibility'];
295 public static function isNotTestExtension(CRM_Extension_Info
$info) {
296 return (bool) !preg_match('/^test\./', $info->key
);
300 * Take an extension's raw XML info and add information about the
301 * extension's status on the local system.
303 * The result format resembles the old CRM_Core_Extensions_Extension.
305 * @param CRM_Extension_Info $obj
309 public static function createExtendedInfo(CRM_Extension_Info
$obj) {
310 $mapper = CRM_Extension_System
::singleton()->getMapper();
311 $manager = CRM_Extension_System
::singleton()->getManager();
313 $extensionRow = (array) $obj;
315 $extensionRow['path'] = $mapper->keyToBasePath($obj->key
);
317 catch (CRM_Extension_Exception
$e) {
318 $extensionRow['path'] = '';
320 $extensionRow['status'] = $manager->getStatus($obj->key
);
322 switch ($extensionRow['status']) {
323 case CRM_Extension_Manager
::STATUS_UNINSTALLED
:
324 // ts('Uninstalled');
325 $extensionRow['statusLabel'] = '';
328 case CRM_Extension_Manager
::STATUS_DISABLED
:
329 $extensionRow['statusLabel'] = ts('Disabled');
332 case CRM_Extension_Manager
::STATUS_INSTALLED
:
334 $extensionRow['statusLabel'] = ts('Enabled');
337 case CRM_Extension_Manager
::STATUS_DISABLED_MISSING
:
338 $extensionRow['statusLabel'] = ts('Disabled (Missing)');
341 case CRM_Extension_Manager
::STATUS_INSTALLED_MISSING
:
343 $extensionRow['statusLabel'] = ts('Enabled (Missing)');
347 $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')';
349 if ($manager->isIncompatible($obj->key
)) {
350 $extensionRow['statusLabel'] = ts('Obsolete') . ($extensionRow['statusLabel'] ?
(' - ' . $extensionRow['statusLabel']) : '');
352 return $extensionRow;