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 proivdes various helper functions for locating extensions | |
14 | * data. It's designed for compatibility with pre-existing functions from | |
15 | * CRM_Core_Extensions. | |
16 | * | |
17 | * Most of these helper functions originate with the first major iteration | |
18 | * of extensions -- a time when every extension had one eponymous PHP class, | |
19 | * when there was no PHP class-loader, and when there was special-case logic | |
20 | * sprinkled around to handle loading of "extension classes". | |
21 | * | |
22 | * With module-extensions (Civi 4.2+), there are no eponymous classes -- | |
23 | * instead, module-extensions follow the same class-naming and class-loading | |
24 | * practices as core (and don't require special-case logic for class | |
25 | * loading). Consequently, the helpers in here aren't much used with | |
26 | * module-extensions. | |
27 | * | |
28 | * @package CRM | |
ca5cec67 | 29 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
30 | */ |
31 | class CRM_Extension_Mapper { | |
32 | ||
33 | /** | |
fe482240 | 34 | * An URL for public extensions repository. |
6a488035 | 35 | */ |
6a488035 TO |
36 | |
37 | /** | |
fe482240 | 38 | * Extension info file name. |
6a488035 TO |
39 | */ |
40 | const EXT_TEMPLATES_DIRNAME = 'templates'; | |
41 | ||
42 | /** | |
43 | * @var CRM_Extension_Container_Interface | |
44 | */ | |
45 | protected $container; | |
46 | ||
47 | /** | |
51dda21e SL |
48 | * @var \CRM_Extension_Info[] |
49 | * (key => CRM_Extension_Info) | |
6a488035 | 50 | */ |
be2fb01f | 51 | protected $infos = []; |
6a488035 TO |
52 | |
53 | /** | |
54 | * @var array | |
55 | */ | |
56 | protected $moduleExtensions = NULL; | |
57 | ||
58 | /** | |
59 | * @var CRM_Utils_Cache_Interface | |
60 | */ | |
61 | protected $cache; | |
62 | ||
63 | protected $cacheKey; | |
64 | ||
65 | protected $civicrmPath; | |
66 | ||
67 | protected $civicrmUrl; | |
68 | ||
e0ef6999 EM |
69 | /** |
70 | * @param CRM_Extension_Container_Interface $container | |
71 | * @param CRM_Utils_Cache_Interface $cache | |
72 | * @param null $cacheKey | |
73 | * @param null $civicrmPath | |
74 | * @param null $civicrmUrl | |
75 | */ | |
6a488035 TO |
76 | public function __construct(CRM_Extension_Container_Interface $container, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $civicrmPath = NULL, $civicrmUrl = NULL) { |
77 | $this->container = $container; | |
78 | $this->cache = $cache; | |
79 | $this->cacheKey = $cacheKey; | |
80 | if ($civicrmUrl) { | |
81 | $this->civicrmUrl = rtrim($civicrmUrl, '/'); | |
0db6c3e1 TO |
82 | } |
83 | else { | |
6a488035 TO |
84 | $config = CRM_Core_Config::singleton(); |
85 | $this->civicrmUrl = rtrim($config->resourceBase, '/'); | |
86 | } | |
87 | if ($civicrmPath) { | |
b3a4b879 | 88 | $this->civicrmPath = rtrim($civicrmPath, '/'); |
0db6c3e1 TO |
89 | } |
90 | else { | |
6a488035 | 91 | global $civicrm_root; |
b3a4b879 | 92 | $this->civicrmPath = rtrim($civicrm_root, '/'); |
6a488035 TO |
93 | } |
94 | } | |
95 | ||
96 | /** | |
97 | * Given the class, provides extension's key. | |
98 | * | |
6a488035 | 99 | * |
f41911fd TO |
100 | * @param string $clazz |
101 | * Extension class name. | |
6a488035 | 102 | * |
a6c01b45 CW |
103 | * @return string |
104 | * name of extension key | |
6a488035 TO |
105 | */ |
106 | public function classToKey($clazz) { | |
107 | return str_replace('_', '.', $clazz); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Given the class, provides extension path. | |
112 | * | |
6a488035 | 113 | * |
6c8f6e67 EM |
114 | * @param $clazz |
115 | * | |
a6c01b45 CW |
116 | * @return string |
117 | * full path the extension .php file | |
6a488035 TO |
118 | */ |
119 | public function classToPath($clazz) { | |
120 | $elements = explode('_', $clazz); | |
121 | $key = implode('.', $elements); | |
122 | return $this->keyToPath($key); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Given the string, returns true or false if it's an extension key. | |
127 | * | |
6a488035 | 128 | * |
f41911fd TO |
129 | * @param string $key |
130 | * A string which might be an extension key. | |
6a488035 | 131 | * |
e7483cbe | 132 | * @return bool |
a6c01b45 | 133 | * true if given string is an extension name |
6a488035 TO |
134 | */ |
135 | public function isExtensionKey($key) { | |
136 | // check if the string is an extension name or the class | |
137 | return (strpos($key, '.') !== FALSE) ? TRUE : FALSE; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Given the string, returns true or false if it's an extension class name. | |
142 | * | |
6a488035 | 143 | * |
f41911fd TO |
144 | * @param string $clazz |
145 | * A string which might be an extension class name. | |
6a488035 | 146 | * |
e7483cbe | 147 | * @return bool |
a6c01b45 | 148 | * true if given string is an extension class name |
6a488035 TO |
149 | */ |
150 | public function isExtensionClass($clazz) { | |
151 | ||
152 | if (substr($clazz, 0, 4) != 'CRM_') { | |
153 | return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz); | |
154 | } | |
155 | return FALSE; | |
156 | } | |
157 | ||
158 | /** | |
f41911fd TO |
159 | * @param string $key |
160 | * Extension fully-qualified-name. | |
77b97be7 EM |
161 | * @param bool $fresh |
162 | * | |
163 | * @throws CRM_Extension_Exception | |
164 | * @throws Exception | |
c490a46a | 165 | * @return CRM_Extension_Info |
6a488035 TO |
166 | */ |
167 | public function keyToInfo($key, $fresh = FALSE) { | |
168 | if ($fresh || !array_key_exists($key, $this->infos)) { | |
169 | try { | |
170 | $this->infos[$key] = CRM_Extension_Info::loadFromFile($this->container->getPath($key) . DIRECTORY_SEPARATOR . CRM_Extension_Info::FILENAME); | |
0db6c3e1 TO |
171 | } |
172 | catch (CRM_Extension_Exception $e) { | |
6a488035 TO |
173 | // file has more detailed info, but we'll fallback to DB if it's missing -- DB has enough info to uninstall |
174 | $this->infos[$key] = CRM_Extension_System::singleton()->getManager()->createInfoFromDB($key); | |
175 | if (!$this->infos[$key]) { | |
176 | throw $e; | |
177 | } | |
178 | } | |
179 | } | |
180 | return $this->infos[$key]; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Given the key, provides extension's class name. | |
185 | * | |
6a488035 | 186 | * |
f41911fd TO |
187 | * @param string $key |
188 | * Extension key. | |
6a488035 | 189 | * |
a6c01b45 CW |
190 | * @return string |
191 | * name of extension's main class | |
6a488035 TO |
192 | */ |
193 | public function keyToClass($key) { | |
194 | return str_replace('.', '_', $key); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Given the key, provides the path to file containing | |
199 | * extension's main class. | |
200 | * | |
6a488035 | 201 | * |
f41911fd TO |
202 | * @param string $key |
203 | * Extension key. | |
6a488035 | 204 | * |
a6c01b45 CW |
205 | * @return string |
206 | * path to file containing extension's main class | |
6a488035 TO |
207 | */ |
208 | public function keyToPath($key) { | |
209 | $info = $this->keyToInfo($key); | |
210 | return $this->container->getPath($key) . DIRECTORY_SEPARATOR . $info->file . '.php'; | |
211 | } | |
212 | ||
213 | /** | |
214 | * Given the key, provides the path to file containing | |
215 | * extension's main class. | |
216 | * | |
f41911fd TO |
217 | * @param string $key |
218 | * Extension key. | |
a6c01b45 CW |
219 | * @return string |
220 | * local path of the extension source tree | |
6a488035 TO |
221 | */ |
222 | public function keyToBasePath($key) { | |
223 | if ($key == 'civicrm') { | |
224 | return $this->civicrmPath; | |
225 | } | |
226 | return $this->container->getPath($key); | |
227 | } | |
228 | ||
229 | /** | |
230 | * Given the key, provides the path to file containing | |
231 | * extension's main class. | |
232 | * | |
6a488035 | 233 | * |
f41911fd TO |
234 | * @param string $key |
235 | * Extension key. | |
6a488035 | 236 | * |
a6c01b45 CW |
237 | * @return string |
238 | * url for resources in this extension | |
6a488035 TO |
239 | */ |
240 | public function keyToUrl($key) { | |
241 | if ($key == 'civicrm') { | |
dee7a2b1 PJ |
242 | // CRM-12130 Workaround: If the domain's config_backend is NULL at the start of the request, |
243 | // then the Mapper is wrongly constructed with an empty value for $this->civicrmUrl. | |
3d4a4ccf PJ |
244 | if (empty($this->civicrmUrl)) { |
245 | $config = CRM_Core_Config::singleton(); | |
246 | return rtrim($config->resourceBase, '/'); | |
247 | } | |
6a488035 TO |
248 | return $this->civicrmUrl; |
249 | } | |
250 | ||
251 | return $this->container->getResUrl($key); | |
252 | } | |
253 | ||
254 | /** | |
255 | * Fetch the list of active extensions of type 'module' | |
256 | * | |
5a4f6742 CW |
257 | * @param bool $fresh |
258 | * whether to forcibly reload extensions list from canonical store. | |
a6c01b45 | 259 | * @return array |
0e092fe0 | 260 | * array(array('prefix' => $, 'fullName' => $, 'filePath' => $)) |
6a488035 TO |
261 | */ |
262 | public function getActiveModuleFiles($fresh = FALSE) { | |
62f662b0 | 263 | if (!defined('CIVICRM_DSN')) { |
264 | // hmm, ok | |
265 | return []; | |
6a488035 TO |
266 | } |
267 | ||
0e092fe0 HD |
268 | // The list of module files is cached in two tiers. The tiers are slightly |
269 | // different: | |
270 | // | |
271 | // 1. The persistent tier (cache) stores | |
272 | // names WITHOUT absolute paths. | |
273 | // 2. The ephemeral/thread-local tier (statics) stores names | |
274 | // WITH absolute paths. | |
275 | // Return static value instead of re-running query | |
24ef6b49 | 276 | if (isset(Civi::$statics[__CLASS__]['moduleExtensions']) && !$fresh) { |
0e092fe0 HD |
277 | return Civi::$statics[__CLASS__]['moduleExtensions']; |
278 | } | |
279 | ||
6a488035 | 280 | $moduleExtensions = NULL; |
0e092fe0 HD |
281 | |
282 | // Checked if it's stored in the persistent cache. | |
6a488035 | 283 | if ($this->cache && !$fresh) { |
ec2dd0bd | 284 | $moduleExtensions = $this->cache->get($this->cacheKey . '_moduleFiles'); |
6a488035 TO |
285 | } |
286 | ||
0e092fe0 | 287 | // If cache is empty we build it from database. |
6a488035 | 288 | if (!is_array($moduleExtensions)) { |
6542d699 TO |
289 | $compat = CRM_Extension_System::getCompatibilityInfo(); |
290 | ||
6a488035 | 291 | // Check canonical module list |
be2fb01f | 292 | $moduleExtensions = []; |
6a488035 TO |
293 | $sql = ' |
294 | SELECT full_name, file | |
295 | FROM civicrm_extension | |
296 | WHERE is_active = 1 | |
297 | AND type = "module" | |
298 | '; | |
299 | $dao = CRM_Core_DAO::executeQuery($sql); | |
300 | while ($dao->fetch()) { | |
6542d699 TO |
301 | if (!empty($compat[$dao->full_name]['force-uninstall'])) { |
302 | continue; | |
303 | } | |
0851bac5 HD |
304 | $moduleExtensions[] = [ |
305 | 'prefix' => $dao->file, | |
306 | 'fullName' => $dao->full_name, | |
307 | 'filePath' => NULL, | |
308 | ]; | |
6a488035 TO |
309 | } |
310 | ||
311 | if ($this->cache) { | |
ec2dd0bd | 312 | $this->cache->set($this->cacheKey . '_moduleFiles', $moduleExtensions); |
6a488035 TO |
313 | } |
314 | } | |
9194bdb3 HD |
315 | |
316 | // Since we're not caching the full path we add it now. | |
317 | array_walk($moduleExtensions, function(&$value, $key) { | |
0851bac5 | 318 | try { |
456e9097 HD |
319 | if (!$value['filePath']) { |
320 | $value['filePath'] = $this->keyToPath($value['fullName']); | |
321 | } | |
0851bac5 HD |
322 | } |
323 | catch (CRM_Extension_Exception $e) { | |
324 | // Putting a stub here provides more consistency | |
325 | // in how getActiveModuleFiles when racing between | |
326 | // dirty file-removals and cache-clears. | |
327 | CRM_Core_Session::setStatus($e->getMessage(), '', 'error'); | |
328 | $value['filePath'] = NULL; | |
329 | } | |
9194bdb3 HD |
330 | }); |
331 | ||
0e092fe0 HD |
332 | Civi::$statics[__CLASS__]['moduleExtensions'] = $moduleExtensions; |
333 | ||
6a488035 TO |
334 | return $moduleExtensions; |
335 | } | |
336 | ||
e7ff7042 | 337 | /** |
fe482240 | 338 | * Get a list of base URLs for all active modules. |
e7ff7042 | 339 | * |
a6c01b45 CW |
340 | * @return array |
341 | * (string $extKey => string $baseUrl) | |
e7ff7042 TO |
342 | */ |
343 | public function getActiveModuleUrls() { | |
344 | // TODO optimization/caching | |
be2fb01f | 345 | $urls = []; |
e7ff7042 TO |
346 | $urls['civicrm'] = $this->keyToUrl('civicrm'); |
347 | foreach ($this->getModules() as $module) { | |
348 | /** @var $module CRM_Core_Module */ | |
349 | if ($module->is_active) { | |
350 | $urls[$module->name] = $this->keyToUrl($module->name); | |
351 | } | |
352 | } | |
353 | return $urls; | |
354 | } | |
355 | ||
3b3f6d23 TO |
356 | /** |
357 | * Get a list of extension keys, filtered by the corresponding file path. | |
358 | * | |
359 | * @param string $pattern | |
360 | * A file path. To search subdirectories, append "*". | |
361 | * Ex: "/var/www/extensions/*" | |
362 | * Ex: "/var/www/extensions/org.foo.bar" | |
363 | * @return array | |
364 | * Array(string $key). | |
365 | * Ex: array("org.foo.bar"). | |
366 | */ | |
367 | public function getKeysByPath($pattern) { | |
be2fb01f | 368 | $keys = []; |
3b3f6d23 TO |
369 | |
370 | if (CRM_Utils_String::endsWith($pattern, '*')) { | |
371 | $prefix = rtrim($pattern, '*'); | |
372 | foreach ($this->container->getKeys() as $key) { | |
373 | $path = CRM_Utils_File::addTrailingSlash($this->container->getPath($key)); | |
374 | if (realpath($prefix) == realpath($path) || CRM_Utils_File::isChildPath($prefix, $path)) { | |
375 | $keys[] = $key; | |
376 | } | |
377 | } | |
378 | } | |
379 | else { | |
380 | foreach ($this->container->getKeys() as $key) { | |
381 | $path = CRM_Utils_File::addTrailingSlash($this->container->getPath($key)); | |
382 | if (realpath($pattern) == realpath($path)) { | |
383 | $keys[] = $key; | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
388 | return $keys; | |
389 | } | |
390 | ||
f8a7cfff TO |
391 | /** |
392 | * @return array | |
393 | * Ex: $result['org.civicrm.foobar'] = new CRM_Extension_Info(...). | |
394 | * @throws \CRM_Extension_Exception | |
395 | * @throws \Exception | |
396 | */ | |
397 | public function getAllInfos() { | |
398 | foreach ($this->container->getKeys() as $key) { | |
399 | $this->keyToInfo($key); | |
400 | } | |
401 | return $this->infos; | |
402 | } | |
403 | ||
e0ef6999 | 404 | /** |
100fef9d | 405 | * @param string $name |
e0ef6999 EM |
406 | * |
407 | * @return bool | |
408 | */ | |
6a488035 TO |
409 | public function isActiveModule($name) { |
410 | $activeModules = $this->getActiveModuleFiles(); | |
411 | foreach ($activeModules as $activeModule) { | |
412 | if ($activeModule['prefix'] == $name) { | |
413 | return TRUE; | |
414 | } | |
415 | } | |
416 | return FALSE; | |
417 | } | |
418 | ||
419 | /** | |
420 | * Get a list of all installed modules, including enabled and disabled ones | |
421 | * | |
a6c01b45 CW |
422 | * @return array |
423 | * CRM_Core_Module | |
6a488035 TO |
424 | */ |
425 | public function getModules() { | |
be2fb01f | 426 | $result = []; |
6a488035 TO |
427 | $dao = new CRM_Core_DAO_Extension(); |
428 | $dao->type = 'module'; | |
429 | $dao->find(); | |
430 | while ($dao->fetch()) { | |
431 | $result[] = new CRM_Core_Module($dao->full_name, $dao->is_active); | |
432 | } | |
433 | return $result; | |
434 | } | |
435 | ||
436 | /** | |
437 | * Given the class, provides the template path. | |
438 | * | |
6a488035 | 439 | * |
f41911fd TO |
440 | * @param string $clazz |
441 | * Extension class name. | |
6a488035 | 442 | * |
a6c01b45 CW |
443 | * @return string |
444 | * path to extension's templates directory | |
6a488035 TO |
445 | */ |
446 | public function getTemplatePath($clazz) { | |
447 | $path = $this->container->getPath($this->classToKey($clazz)); | |
448 | return $path . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME; | |
449 | /* | |
450 | $path = $this->classToPath($clazz); | |
451 | $pathElm = explode(DIRECTORY_SEPARATOR, $path); | |
452 | array_pop($pathElm); | |
453 | return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME; | |
e70a7fc0 | 454 | */ |
6a488035 TO |
455 | } |
456 | ||
457 | /** | |
458 | * Given te class, provides the template name. | |
459 | * @todo consider multiple templates, support for one template for now | |
460 | * | |
6a488035 | 461 | * |
f41911fd TO |
462 | * @param string $clazz |
463 | * Extension class name. | |
6a488035 | 464 | * |
a6c01b45 CW |
465 | * @return string |
466 | * extension's template name | |
6a488035 TO |
467 | */ |
468 | public function getTemplateName($clazz) { | |
469 | $info = $this->keyToInfo($this->classToKey($clazz)); | |
470 | return (string) $info->file . '.tpl'; | |
471 | } | |
472 | ||
473 | public function refresh() { | |
be2fb01f | 474 | $this->infos = []; |
6a488035 TO |
475 | $this->moduleExtensions = NULL; |
476 | if ($this->cache) { | |
ec2dd0bd | 477 | $this->cache->delete($this->cacheKey . '_moduleFiles'); |
6a488035 | 478 | } |
85c7eb67 TO |
479 | // FIXME: How can code so code wrong be so right? |
480 | CRM_Extension_System::singleton()->getClassLoader()->refresh(); | |
6a488035 | 481 | } |
96025800 | 482 | |
6a488035 | 483 | } |