3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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;
87 public function __construct(CRM_Extension_Container_Interface
$container, CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL, $civicrmPath = NULL, $civicrmUrl = NULL) {
88 $this->container
= $container;
89 $this->cache
= $cache;
90 $this->cacheKey
= $cacheKey;
92 $this->civicrmUrl
= rtrim($civicrmUrl, '/');
94 $config = CRM_Core_Config
::singleton();
95 $this->civicrmUrl
= rtrim($config->resourceBase
, '/');
98 $this->civicrmPath
= rtrim($civicrmPath,'/');
100 global $civicrm_root;
101 $this->civicrmPath
= rtrim($civicrm_root,'/');
106 * Given the class, provides extension's key.
110 * @param string $clazz extension class name
112 * @return string name of extension key
114 public function classToKey($clazz) {
115 return str_replace('_', '.', $clazz);
119 * Given the class, provides extension path.
125 * @internal param string $key extension key
127 * @return string full path the extension .php file
129 public function classToPath($clazz) {
130 $elements = explode('_', $clazz);
131 $key = implode('.', $elements);
132 return $this->keyToPath($key);
136 * Given the string, returns true or false if it's an extension key.
140 * @param string $key a string which might be an extension key
142 * @return boolean true if given string is an extension name
144 public function isExtensionKey($key) {
145 // check if the string is an extension name or the class
146 return (strpos($key, '.') !== FALSE) ?
TRUE : FALSE;
150 * Given the string, returns true or false if it's an extension class name.
154 * @param string $clazz a string which might be an extension class name
156 * @return boolean true if given string is an extension class name
158 public function isExtensionClass($clazz) {
160 if (substr($clazz, 0, 4) != 'CRM_') {
161 return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz);
167 * @param string $key extension fully-qualified-name
170 * @throws CRM_Extension_Exception
172 * @return object CRM_Extension_Info
174 public function keyToInfo($key, $fresh = FALSE) {
175 if ($fresh ||
!array_key_exists($key, $this->infos
)) {
177 $this->infos
[$key] = CRM_Extension_Info
::loadFromFile($this->container
->getPath($key) . DIRECTORY_SEPARATOR
. CRM_Extension_Info
::FILENAME
);
178 } catch (CRM_Extension_Exception
$e) {
179 // file has more detailed info, but we'll fallback to DB if it's missing -- DB has enough info to uninstall
180 $this->infos
[$key] = CRM_Extension_System
::singleton()->getManager()->createInfoFromDB($key);
181 if (!$this->infos
[$key]) {
186 return $this->infos
[$key];
190 * Given the key, provides extension's class name.
194 * @param string $key extension key
196 * @return string name of extension's main class
198 public function keyToClass($key) {
199 return str_replace('.', '_', $key);
203 * Given the key, provides the path to file containing
204 * extension's main class.
208 * @param string $key extension key
210 * @return string path to file containing extension's main class
212 public function keyToPath($key) {
213 $info = $this->keyToInfo($key);
214 return $this->container
->getPath($key) . DIRECTORY_SEPARATOR
. $info->file
. '.php';
218 * Given the key, provides the path to file containing
219 * extension's main class.
222 * @param string $key extension key
223 * @return string local path of the extension source tree
225 public function keyToBasePath($key) {
226 if ($key == 'civicrm') {
227 return $this->civicrmPath
;
229 return $this->container
->getPath($key);
233 * Given the key, provides the path to file containing
234 * extension's main class.
238 * @param string $key extension key
240 * @return string url for resources in this extension
242 public function keyToUrl($key) {
243 if ($key == 'civicrm') {
244 // CRM-12130 Workaround: If the domain's config_backend is NULL at the start of the request,
245 // then the Mapper is wrongly constructed with an empty value for $this->civicrmUrl.
246 if (empty($this->civicrmUrl
)) {
247 $config = CRM_Core_Config
::singleton();
248 return rtrim($config->resourceBase
, '/');
250 return $this->civicrmUrl
;
253 return $this->container
->getResUrl($key);
257 * Fetch the list of active extensions of type 'module'
259 * @param $fresh bool whether to forcibly reload extensions list from canonical store
260 * @return array - array(array('prefix' => $, 'file' => $))
262 public function getActiveModuleFiles($fresh = FALSE) {
263 $config = CRM_Core_Config
::singleton();
264 if ($config->isUpgradeMode() ||
!defined('CIVICRM_DSN')) {
265 return array(); // hmm, ok
268 $moduleExtensions = NULL;
269 if ($this->cache
&& !$fresh) {
270 $moduleExtensions = $this->cache
->get($this->cacheKey
. '/moduleFiles');
273 if (!is_array($moduleExtensions)) {
274 // Check canonical module list
275 $moduleExtensions = array();
277 SELECT full_name, file
278 FROM civicrm_extension
282 $dao = CRM_Core_DAO
::executeQuery($sql);
283 while ($dao->fetch()) {
285 $moduleExtensions[] = array(
286 'prefix' => $dao->file
,
287 'filePath' => $this->keyToPath($dao->full_name
),
289 } catch (CRM_Extension_Exception
$e) {
290 // Putting a stub here provides more consistency
291 // in how getActiveModuleFiles when racing between
292 // dirty file-removals and cache-clears.
293 CRM_Core_Session
::setStatus($e->getMessage(), '', 'error');
294 $moduleExtensions[] = array(
295 'prefix' => $dao->file
,
302 $this->cache
->set($this->cacheKey
. '/moduleFiles', $moduleExtensions);
305 return $moduleExtensions;
309 * Get a list of base URLs for all active modules
311 * @return array (string $extKey => string $baseUrl)
313 public function getActiveModuleUrls() {
314 // TODO optimization/caching
316 $urls['civicrm'] = $this->keyToUrl('civicrm');
317 foreach ($this->getModules() as $module) {
318 /** @var $module CRM_Core_Module */
319 if ($module->is_active
) {
320 $urls[$module->name
] = $this->keyToUrl($module->name
);
326 public function isActiveModule($name) {
327 $activeModules = $this->getActiveModuleFiles();
328 foreach ($activeModules as $activeModule) {
329 if ($activeModule['prefix'] == $name) {
337 * Get a list of all installed modules, including enabled and disabled ones
339 * @return array CRM_Core_Module
341 public function getModules() {
343 $dao = new CRM_Core_DAO_Extension();
344 $dao->type
= 'module';
346 while ($dao->fetch()) {
347 $result[] = new CRM_Core_Module($dao->full_name
, $dao->is_active
);
353 * Given the class, provides the template path.
357 * @param string $clazz extension class name
359 * @return string path to extension's templates directory
361 public function getTemplatePath($clazz) {
362 $path = $this->container
->getPath($this->classToKey($clazz));
363 return $path . DIRECTORY_SEPARATOR
. self
::EXT_TEMPLATES_DIRNAME
;
365 $path = $this->classToPath($clazz);
366 $pathElm = explode(DIRECTORY_SEPARATOR, $path);
368 return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME;
373 * Given te class, provides the template name.
374 * @todo consider multiple templates, support for one template for now
378 * @param string $clazz extension class name
380 * @return string extension's template name
382 public function getTemplateName($clazz) {
383 $info = $this->keyToInfo($this->classToKey($clazz));
384 return (string) $info->file
. '.tpl';
387 public function refresh() {
388 $this->infos
= array();
389 $this->moduleExtensions
= NULL;
391 $this->cache
->delete($this->cacheKey
. '/moduleFiles');