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