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