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