freeze the contact field in non standalone context
[civicrm-core.git] / CRM / Extension / System.php
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 */
35 class 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 * Class constructor.
93 *
94 * @param array $parameters
95 * List of configuration values required by the extension system.
96 * Missing values will be guessed based on $config.
97 */
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'];
107 }
108 if (!array_key_exists('cmsRootPath', $parameters)) {
109 $parameters['cmsRootPath'] = $config->userSystem->cmsRootPath();
110 }
111 if (!array_key_exists('domain_id', $parameters)) {
112 $parameters['domain_id'] = CRM_Core_Config::domainID();
113 }
114 // guaranteed ordering - useful for md5(serialize($parameters))
115 ksort($parameters);
116
117 $this->parameters = $parameters;
118 }
119
120 /**
121 * Get a container which represents all available extensions.
122 *
123 * @return CRM_Extension_Container_Interface
124 */
125 public function getFullContainer() {
126 if ($this->fullContainer === NULL) {
127 $containers = [];
128
129 if ($this->getDefaultContainer()) {
130 $containers['default'] = $this->getDefaultContainer();
131 }
132
133 $containers['civiroot'] = new CRM_Extension_Container_Basic(
134 $this->parameters['civicrm_root'],
135 $this->parameters['resourceBase'],
136 $this->getCache(),
137 'civiroot'
138 );
139
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 )
144
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(
150 $vendorPath,
151 CRM_Utils_File::addTrailingSlash($this->parameters['userFrameworkBaseURL'], '/') . 'vendor',
152 $this->getCache(),
153 'cmsvendor'
154 );
155 }
156 }
157
158 if (!defined('CIVICRM_TEST')) {
159 foreach ($containers as $container) {
160 $container->addFilter([__CLASS__, 'isNotTestExtension']);
161 }
162 }
163
164 $this->fullContainer = new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
165 }
166 return $this->fullContainer;
167 }
168
169 /**
170 * Get the container to which new extensions are installed.
171 *
172 * This container should be a particular, writeable directory.
173 *
174 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
175 */
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');
180 }
181 else {
182 $this->defaultContainer = FALSE;
183 }
184 }
185 return $this->defaultContainer;
186 }
187
188 /**
189 * Get the service which provides runtime information about extensions.
190 *
191 * @return CRM_Extension_Mapper
192 */
193 public function getMapper() {
194 if ($this->mapper === NULL) {
195 $this->mapper = new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
196 }
197 return $this->mapper;
198 }
199
200 /**
201 * @return \CRM_Extension_ClassLoader
202 */
203 public function getClassLoader() {
204 if ($this->classLoader === NULL) {
205 $this->classLoader = new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager());
206 }
207 return $this->classLoader;
208 }
209
210 /**
211 * Get the service for enabling and disabling extensions.
212 *
213 * @return CRM_Extension_Manager
214 */
215 public function getManager() {
216 if ($this->manager === NULL) {
217 $typeManagers = [
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()),
222 ];
223 $this->manager = new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
224 }
225 return $this->manager;
226 }
227
228 /**
229 * Get the service for finding remotely-available extensions
230 *
231 * @return CRM_Extension_Browser
232 */
233 public function getBrowser() {
234 if ($this->browser === NULL) {
235 $cacheDir = NULL;
236 if (!empty($this->parameters['uploadDir'])) {
237 $cacheDir = CRM_Utils_File::addTrailingSlash($this->parameters['uploadDir']) . 'cache';
238 }
239 $this->browser = new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
240 }
241 return $this->browser;
242 }
243
244 /**
245 * Get the service for loading code from remotely-available extensions
246 *
247 * @return CRM_Extension_Downloader
248 */
249 public function getDownloader() {
250 if ($this->downloader === NULL) {
251 $basedir = ($this->getDefaultContainer() ? $this->getDefaultContainer()->getBaseDir() : NULL);
252 $this->downloader = new CRM_Extension_Downloader(
253 $this->getManager(),
254 $basedir,
255 // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
256 CRM_Utils_File::tempdir()
257 );
258 }
259 return $this->downloader;
260 }
261
262 /**
263 * @return CRM_Utils_Cache_Interface
264 */
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'],
272 'prefetch' => TRUE,
273 ]);
274 }
275 return $this->cache;
276 }
277
278 /**
279 * Determine the URL which provides a feed of available extensions.
280 *
281 * @return string|FALSE
282 */
283 public function getRepositoryUrl() {
284 if (empty($this->_repoUrl) && $this->_repoUrl !== FALSE) {
285 $url = Civi::settings()->get('ext_repo_url');
286
287 // boolean false means don't try to check extensions
288 // CRM-10575
289 if ($url === FALSE) {
290 $this->_repoUrl = FALSE;
291 }
292 else {
293 $this->_repoUrl = CRM_Utils_System::evalUrl($url);
294 }
295 }
296 return $this->_repoUrl;
297 }
298
299 /**
300 * Returns a list keyed by extension key
301 *
302 * @return array
303 */
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);
307 }
308 return Civi::$statics[__CLASS__]['compatibility'];
309 }
310
311 public static function isNotTestExtension(CRM_Extension_Info $info) {
312 return (bool) !preg_match('/^test\./', $info->key);
313 }
314
315 /**
316 * Take an extension's raw XML info and add information about the
317 * extension's status on the local system.
318 *
319 * The result format resembles the old CRM_Core_Extensions_Extension.
320 *
321 * @param CRM_Extension_Info $obj
322 *
323 * @return array
324 */
325 public static function createExtendedInfo(CRM_Extension_Info $obj) {
326 $mapper = CRM_Extension_System::singleton()->getMapper();
327 $manager = CRM_Extension_System::singleton()->getManager();
328
329 $extensionRow = (array) $obj;
330 try {
331 $extensionRow['path'] = $mapper->keyToBasePath($obj->key);
332 }
333 catch (CRM_Extension_Exception $e) {
334 $extensionRow['path'] = '';
335 }
336 $extensionRow['status'] = $manager->getStatus($obj->key);
337
338 switch ($extensionRow['status']) {
339 case CRM_Extension_Manager::STATUS_UNINSTALLED:
340 // ts('Uninstalled');
341 $extensionRow['statusLabel'] = '';
342 break;
343
344 case CRM_Extension_Manager::STATUS_DISABLED:
345 $extensionRow['statusLabel'] = ts('Disabled');
346 break;
347
348 case CRM_Extension_Manager::STATUS_INSTALLED:
349 // ts('Installed');
350 $extensionRow['statusLabel'] = ts('Enabled');
351 break;
352
353 case CRM_Extension_Manager::STATUS_DISABLED_MISSING:
354 $extensionRow['statusLabel'] = ts('Disabled (Missing)');
355 break;
356
357 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
358 // ts('Installed');
359 $extensionRow['statusLabel'] = ts('Enabled (Missing)');
360 break;
361
362 default:
363 $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')';
364 }
365 return $extensionRow;
366 }
367
368 }