3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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;
92 * @param array $parameters
93 * List of configuration values required by the extension system.
94 * Missing values will be guessed based on $config.
96 public function __construct($parameters = array()) {
97 $config = CRM_Core_Config
::singleton();
98 $parameters['extensionsDir'] = CRM_Utils_Array
::value('extensionsDir', $parameters, $config->extensionsDir
);
99 $parameters['extensionsURL'] = CRM_Utils_Array
::value('extensionsURL', $parameters, $config->extensionsURL
);
100 $parameters['resourceBase'] = CRM_Utils_Array
::value('resourceBase', $parameters, $config->resourceBase
);
101 $parameters['uploadDir'] = CRM_Utils_Array
::value('uploadDir', $parameters, $config->uploadDir
);
102 $parameters['userFrameworkBaseURL'] = CRM_Utils_Array
::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL
);
103 if (!array_key_exists('civicrm_root', $parameters)) {
104 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
106 if (!array_key_exists('cmsRootPath', $parameters)) {
107 $parameters['cmsRootPath'] = $config->userSystem
->cmsRootPath();
109 if (!array_key_exists('domain_id', $parameters)) {
110 $parameters['domain_id'] = CRM_Core_Config
::domainID();
112 ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters))
114 $this->parameters
= $parameters;
118 * Get a container which represents all available extensions.
120 * @return CRM_Extension_Container_Interface
122 public function getFullContainer() {
123 if ($this->fullContainer
=== NULL) {
124 $containers = array();
126 if ($this->getDefaultContainer()) {
127 $containers['default'] = $this->getDefaultContainer();
130 $containers['civiroot'] = new CRM_Extension_Container_Basic(
131 $this->parameters
['civicrm_root'],
132 $this->parameters
['resourceBase'],
137 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
138 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
139 // TODO: CRM_Extension_Container_Basic( /modules )
140 // TODO: CRM_Extension_Container_Basic( /vendors )
142 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
143 if (NULL !== $this->parameters
['cmsRootPath']) {
144 $vendorPath = $this->parameters
['cmsRootPath'] . DIRECTORY_SEPARATOR
. 'vendor';
145 if (is_dir($vendorPath)) {
146 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
148 CRM_Utils_File
::addTrailingSlash($this->parameters
['userFrameworkBaseURL'], '/') . 'vendor',
155 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
157 return $this->fullContainer
;
161 * Get the container to which new extensions are installed.
163 * This container should be a particular, writeable directory.
165 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
167 public function getDefaultContainer() {
168 if ($this->defaultContainer
=== NULL) {
169 if ($this->parameters
['extensionsDir']) {
170 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
173 $this->defaultContainer
= FALSE;
176 return $this->defaultContainer
;
180 * Get the service which provides runtime information about extensions.
182 * @return CRM_Extension_Mapper
184 public function getMapper() {
185 if ($this->mapper
=== NULL) {
186 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
188 return $this->mapper
;
192 * @return \CRM_Extension_ClassLoader
194 public function getClassLoader() {
195 if ($this->classLoader
=== NULL) {
196 $this->classLoader
= new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager());
198 return $this->classLoader
;
202 * Get the service for enabling and disabling extensions.
204 * @return CRM_Extension_Manager
206 public function getManager() {
207 if ($this->manager
=== NULL) {
208 $typeManagers = array(
209 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
210 'report' => new CRM_Extension_Manager_Report(),
211 'search' => new CRM_Extension_Manager_Search(),
212 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
214 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
216 return $this->manager
;
220 * Get the service for finding remotely-available extensions
222 * @return CRM_Extension_Browser
224 public function getBrowser() {
225 if ($this->browser
=== NULL) {
227 if (!empty($this->parameters
['uploadDir'])) {
228 $cacheDir = CRM_Utils_File
::addTrailingSlash($this->parameters
['uploadDir']) . 'cache';
230 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
232 return $this->browser
;
236 * Get the service for loading code from remotely-available extensions
238 * @return CRM_Extension_Downloader
240 public function getDownloader() {
241 if ($this->downloader
=== NULL) {
242 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
243 $this->downloader
= new CRM_Extension_Downloader(
246 CRM_Utils_File
::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
249 return $this->downloader
;
253 * @return CRM_Utils_Cache_Interface
255 public function getCache() {
256 if ($this->cache
=== NULL) {
257 $cacheGroup = md5(serialize(array('ext', $this->parameters
)));
258 // Extension system starts before container. Manage our own cache.
259 $this->cache
= CRM_Utils_Cache
::create(array(
260 'name' => $cacheGroup,
261 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'),
269 * Determine the URL which provides a feed of available extensions.
271 * @return string|FALSE
273 public function getRepositoryUrl() {
274 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
275 $url = Civi
::settings()->get('ext_repo_url');
277 // boolean false means don't try to check extensions
279 if ($url === FALSE) {
280 $this->_repoUrl
= FALSE;
283 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
286 return $this->_repoUrl
;
290 * Take an extension's raw XML info and add information about the
291 * extension's status on the local system.
293 * The result format resembles the old CRM_Core_Extensions_Extension.
295 * @param CRM_Extension_Info $obj
299 public static function createExtendedInfo(CRM_Extension_Info
$obj) {
300 $mapper = CRM_Extension_System
::singleton()->getMapper();
301 $manager = CRM_Extension_System
::singleton()->getManager();
303 $extensionRow = (array) $obj;
305 $extensionRow['path'] = $mapper->keyToBasePath($obj->key
);
307 catch (CRM_Extension_Exception
$e) {
308 $extensionRow['path'] = '';
310 $extensionRow['status'] = $manager->getStatus($obj->key
);
312 switch ($extensionRow['status']) {
313 case CRM_Extension_Manager
::STATUS_UNINSTALLED
:
314 $extensionRow['statusLabel'] = ''; // ts('Uninstalled');
317 case CRM_Extension_Manager
::STATUS_DISABLED
:
318 $extensionRow['statusLabel'] = ts('Disabled');
321 case CRM_Extension_Manager
::STATUS_INSTALLED
:
322 $extensionRow['statusLabel'] = ts('Enabled'); // ts('Installed');
325 case CRM_Extension_Manager
::STATUS_DISABLED_MISSING
:
326 $extensionRow['statusLabel'] = ts('Disabled (Missing)');
329 case CRM_Extension_Manager
::STATUS_INSTALLED_MISSING
:
330 $extensionRow['statusLabel'] = ts('Enabled (Missing)'); // ts('Installed');
334 $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')';
336 return $extensionRow;