3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This class proivdes various helper functions for locating extensions
30 * data. It's designed for compatibility with pre-existing functions from
31 * CRM_Core_Extensions.
33 * Most of these helper functions originate with the first major iteration
34 * of extensions -- a time when every extension had one eponymous PHP class,
35 * when there was no PHP class-loader, and when there was special-case logic
36 * sprinkled around to handle loading of "extension classes".
38 * With module-extensions (Civi 4.2+), there are no eponymous classes --
39 * instead, module-extensions follow the same class-naming and class-loading
40 * practices as core (and don't require special-case logic for class
41 * loading). Consequently, the helpers in here aren't much used with
45 * @copyright CiviCRM LLC (c) 2004-2015
49 class CRM_Extension_Mapper
{
52 * An URL for public extensions repository.
54 //const DEFAULT_EXTENSIONS_REPOSITORY = 'http://civicrm.org/extdir/ver={ver}|cms={uf}';
57 * Extension info file name.
59 const EXT_TEMPLATES_DIRNAME
= 'templates';
62 * @var CRM_Extension_Container_Interface
67 * @var array (key => CRM_Extension_Info)
69 protected $infos = array();
74 protected $moduleExtensions = NULL;
77 * @var CRM_Utils_Cache_Interface
83 protected $civicrmPath;
85 protected $civicrmUrl;
88 * @param CRM_Extension_Container_Interface $container
89 * @param CRM_Utils_Cache_Interface $cache
90 * @param null $cacheKey
91 * @param null $civicrmPath
92 * @param null $civicrmUrl
94 public function __construct(CRM_Extension_Container_Interface
$container, CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL, $civicrmPath = NULL, $civicrmUrl = NULL) {
95 $this->container
= $container;
96 $this->cache
= $cache;
97 $this->cacheKey
= $cacheKey;
99 $this->civicrmUrl
= rtrim($civicrmUrl, '/');
102 $config = CRM_Core_Config
::singleton();
103 $this->civicrmUrl
= rtrim($config->resourceBase
, '/');
106 $this->civicrmPath
= rtrim($civicrmPath, '/');
109 global $civicrm_root;
110 $this->civicrmPath
= rtrim($civicrm_root, '/');
115 * Given the class, provides extension's key.
118 * @param string $clazz
119 * Extension class name.
122 * name of extension key
124 public function classToKey($clazz) {
125 return str_replace('_', '.', $clazz);
129 * Given the class, provides extension path.
135 * full path the extension .php file
137 public function classToPath($clazz) {
138 $elements = explode('_', $clazz);
139 $key = implode('.', $elements);
140 return $this->keyToPath($key);
144 * Given the string, returns true or false if it's an extension key.
148 * A string which might be an extension key.
151 * true if given string is an extension name
153 public function isExtensionKey($key) {
154 // check if the string is an extension name or the class
155 return (strpos($key, '.') !== FALSE) ?
TRUE : FALSE;
159 * Given the string, returns true or false if it's an extension class name.
162 * @param string $clazz
163 * A string which might be an extension class name.
166 * true if given string is an extension class name
168 public function isExtensionClass($clazz) {
170 if (substr($clazz, 0, 4) != 'CRM_') {
171 return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz);
178 * Extension fully-qualified-name.
181 * @throws CRM_Extension_Exception
183 * @return CRM_Extension_Info
185 public function keyToInfo($key, $fresh = FALSE) {
186 if ($fresh ||
!array_key_exists($key, $this->infos
)) {
188 $this->infos
[$key] = CRM_Extension_Info
::loadFromFile($this->container
->getPath($key) . DIRECTORY_SEPARATOR
. CRM_Extension_Info
::FILENAME
);
190 catch (CRM_Extension_Exception
$e) {
191 // file has more detailed info, but we'll fallback to DB if it's missing -- DB has enough info to uninstall
192 $this->infos
[$key] = CRM_Extension_System
::singleton()->getManager()->createInfoFromDB($key);
193 if (!$this->infos
[$key]) {
198 return $this->infos
[$key];
202 * Given the key, provides extension's class name.
209 * name of extension's main class
211 public function keyToClass($key) {
212 return str_replace('.', '_', $key);
216 * Given the key, provides the path to file containing
217 * extension's main class.
224 * path to file containing extension's main class
226 public function keyToPath($key) {
227 $info = $this->keyToInfo($key);
228 return $this->container
->getPath($key) . DIRECTORY_SEPARATOR
. $info->file
. '.php';
232 * Given the key, provides the path to file containing
233 * extension's main class.
238 * local path of the extension source tree
240 public function keyToBasePath($key) {
241 if ($key == 'civicrm') {
242 return $this->civicrmPath
;
244 return $this->container
->getPath($key);
248 * Given the key, provides the path to file containing
249 * extension's main class.
256 * url for resources in this extension
258 public function keyToUrl($key) {
259 if ($key == 'civicrm') {
260 // CRM-12130 Workaround: If the domain's config_backend is NULL at the start of the request,
261 // then the Mapper is wrongly constructed with an empty value for $this->civicrmUrl.
262 if (empty($this->civicrmUrl
)) {
263 $config = CRM_Core_Config
::singleton();
264 return rtrim($config->resourceBase
, '/');
266 return $this->civicrmUrl
;
269 return $this->container
->getResUrl($key);
273 * Fetch the list of active extensions of type 'module'
276 * whether to forcibly reload extensions list from canonical store.
278 * array(array('prefix' => $, 'file' => $))
280 public function getActiveModuleFiles($fresh = FALSE) {
281 $config = CRM_Core_Config
::singleton();
282 if ($config->isUpgradeMode() ||
!defined('CIVICRM_DSN')) {
283 return array(); // hmm, ok
286 $moduleExtensions = NULL;
287 if ($this->cache
&& !$fresh) {
288 $moduleExtensions = $this->cache
->get($this->cacheKey
. '/moduleFiles');
291 if (!is_array($moduleExtensions)) {
292 // Check canonical module list
293 $moduleExtensions = array();
295 SELECT full_name, file
296 FROM civicrm_extension
300 $dao = CRM_Core_DAO
::executeQuery($sql);
301 while ($dao->fetch()) {
303 $moduleExtensions[] = array(
304 'prefix' => $dao->file
,
305 'filePath' => $this->keyToPath($dao->full_name
),
308 catch (CRM_Extension_Exception
$e) {
309 // Putting a stub here provides more consistency
310 // in how getActiveModuleFiles when racing between
311 // dirty file-removals and cache-clears.
312 CRM_Core_Session
::setStatus($e->getMessage(), '', 'error');
313 $moduleExtensions[] = array(
314 'prefix' => $dao->file
,
321 $this->cache
->set($this->cacheKey
. '/moduleFiles', $moduleExtensions);
324 return $moduleExtensions;
328 * Get a list of base URLs for all active modules.
331 * (string $extKey => string $baseUrl)
333 public function getActiveModuleUrls() {
334 // TODO optimization/caching
336 $urls['civicrm'] = $this->keyToUrl('civicrm');
337 foreach ($this->getModules() as $module) {
338 /** @var $module CRM_Core_Module */
339 if ($module->is_active
) {
340 $urls[$module->name
] = $this->keyToUrl($module->name
);
347 * @param string $name
351 public function isActiveModule($name) {
352 $activeModules = $this->getActiveModuleFiles();
353 foreach ($activeModules as $activeModule) {
354 if ($activeModule['prefix'] == $name) {
362 * Get a list of all installed modules, including enabled and disabled ones
367 public function getModules() {
369 $dao = new CRM_Core_DAO_Extension();
370 $dao->type
= 'module';
372 while ($dao->fetch()) {
373 $result[] = new CRM_Core_Module($dao->full_name
, $dao->is_active
);
379 * Given the class, provides the template path.
382 * @param string $clazz
383 * Extension class name.
386 * path to extension's templates directory
388 public function getTemplatePath($clazz) {
389 $path = $this->container
->getPath($this->classToKey($clazz));
390 return $path . DIRECTORY_SEPARATOR
. self
::EXT_TEMPLATES_DIRNAME
;
392 $path = $this->classToPath($clazz);
393 $pathElm = explode(DIRECTORY_SEPARATOR, $path);
395 return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME;
400 * Given te class, provides the template name.
401 * @todo consider multiple templates, support for one template for now
404 * @param string $clazz
405 * Extension class name.
408 * extension's template name
410 public function getTemplateName($clazz) {
411 $info = $this->keyToInfo($this->classToKey($clazz));
412 return (string) $info->file
. '.tpl';
415 public function refresh() {
416 $this->infos
= array();
417 $this->moduleExtensions
= NULL;
419 $this->cache
->delete($this->cacheKey
. '/moduleFiles');