Merge pull request #3280 from atif-shaikh/CiviCoreBugs
[civicrm-core.git] / CRM / Extension / Mapper.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
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.
32 *
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".
37 *
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
42 * module-extensions.
43 *
44 * @package CRM
45 * @copyright CiviCRM LLC (c) 2004-2014
46 * $Id$
47 *
48 */
49 class CRM_Extension_Mapper {
50
51 /**
52 * An URL for public extensions repository
53 */
54 //CONST DEFAULT_EXTENSIONS_REPOSITORY = 'http://civicrm.org/extdir/ver={ver}|cms={uf}';
55
56 /**
57 * Extension info file name
58 */
59 const EXT_TEMPLATES_DIRNAME = 'templates';
60
61 /**
62 * @var CRM_Extension_Container_Interface
63 */
64 protected $container;
65
66 /**
67 * @var array (key => CRM_Extension_Info)
68 */
69 protected $infos = array();
70
71 /**
72 * @var array
73 */
74 protected $moduleExtensions = NULL;
75
76 /**
77 * @var CRM_Utils_Cache_Interface
78 */
79 protected $cache;
80
81 protected $cacheKey;
82
83 protected $civicrmPath;
84
85 protected $civicrmUrl;
86
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;
91 if ($civicrmUrl) {
92 $this->civicrmUrl = rtrim($civicrmUrl, '/');
93 } else {
94 $config = CRM_Core_Config::singleton();
95 $this->civicrmUrl = rtrim($config->resourceBase, '/');
96 }
97 if ($civicrmPath) {
98 $this->civicrmPath = rtrim($civicrmPath,'/');
99 } else {
100 global $civicrm_root;
101 $this->civicrmPath = rtrim($civicrm_root,'/');
102 }
103 }
104
105 /**
106 * Given the class, provides extension's key.
107 *
108 * @access public
109 *
110 * @param string $clazz extension class name
111 *
112 * @return string name of extension key
113 */
114 public function classToKey($clazz) {
115 return str_replace('_', '.', $clazz);
116 }
117
118 /**
119 * Given the class, provides extension path.
120 *
121 * @access public
122 *
123 * @param $clazz
124 *
125 * @internal param string $key extension key
126 *
127 * @return string full path the extension .php file
128 */
129 public function classToPath($clazz) {
130 $elements = explode('_', $clazz);
131 $key = implode('.', $elements);
132 return $this->keyToPath($key);
133 }
134
135 /**
136 * Given the string, returns true or false if it's an extension key.
137 *
138 * @access public
139 *
140 * @param string $key a string which might be an extension key
141 *
142 * @return boolean true if given string is an extension name
143 */
144 public function isExtensionKey($key) {
145 // check if the string is an extension name or the class
146 return (strpos($key, '.') !== FALSE) ? TRUE : FALSE;
147 }
148
149 /**
150 * Given the string, returns true or false if it's an extension class name.
151 *
152 * @access public
153 *
154 * @param string $clazz a string which might be an extension class name
155 *
156 * @return boolean true if given string is an extension class name
157 */
158 public function isExtensionClass($clazz) {
159
160 if (substr($clazz, 0, 4) != 'CRM_') {
161 return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz);
162 }
163 return FALSE;
164 }
165
166 /**
167 * @param string $key extension fully-qualified-name
168 * @param bool $fresh
169 *
170 * @throws CRM_Extension_Exception
171 * @throws Exception
172 * @return object CRM_Extension_Info
173 */
174 public function keyToInfo($key, $fresh = FALSE) {
175 if ($fresh || !array_key_exists($key, $this->infos)) {
176 try {
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]) {
182 throw $e;
183 }
184 }
185 }
186 return $this->infos[$key];
187 }
188
189 /**
190 * Given the key, provides extension's class name.
191 *
192 * @access public
193 *
194 * @param string $key extension key
195 *
196 * @return string name of extension's main class
197 */
198 public function keyToClass($key) {
199 return str_replace('.', '_', $key);
200 }
201
202 /**
203 * Given the key, provides the path to file containing
204 * extension's main class.
205 *
206 * @access public
207 *
208 * @param string $key extension key
209 *
210 * @return string path to file containing extension's main class
211 */
212 public function keyToPath($key) {
213 $info = $this->keyToInfo($key);
214 return $this->container->getPath($key) . DIRECTORY_SEPARATOR . $info->file . '.php';
215 }
216
217 /**
218 * Given the key, provides the path to file containing
219 * extension's main class.
220 *
221 * @access public
222 * @param string $key extension key
223 * @return string local path of the extension source tree
224 */
225 public function keyToBasePath($key) {
226 if ($key == 'civicrm') {
227 return $this->civicrmPath;
228 }
229 return $this->container->getPath($key);
230 }
231
232 /**
233 * Given the key, provides the path to file containing
234 * extension's main class.
235 *
236 * @access public
237 *
238 * @param string $key extension key
239 *
240 * @return string url for resources in this extension
241 */
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, '/');
249 }
250 return $this->civicrmUrl;
251 }
252
253 return $this->container->getResUrl($key);
254 }
255
256 /**
257 * Fetch the list of active extensions of type 'module'
258 *
259 * @param $fresh bool whether to forcibly reload extensions list from canonical store
260 * @return array - array(array('prefix' => $, 'file' => $))
261 */
262 public function getActiveModuleFiles($fresh = FALSE) {
263 $config = CRM_Core_Config::singleton();
264 if ($config->isUpgradeMode() || !defined('CIVICRM_DSN')) {
265 return array(); // hmm, ok
266 }
267
268 $moduleExtensions = NULL;
269 if ($this->cache && !$fresh) {
270 $moduleExtensions = $this->cache->get($this->cacheKey . '/moduleFiles');
271 }
272
273 if (!is_array($moduleExtensions)) {
274 // Check canonical module list
275 $moduleExtensions = array();
276 $sql = '
277 SELECT full_name, file
278 FROM civicrm_extension
279 WHERE is_active = 1
280 AND type = "module"
281 ';
282 $dao = CRM_Core_DAO::executeQuery($sql);
283 while ($dao->fetch()) {
284 try {
285 $moduleExtensions[] = array(
286 'prefix' => $dao->file,
287 'filePath' => $this->keyToPath($dao->full_name),
288 );
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,
296 'filePath' => NULL,
297 );
298 }
299 }
300
301 if ($this->cache) {
302 $this->cache->set($this->cacheKey . '/moduleFiles', $moduleExtensions);
303 }
304 }
305 return $moduleExtensions;
306 }
307
308 /**
309 * Get a list of base URLs for all active modules
310 *
311 * @return array (string $extKey => string $baseUrl)
312 */
313 public function getActiveModuleUrls() {
314 // TODO optimization/caching
315 $urls = array();
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);
321 }
322 }
323 return $urls;
324 }
325
326 public function isActiveModule($name) {
327 $activeModules = $this->getActiveModuleFiles();
328 foreach ($activeModules as $activeModule) {
329 if ($activeModule['prefix'] == $name) {
330 return TRUE;
331 }
332 }
333 return FALSE;
334 }
335
336 /**
337 * Get a list of all installed modules, including enabled and disabled ones
338 *
339 * @return array CRM_Core_Module
340 */
341 public function getModules() {
342 $result = array();
343 $dao = new CRM_Core_DAO_Extension();
344 $dao->type = 'module';
345 $dao->find();
346 while ($dao->fetch()) {
347 $result[] = new CRM_Core_Module($dao->full_name, $dao->is_active);
348 }
349 return $result;
350 }
351
352 /**
353 * Given the class, provides the template path.
354 *
355 * @access public
356 *
357 * @param string $clazz extension class name
358 *
359 * @return string path to extension's templates directory
360 */
361 public function getTemplatePath($clazz) {
362 $path = $this->container->getPath($this->classToKey($clazz));
363 return $path . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME;
364 /*
365 $path = $this->classToPath($clazz);
366 $pathElm = explode(DIRECTORY_SEPARATOR, $path);
367 array_pop($pathElm);
368 return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME;
369 */
370 }
371
372 /**
373 * Given te class, provides the template name.
374 * @todo consider multiple templates, support for one template for now
375 *
376 * @access public
377 *
378 * @param string $clazz extension class name
379 *
380 * @return string extension's template name
381 */
382 public function getTemplateName($clazz) {
383 $info = $this->keyToInfo($this->classToKey($clazz));
384 return (string) $info->file . '.tpl';
385 }
386
387 public function refresh() {
388 $this->infos = array();
389 $this->moduleExtensions = NULL;
390 if ($this->cache) {
391 $this->cache->delete($this->cacheKey . '/moduleFiles');
392 }
393 }
394 }