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;
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, '/');
101 $config = CRM_Core_Config
::singleton();
102 $this->civicrmUrl
= rtrim($config->resourceBase
, '/');
105 $this->civicrmPath
= rtrim($civicrmPath,'/');
107 global $civicrm_root;
108 $this->civicrmPath
= rtrim($civicrm_root,'/');
113 * Given the class, provides extension's key.
117 * @param string $clazz extension class name
119 * @return string name of extension key
121 public function classToKey($clazz) {
122 return str_replace('_', '.', $clazz);
126 * Given the class, provides extension path.
132 * @internal param string $key extension key
134 * @return string full path the extension .php file
136 public function classToPath($clazz) {
137 $elements = explode('_', $clazz);
138 $key = implode('.', $elements);
139 return $this->keyToPath($key);
143 * Given the string, returns true or false if it's an extension key.
147 * @param string $key a string which might be an extension key
149 * @return boolean true if given string is an extension name
151 public function isExtensionKey($key) {
152 // check if the string is an extension name or the class
153 return (strpos($key, '.') !== FALSE) ?
TRUE : FALSE;
157 * Given the string, returns true or false if it's an extension class name.
161 * @param string $clazz a string which might be an extension class name
163 * @return boolean true if given string is an extension class name
165 public function isExtensionClass($clazz) {
167 if (substr($clazz, 0, 4) != 'CRM_') {
168 return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz);
174 * @param string $key extension fully-qualified-name
177 * @throws CRM_Extension_Exception
179 * @return object CRM_Extension_Info
181 public function keyToInfo($key, $fresh = FALSE) {
182 if ($fresh ||
!array_key_exists($key, $this->infos
)) {
184 $this->infos
[$key] = CRM_Extension_Info
::loadFromFile($this->container
->getPath($key) . DIRECTORY_SEPARATOR
. CRM_Extension_Info
::FILENAME
);
185 } catch (CRM_Extension_Exception
$e) {
186 // file has more detailed info, but we'll fallback to DB if it's missing -- DB has enough info to uninstall
187 $this->infos
[$key] = CRM_Extension_System
::singleton()->getManager()->createInfoFromDB($key);
188 if (!$this->infos
[$key]) {
193 return $this->infos
[$key];
197 * Given the key, provides extension's class name.
201 * @param string $key extension key
203 * @return string name of extension's main class
205 public function keyToClass($key) {
206 return str_replace('.', '_', $key);
210 * Given the key, provides the path to file containing
211 * extension's main class.
215 * @param string $key extension key
217 * @return string path to file containing extension's main class
219 public function keyToPath($key) {
220 $info = $this->keyToInfo($key);
221 return $this->container
->getPath($key) . DIRECTORY_SEPARATOR
. $info->file
. '.php';
225 * Given the key, provides the path to file containing
226 * extension's main class.
229 * @param string $key extension key
230 * @return string local path of the extension source tree
232 public function keyToBasePath($key) {
233 if ($key == 'civicrm') {
234 return $this->civicrmPath
;
236 return $this->container
->getPath($key);
240 * Given the key, provides the path to file containing
241 * extension's main class.
245 * @param string $key extension key
247 * @return string url for resources in this extension
249 public function keyToUrl($key) {
250 if ($key == 'civicrm') {
251 // CRM-12130 Workaround: If the domain's config_backend is NULL at the start of the request,
252 // then the Mapper is wrongly constructed with an empty value for $this->civicrmUrl.
253 if (empty($this->civicrmUrl
)) {
254 $config = CRM_Core_Config
::singleton();
255 return rtrim($config->resourceBase
, '/');
257 return $this->civicrmUrl
;
260 return $this->container
->getResUrl($key);
264 * Fetch the list of active extensions of type 'module'
266 * @param $fresh bool whether to forcibly reload extensions list from canonical store
267 * @return array - array(array('prefix' => $, 'file' => $))
269 public function getActiveModuleFiles($fresh = FALSE) {
270 $config = CRM_Core_Config
::singleton();
271 if ($config->isUpgradeMode() ||
!defined('CIVICRM_DSN')) {
272 return array(); // hmm, ok
275 $moduleExtensions = NULL;
276 if ($this->cache
&& !$fresh) {
277 $moduleExtensions = $this->cache
->get($this->cacheKey
. '/moduleFiles');
280 if (!is_array($moduleExtensions)) {
281 // Check canonical module list
282 $moduleExtensions = array();
284 SELECT full_name, file
285 FROM civicrm_extension
289 $dao = CRM_Core_DAO
::executeQuery($sql);
290 while ($dao->fetch()) {
292 $moduleExtensions[] = array(
293 'prefix' => $dao->file
,
294 'filePath' => $this->keyToPath($dao->full_name
),
296 } catch (CRM_Extension_Exception
$e) {
297 // Putting a stub here provides more consistency
298 // in how getActiveModuleFiles when racing between
299 // dirty file-removals and cache-clears.
300 CRM_Core_Session
::setStatus($e->getMessage(), '', 'error');
301 $moduleExtensions[] = array(
302 'prefix' => $dao->file
,
309 $this->cache
->set($this->cacheKey
. '/moduleFiles', $moduleExtensions);
312 return $moduleExtensions;
316 * Get a list of base URLs for all active modules
318 * @return array (string $extKey => string $baseUrl)
320 public function getActiveModuleUrls() {
321 // TODO optimization/caching
323 $urls['civicrm'] = $this->keyToUrl('civicrm');
324 foreach ($this->getModules() as $module) {
325 /** @var $module CRM_Core_Module */
326 if ($module->is_active
) {
327 $urls[$module->name
] = $this->keyToUrl($module->name
);
338 public function isActiveModule($name) {
339 $activeModules = $this->getActiveModuleFiles();
340 foreach ($activeModules as $activeModule) {
341 if ($activeModule['prefix'] == $name) {
349 * Get a list of all installed modules, including enabled and disabled ones
351 * @return array CRM_Core_Module
353 public function getModules() {
355 $dao = new CRM_Core_DAO_Extension();
356 $dao->type
= 'module';
358 while ($dao->fetch()) {
359 $result[] = new CRM_Core_Module($dao->full_name
, $dao->is_active
);
365 * Given the class, provides the template path.
369 * @param string $clazz extension class name
371 * @return string path to extension's templates directory
373 public function getTemplatePath($clazz) {
374 $path = $this->container
->getPath($this->classToKey($clazz));
375 return $path . DIRECTORY_SEPARATOR
. self
::EXT_TEMPLATES_DIRNAME
;
377 $path = $this->classToPath($clazz);
378 $pathElm = explode(DIRECTORY_SEPARATOR, $path);
380 return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME;
385 * Given te class, provides the template name.
386 * @todo consider multiple templates, support for one template for now
390 * @param string $clazz extension class name
392 * @return string extension's template name
394 public function getTemplateName($clazz) {
395 $info = $this->keyToInfo($this->classToKey($clazz));
396 return (string) $info->file
. '.tpl';
399 public function refresh() {
400 $this->infos
= array();
401 $this->moduleExtensions
= NULL;
403 $this->cache
->delete($this->cacheKey
. '/moduleFiles');