3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This class glues together the various parts of the extension
33 * @copyright CiviCRM LLC (c) 2004-2019
35 class CRM_Extension_System
{
36 private static $singleton;
38 private $cache = NULL;
39 private $fullContainer = NULL;
40 private $defaultContainer = NULL;
41 private $mapper = NULL;
42 private $manager = NULL;
43 private $browser = NULL;
44 private $downloader = NULL;
47 * @var CRM_Extension_ClassLoader
52 * The URL of the remote extensions repository.
56 private $_repoUrl = NULL;
60 * Construction parameters. These are primarily retained so
61 * that they can influence the cache name.
63 protected $parameters;
67 * TRUE to force creation of a new system.
69 * @return CRM_Extension_System
71 public static function singleton($fresh = FALSE) {
72 if (!self
::$singleton ||
$fresh) {
73 if (self
::$singleton) {
74 self
::$singleton = new CRM_Extension_System(self
::$singleton->parameters
);
77 self
::$singleton = new CRM_Extension_System();
80 return self
::$singleton;
84 * @param CRM_Extension_System $singleton
85 * The new, singleton extension system.
87 public static function setSingleton(CRM_Extension_System
$singleton) {
88 self
::$singleton = $singleton;
94 * @param array $parameters
95 * List of configuration values required by the extension system.
96 * Missing values will be guessed based on $config.
98 public function __construct($parameters = []) {
99 $config = CRM_Core_Config
::singleton();
100 $parameters['extensionsDir'] = CRM_Utils_Array
::value('extensionsDir', $parameters, $config->extensionsDir
);
101 $parameters['extensionsURL'] = CRM_Utils_Array
::value('extensionsURL', $parameters, $config->extensionsURL
);
102 $parameters['resourceBase'] = CRM_Utils_Array
::value('resourceBase', $parameters, $config->resourceBase
);
103 $parameters['uploadDir'] = CRM_Utils_Array
::value('uploadDir', $parameters, $config->uploadDir
);
104 $parameters['userFrameworkBaseURL'] = CRM_Utils_Array
::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL
);
105 if (!array_key_exists('civicrm_root', $parameters)) {
106 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
108 if (!array_key_exists('cmsRootPath', $parameters)) {
109 $parameters['cmsRootPath'] = $config->userSystem
->cmsRootPath();
111 if (!array_key_exists('domain_id', $parameters)) {
112 $parameters['domain_id'] = CRM_Core_Config
::domainID();
114 // guaranteed ordering - useful for md5(serialize($parameters))
117 $this->parameters
= $parameters;
121 * Get a container which represents all available extensions.
123 * @return CRM_Extension_Container_Interface
125 public function getFullContainer() {
126 if ($this->fullContainer
=== NULL) {
129 if ($this->getDefaultContainer()) {
130 $containers['default'] = $this->getDefaultContainer();
133 $containers['civiroot'] = new CRM_Extension_Container_Basic(
134 $this->parameters
['civicrm_root'],
135 $this->parameters
['resourceBase'],
140 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
141 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
142 // TODO: CRM_Extension_Container_Basic( /modules )
143 // TODO: CRM_Extension_Container_Basic( /vendors )
145 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
146 if (NULL !== $this->parameters
['cmsRootPath']) {
147 $vendorPath = $this->parameters
['cmsRootPath'] . DIRECTORY_SEPARATOR
. 'vendor';
148 if (is_dir($vendorPath)) {
149 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
151 CRM_Utils_File
::addTrailingSlash($this->parameters
['userFrameworkBaseURL'], '/') . 'vendor',
158 if (!defined('CIVICRM_TEST')) {
159 foreach ($containers as $container) {
160 $container->addFilter([__CLASS__
, 'isNotTestExtension']);
164 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
166 return $this->fullContainer
;
170 * Get the container to which new extensions are installed.
172 * This container should be a particular, writeable directory.
174 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
176 public function getDefaultContainer() {
177 if ($this->defaultContainer
=== NULL) {
178 if ($this->parameters
['extensionsDir']) {
179 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
182 $this->defaultContainer
= FALSE;
185 return $this->defaultContainer
;
189 * Get the service which provides runtime information about extensions.
191 * @return CRM_Extension_Mapper
193 public function getMapper() {
194 if ($this->mapper
=== NULL) {
195 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
197 return $this->mapper
;
201 * @return \CRM_Extension_ClassLoader
203 public function getClassLoader() {
204 if ($this->classLoader
=== NULL) {
205 $this->classLoader
= new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager());
207 return $this->classLoader
;
211 * Get the service for enabling and disabling extensions.
213 * @return CRM_Extension_Manager
215 public function getManager() {
216 if ($this->manager
=== NULL) {
218 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
219 'report' => new CRM_Extension_Manager_Report(),
220 'search' => new CRM_Extension_Manager_Search(),
221 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
223 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
225 return $this->manager
;
229 * Get the service for finding remotely-available extensions
231 * @return CRM_Extension_Browser
233 public function getBrowser() {
234 if ($this->browser
=== NULL) {
236 if (!empty($this->parameters
['uploadDir'])) {
237 $cacheDir = CRM_Utils_File
::addTrailingSlash($this->parameters
['uploadDir']) . 'cache';
239 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
241 return $this->browser
;
245 * Get the service for loading code from remotely-available extensions
247 * @return CRM_Extension_Downloader
249 public function getDownloader() {
250 if ($this->downloader
=== NULL) {
251 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
252 $this->downloader
= new CRM_Extension_Downloader(
255 // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
256 CRM_Utils_File
::tempdir()
259 return $this->downloader
;
263 * @return CRM_Utils_Cache_Interface
265 public function getCache() {
266 if ($this->cache
=== NULL) {
267 $cacheGroup = md5(serialize(['ext', $this->parameters
]));
268 // Extension system starts before container. Manage our own cache.
269 $this->cache
= CRM_Utils_Cache
::create([
270 'name' => $cacheGroup,
271 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'],
279 * Determine the URL which provides a feed of available extensions.
281 * @return string|FALSE
283 public function getRepositoryUrl() {
284 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
285 $url = Civi
::settings()->get('ext_repo_url');
287 // boolean false means don't try to check extensions
289 if ($url === FALSE) {
290 $this->_repoUrl
= FALSE;
293 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
296 return $this->_repoUrl
;
300 * Returns a list keyed by extension key
304 public static function getCompatibilityInfo() {
305 if (!isset(Civi
::$statics[__CLASS__
]['compatibility'])) {
306 Civi
::$statics[__CLASS__
]['compatibility'] = json_decode(file_get_contents(Civi
::paths()->getPath('[civicrm.root]/extension-compatibility.json')), TRUE);
308 return Civi
::$statics[__CLASS__
]['compatibility'];
311 public static function isNotTestExtension(CRM_Extension_Info
$info) {
312 return (bool) !preg_match('/^test\./', $info->key
);
316 * Take an extension's raw XML info and add information about the
317 * extension's status on the local system.
319 * The result format resembles the old CRM_Core_Extensions_Extension.
321 * @param CRM_Extension_Info $obj
325 public static function createExtendedInfo(CRM_Extension_Info
$obj) {
326 $mapper = CRM_Extension_System
::singleton()->getMapper();
327 $manager = CRM_Extension_System
::singleton()->getManager();
329 $extensionRow = (array) $obj;
331 $extensionRow['path'] = $mapper->keyToBasePath($obj->key
);
333 catch (CRM_Extension_Exception
$e) {
334 $extensionRow['path'] = '';
336 $extensionRow['status'] = $manager->getStatus($obj->key
);
338 switch ($extensionRow['status']) {
339 case CRM_Extension_Manager
::STATUS_UNINSTALLED
:
340 // ts('Uninstalled');
341 $extensionRow['statusLabel'] = '';
344 case CRM_Extension_Manager
::STATUS_DISABLED
:
345 $extensionRow['statusLabel'] = ts('Disabled');
348 case CRM_Extension_Manager
::STATUS_INSTALLED
:
350 $extensionRow['statusLabel'] = ts('Enabled');
353 case CRM_Extension_Manager
::STATUS_DISABLED_MISSING
:
354 $extensionRow['statusLabel'] = ts('Disabled (Missing)');
357 case CRM_Extension_Manager
::STATUS_INSTALLED_MISSING
:
359 $extensionRow['statusLabel'] = ts('Enabled (Missing)');
363 $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')';
365 return $extensionRow;