Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 string $key extension key | |
124 | * | |
125 | * @return string full path the extension .php file | |
126 | */ | |
127 | public function classToPath($clazz) { | |
128 | $elements = explode('_', $clazz); | |
129 | $key = implode('.', $elements); | |
130 | return $this->keyToPath($key); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Given the string, returns true or false if it's an extension key. | |
135 | * | |
136 | * @access public | |
137 | * | |
138 | * @param string $key a string which might be an extension key | |
139 | * | |
140 | * @return boolean true if given string is an extension name | |
141 | */ | |
142 | public function isExtensionKey($key) { | |
143 | // check if the string is an extension name or the class | |
144 | return (strpos($key, '.') !== FALSE) ? TRUE : FALSE; | |
145 | } | |
146 | ||
147 | /** | |
148 | * Given the string, returns true or false if it's an extension class name. | |
149 | * | |
150 | * @access public | |
151 | * | |
152 | * @param string $clazz a string which might be an extension class name | |
153 | * | |
154 | * @return boolean true if given string is an extension class name | |
155 | */ | |
156 | public function isExtensionClass($clazz) { | |
157 | ||
158 | if (substr($clazz, 0, 4) != 'CRM_') { | |
159 | return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz); | |
160 | } | |
161 | return FALSE; | |
162 | } | |
163 | ||
164 | /** | |
165 | * @param string $key extension fully-qualified-name | |
166 | * @return object CRM_Extension_Info | |
167 | */ | |
168 | public function keyToInfo($key, $fresh = FALSE) { | |
169 | if ($fresh || !array_key_exists($key, $this->infos)) { | |
170 | try { | |
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]) { | |
176 | throw $e; | |
177 | } | |
178 | } | |
179 | } | |
180 | return $this->infos[$key]; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Given the key, provides extension's class name. | |
185 | * | |
186 | * @access public | |
187 | * | |
188 | * @param string $key extension key | |
189 | * | |
190 | * @return string name of extension's main class | |
191 | */ | |
192 | public function keyToClass($key) { | |
193 | return str_replace('.', '_', $key); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Given the key, provides the path to file containing | |
198 | * extension's main class. | |
199 | * | |
200 | * @access public | |
201 | * | |
202 | * @param string $key extension key | |
203 | * | |
204 | * @return string path to file containing extension's main class | |
205 | */ | |
206 | public function keyToPath($key) { | |
207 | $info = $this->keyToInfo($key); | |
208 | return $this->container->getPath($key) . DIRECTORY_SEPARATOR . $info->file . '.php'; | |
209 | } | |
210 | ||
211 | /** | |
212 | * Given the key, provides the path to file containing | |
213 | * extension's main class. | |
214 | * | |
215 | * @access public | |
216 | * @param string $key extension key | |
217 | * @return string local path of the extension source tree | |
218 | */ | |
219 | public function keyToBasePath($key) { | |
220 | if ($key == 'civicrm') { | |
221 | return $this->civicrmPath; | |
222 | } | |
223 | return $this->container->getPath($key); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Given the key, provides the path to file containing | |
228 | * extension's main class. | |
229 | * | |
230 | * @access public | |
231 | * | |
232 | * @param string $key extension key | |
233 | * | |
234 | * @return string url for resources in this extension | |
235 | */ | |
236 | public function keyToUrl($key) { | |
237 | if ($key == 'civicrm') { | |
dee7a2b1 PJ |
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. | |
3d4a4ccf PJ |
240 | if (empty($this->civicrmUrl)) { |
241 | $config = CRM_Core_Config::singleton(); | |
242 | return rtrim($config->resourceBase, '/'); | |
243 | } | |
6a488035 TO |
244 | return $this->civicrmUrl; |
245 | } | |
246 | ||
247 | return $this->container->getResUrl($key); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Fetch the list of active extensions of type 'module' | |
252 | * | |
253 | * @param $fresh bool whether to forcibly reload extensions list from canonical store | |
254 | * @return array - array(array('prefix' => $, 'file' => $)) | |
255 | */ | |
256 | public function getActiveModuleFiles($fresh = FALSE) { | |
257 | $config = CRM_Core_Config::singleton(); | |
258 | if ($config->isUpgradeMode() || !defined('CIVICRM_DSN')) { | |
259 | return array(); // hmm, ok | |
260 | } | |
261 | ||
262 | $moduleExtensions = NULL; | |
263 | if ($this->cache && !$fresh) { | |
264 | $moduleExtensions = $this->cache->get($this->cacheKey . '/moduleFiles'); | |
265 | } | |
266 | ||
267 | if (!is_array($moduleExtensions)) { | |
268 | // Check canonical module list | |
269 | $moduleExtensions = array(); | |
270 | $sql = ' | |
271 | SELECT full_name, file | |
272 | FROM civicrm_extension | |
273 | WHERE is_active = 1 | |
274 | AND type = "module" | |
275 | '; | |
276 | $dao = CRM_Core_DAO::executeQuery($sql); | |
277 | while ($dao->fetch()) { | |
278 | try { | |
279 | $moduleExtensions[] = array( | |
280 | 'prefix' => $dao->file, | |
281 | 'filePath' => $this->keyToPath($dao->full_name), | |
282 | ); | |
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, | |
290 | 'filePath' => NULL, | |
291 | ); | |
292 | } | |
293 | } | |
294 | ||
295 | if ($this->cache) { | |
296 | $this->cache->set($this->cacheKey . '/moduleFiles', $moduleExtensions); | |
297 | } | |
298 | } | |
299 | return $moduleExtensions; | |
300 | } | |
301 | ||
302 | public function isActiveModule($name) { | |
303 | $activeModules = $this->getActiveModuleFiles(); | |
304 | foreach ($activeModules as $activeModule) { | |
305 | if ($activeModule['prefix'] == $name) { | |
306 | return TRUE; | |
307 | } | |
308 | } | |
309 | return FALSE; | |
310 | } | |
311 | ||
312 | /** | |
313 | * Get a list of all installed modules, including enabled and disabled ones | |
314 | * | |
315 | * @return array CRM_Core_Module | |
316 | */ | |
317 | public function getModules() { | |
318 | $result = array(); | |
319 | $dao = new CRM_Core_DAO_Extension(); | |
320 | $dao->type = 'module'; | |
321 | $dao->find(); | |
322 | while ($dao->fetch()) { | |
323 | $result[] = new CRM_Core_Module($dao->full_name, $dao->is_active); | |
324 | } | |
325 | return $result; | |
326 | } | |
327 | ||
328 | /** | |
329 | * Given the class, provides the template path. | |
330 | * | |
331 | * @access public | |
332 | * | |
333 | * @param string $clazz extension class name | |
334 | * | |
335 | * @return string path to extension's templates directory | |
336 | */ | |
337 | public function getTemplatePath($clazz) { | |
338 | $path = $this->container->getPath($this->classToKey($clazz)); | |
339 | return $path . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME; | |
340 | /* | |
341 | $path = $this->classToPath($clazz); | |
342 | $pathElm = explode(DIRECTORY_SEPARATOR, $path); | |
343 | array_pop($pathElm); | |
344 | return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME; | |
345 | */ | |
346 | } | |
347 | ||
348 | /** | |
349 | * Given te class, provides the template name. | |
350 | * @todo consider multiple templates, support for one template for now | |
351 | * | |
352 | * @access public | |
353 | * | |
354 | * @param string $clazz extension class name | |
355 | * | |
356 | * @return string extension's template name | |
357 | */ | |
358 | public function getTemplateName($clazz) { | |
359 | $info = $this->keyToInfo($this->classToKey($clazz)); | |
360 | return (string) $info->file . '.tpl'; | |
361 | } | |
362 | ||
363 | public function refresh() { | |
364 | $this->infos = array(); | |
365 | $this->moduleExtensions = NULL; | |
366 | if ($this->cache) { | |
367 | $this->cache->delete($this->cacheKey . '/moduleFiles'); | |
368 | } | |
369 | } | |
370 | } |