3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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.
123 * @param string $key extension key
125 * @return string full path the extension .php file
127 public function classToPath($clazz) {
128 $elements = explode('_', $clazz);
129 $key = implode('.', $elements);
130 return $this->keyToPath($key);
134 * Given the string, returns true or false if it's an extension key.
138 * @param string $key a string which might be an extension key
140 * @return boolean true if given string is an extension name
142 public function isExtensionKey($key) {
143 // check if the string is an extension name or the class
144 return (strpos($key, '.') !== FALSE) ?
TRUE : FALSE;
148 * Given the string, returns true or false if it's an extension class name.
152 * @param string $clazz a string which might be an extension class name
154 * @return boolean true if given string is an extension class name
156 public function isExtensionClass($clazz) {
158 if (substr($clazz, 0, 4) != 'CRM_') {
159 return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz);
165 * @param string $key extension fully-qualified-name
166 * @return object CRM_Extension_Info
168 public function keyToInfo($key, $fresh = FALSE) {
169 if ($fresh ||
!array_key_exists($key, $this->infos
)) {
171 $this->infos
[$key] = CRM_Extension_Info
::loadFromFile($this->container
->getPath($key) . DIRECTORY_SEPARATOR
. CRM_Extension_Info
::FILENAME
);
172 } catch (CRM_Extension_Exception
$e) {
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]) {
180 return $this->infos
[$key];
184 * Given the key, provides extension's class name.
188 * @param string $key extension key
190 * @return string name of extension's main class
192 public function keyToClass($key) {
193 return str_replace('.', '_', $key);
197 * Given the key, provides the path to file containing
198 * extension's main class.
202 * @param string $key extension key
204 * @return string path to file containing extension's main class
206 public function keyToPath($key) {
207 $info = $this->keyToInfo($key);
208 return $this->container
->getPath($key) . DIRECTORY_SEPARATOR
. $info->file
. '.php';
212 * Given the key, provides the path to file containing
213 * extension's main class.
216 * @param string $key extension key
217 * @return string local path of the extension source tree
219 public function keyToBasePath($key) {
220 if ($key == 'civicrm') {
221 return $this->civicrmPath
;
223 return $this->container
->getPath($key);
227 * Given the key, provides the path to file containing
228 * extension's main class.
232 * @param string $key extension key
234 * @return string url for resources in this extension
236 public function keyToUrl($key) {
237 if ($key == 'civicrm') {
238 // CRM-12130 Workaround: If the domain's config_backend is NULL at the start of the request,
239 // then the Mapper is wrongly constructed with an empty value for $this->civicrmUrl.
240 if (empty($this->civicrmUrl
)) {
241 $config = CRM_Core_Config
::singleton();
242 return rtrim($config->resourceBase
, '/');
244 return $this->civicrmUrl
;
247 return $this->container
->getResUrl($key);
251 * Fetch the list of active extensions of type 'module'
253 * @param $fresh bool whether to forcibly reload extensions list from canonical store
254 * @return array - array(array('prefix' => $, 'file' => $))
256 public function getActiveModuleFiles($fresh = FALSE) {
257 $config = CRM_Core_Config
::singleton();
258 if ($config->isUpgradeMode() ||
!defined('CIVICRM_DSN')) {
259 return array(); // hmm, ok
262 $moduleExtensions = NULL;
263 if ($this->cache
&& !$fresh) {
264 $moduleExtensions = $this->cache
->get($this->cacheKey
. '/moduleFiles');
267 if (!is_array($moduleExtensions)) {
268 // Check canonical module list
269 $moduleExtensions = array();
271 SELECT full_name, file
272 FROM civicrm_extension
276 $dao = CRM_Core_DAO
::executeQuery($sql);
277 while ($dao->fetch()) {
279 $moduleExtensions[] = array(
280 'prefix' => $dao->file
,
281 'filePath' => $this->keyToPath($dao->full_name
),
283 } catch (CRM_Extension_Exception
$e) {
284 // Putting a stub here provides more consistency
285 // in how getActiveModuleFiles when racing between
286 // dirty file-removals and cache-clears.
287 CRM_Core_Session
::setStatus($e->getMessage(), '', 'error');
288 $moduleExtensions[] = array(
289 'prefix' => $dao->file
,
296 $this->cache
->set($this->cacheKey
. '/moduleFiles', $moduleExtensions);
299 return $moduleExtensions;
302 public function isActiveModule($name) {
303 $activeModules = $this->getActiveModuleFiles();
304 foreach ($activeModules as $activeModule) {
305 if ($activeModule['prefix'] == $name) {
313 * Get a list of all installed modules, including enabled and disabled ones
315 * @return array CRM_Core_Module
317 public function getModules() {
319 $dao = new CRM_Core_DAO_Extension();
320 $dao->type
= 'module';
322 while ($dao->fetch()) {
323 $result[] = new CRM_Core_Module($dao->full_name
, $dao->is_active
);
329 * Given the class, provides the template path.
333 * @param string $clazz extension class name
335 * @return string path to extension's templates directory
337 public function getTemplatePath($clazz) {
338 $path = $this->container
->getPath($this->classToKey($clazz));
339 return $path . DIRECTORY_SEPARATOR
. self
::EXT_TEMPLATES_DIRNAME
;
341 $path = $this->classToPath($clazz);
342 $pathElm = explode(DIRECTORY_SEPARATOR, $path);
344 return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME;
349 * Given te class, provides the template name.
350 * @todo consider multiple templates, support for one template for now
354 * @param string $clazz extension class name
356 * @return string extension's template name
358 public function getTemplateName($clazz) {
359 $info = $this->keyToInfo($this->classToKey($clazz));
360 return (string) $info->file
. '.tpl';
363 public function refresh() {
364 $this->infos
= array();
365 $this->moduleExtensions
= NULL;
367 $this->cache
->delete($this->cacheKey
. '/moduleFiles');