Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * This class glues together the various parts of the extension | |
30 | * system. | |
31 | * | |
32 | * @package CRM | |
0f03f337 | 33 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
34 | */ |
35 | class CRM_Extension_System { | |
36 | private static $singleton; | |
37 | ||
38 | private $cache = NULL; | |
39 | private $fullContainer = NULL; | |
40 | private $defaultContainer = NULL; | |
41 | private $mapper = NULL; | |
42 | private $manager = NULL; | |
43 | private $browser = NULL; | |
44 | private $downloader = NULL; | |
45 | ||
4025c773 TO |
46 | /** |
47 | * @var CRM_Extension_ClassLoader | |
48 | * */ | |
49 | private $classLoader; | |
50 | ||
6a488035 | 51 | /** |
fe482240 | 52 | * The URL of the remote extensions repository. |
6a488035 TO |
53 | * |
54 | * @var string|FALSE | |
55 | */ | |
56 | private $_repoUrl = NULL; | |
57 | ||
adada472 TO |
58 | /** |
59 | * @var array | |
60 | * Construction parameters. These are primarily retained so | |
61 | * that they can influence the cache name. | |
62 | */ | |
63 | protected $parameters; | |
64 | ||
6a488035 | 65 | /** |
6c8f6e67 | 66 | * @param bool $fresh |
8f24a46c | 67 | * TRUE to force creation of a new system. |
6c8f6e67 | 68 | * |
6a488035 TO |
69 | * @return CRM_Extension_System |
70 | */ | |
71 | public static function singleton($fresh = FALSE) { | |
8f24a46c | 72 | if (!self::$singleton || $fresh) { |
6a488035 TO |
73 | if (self::$singleton) { |
74 | self::$singleton = new CRM_Extension_System(self::$singleton->parameters); | |
8f24a46c TO |
75 | } |
76 | else { | |
6a488035 TO |
77 | self::$singleton = new CRM_Extension_System(); |
78 | } | |
79 | } | |
80 | return self::$singleton; | |
81 | } | |
82 | ||
e0ef6999 EM |
83 | /** |
84 | * @param CRM_Extension_System $singleton | |
8f24a46c | 85 | * The new, singleton extension system. |
e0ef6999 | 86 | */ |
6a488035 TO |
87 | public static function setSingleton(CRM_Extension_System $singleton) { |
88 | self::$singleton = $singleton; | |
89 | } | |
90 | ||
e0ef6999 EM |
91 | /** |
92 | * @param array $parameters | |
6a2d9900 TO |
93 | * List of configuration values required by the extension system. |
94 | * Missing values will be guessed based on $config. | |
e0ef6999 | 95 | */ |
6a488035 TO |
96 | public function __construct($parameters = array()) { |
97 | $config = CRM_Core_Config::singleton(); | |
918eeb34 TO |
98 | $parameters['extensionsDir'] = CRM_Utils_Array::value('extensionsDir', $parameters, $config->extensionsDir); |
99 | $parameters['extensionsURL'] = CRM_Utils_Array::value('extensionsURL', $parameters, $config->extensionsURL); | |
100 | $parameters['resourceBase'] = CRM_Utils_Array::value('resourceBase', $parameters, $config->resourceBase); | |
101 | $parameters['userFrameworkBaseURL'] = CRM_Utils_Array::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL); | |
6a2d9900 TO |
102 | if (!array_key_exists('civicrm_root', $parameters)) { |
103 | $parameters['civicrm_root'] = $GLOBALS['civicrm_root']; | |
6a488035 | 104 | } |
6a2d9900 TO |
105 | if (!array_key_exists('cmsRootPath', $parameters)) { |
106 | $parameters['cmsRootPath'] = $config->userSystem->cmsRootPath(); | |
6a488035 | 107 | } |
adada472 TO |
108 | if (!array_key_exists('domain_id', $parameters)) { |
109 | $parameters['domain_id'] = CRM_Core_Config::domainID(); | |
110 | } | |
6a2d9900 TO |
111 | ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters)) |
112 | ||
6a488035 TO |
113 | $this->parameters = $parameters; |
114 | } | |
115 | ||
116 | /** | |
fe482240 | 117 | * Get a container which represents all available extensions. |
6a488035 TO |
118 | * |
119 | * @return CRM_Extension_Container_Interface | |
120 | */ | |
121 | public function getFullContainer() { | |
122 | if ($this->fullContainer === NULL) { | |
123 | $containers = array(); | |
124 | ||
125 | if ($this->getDefaultContainer()) { | |
126 | $containers['default'] = $this->getDefaultContainer(); | |
127 | } | |
128 | ||
6a2d9900 TO |
129 | $containers['civiroot'] = new CRM_Extension_Container_Basic( |
130 | $this->parameters['civicrm_root'], | |
131 | $this->parameters['resourceBase'], | |
132 | $this->getCache(), | |
133 | 'civiroot' | |
134 | ); | |
6a488035 TO |
135 | |
136 | // TODO: CRM_Extension_Container_Basic( /sites/all/modules ) | |
137 | // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules | |
138 | // TODO: CRM_Extension_Container_Basic( /modules ) | |
139 | // TODO: CRM_Extension_Container_Basic( /vendors ) | |
140 | ||
e29aefb4 | 141 | // At time of writing, D6, D7, and WP support cmsRootPath() but J does not |
6a2d9900 TO |
142 | if (NULL !== $this->parameters['cmsRootPath']) { |
143 | $vendorPath = $this->parameters['cmsRootPath'] . DIRECTORY_SEPARATOR . 'vendor'; | |
e29aefb4 | 144 | if (is_dir($vendorPath)) { |
6a2d9900 TO |
145 | $containers['cmsvendor'] = new CRM_Extension_Container_Basic( |
146 | $vendorPath, | |
945ccd6c | 147 | CRM_Utils_File::addTrailingSlash($this->parameters['userFrameworkBaseURL'], '/') . 'vendor', |
6a2d9900 TO |
148 | $this->getCache(), |
149 | 'cmsvendor' | |
150 | ); | |
e29aefb4 TO |
151 | } |
152 | } | |
153 | ||
6a488035 TO |
154 | $this->fullContainer = new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full'); |
155 | } | |
156 | return $this->fullContainer; | |
157 | } | |
158 | ||
159 | /** | |
fe482240 | 160 | * Get the container to which new extensions are installed. |
6a488035 TO |
161 | * |
162 | * This container should be a particular, writeable directory. | |
163 | * | |
70d331a6 | 164 | * @return CRM_Extension_Container_Default|FALSE (false if not configured) |
6a488035 TO |
165 | */ |
166 | public function getDefaultContainer() { | |
167 | if ($this->defaultContainer === NULL) { | |
168 | if ($this->parameters['extensionsDir']) { | |
70d331a6 | 169 | $this->defaultContainer = new CRM_Extension_Container_Default($this->parameters['extensionsDir'], $this->parameters['extensionsURL'], $this->getCache(), 'default'); |
8f24a46c TO |
170 | } |
171 | else { | |
6a488035 TO |
172 | $this->defaultContainer = FALSE; |
173 | } | |
174 | } | |
175 | return $this->defaultContainer; | |
176 | } | |
177 | ||
178 | /** | |
fe482240 | 179 | * Get the service which provides runtime information about extensions. |
6a488035 TO |
180 | * |
181 | * @return CRM_Extension_Mapper | |
182 | */ | |
183 | public function getMapper() { | |
184 | if ($this->mapper === NULL) { | |
185 | $this->mapper = new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper'); | |
186 | } | |
187 | return $this->mapper; | |
188 | } | |
189 | ||
4025c773 TO |
190 | /** |
191 | * @return \CRM_Extension_ClassLoader | |
192 | */ | |
193 | public function getClassLoader() { | |
194 | if ($this->classLoader === NULL) { | |
195 | $this->classLoader = new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager()); | |
196 | } | |
197 | return $this->classLoader; | |
198 | } | |
199 | ||
6a488035 | 200 | /** |
fe482240 | 201 | * Get the service for enabling and disabling extensions. |
6a488035 TO |
202 | * |
203 | * @return CRM_Extension_Manager | |
204 | */ | |
205 | public function getManager() { | |
206 | if ($this->manager === NULL) { | |
207 | $typeManagers = array( | |
208 | 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()), | |
209 | 'report' => new CRM_Extension_Manager_Report(), | |
210 | 'search' => new CRM_Extension_Manager_Search(), | |
211 | 'module' => new CRM_Extension_Manager_Module($this->getMapper()), | |
212 | ); | |
213 | $this->manager = new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers); | |
214 | } | |
215 | return $this->manager; | |
216 | } | |
217 | ||
218 | /** | |
219 | * Get the service for finding remotely-available extensions | |
220 | * | |
221 | * @return CRM_Extension_Browser | |
222 | */ | |
223 | public function getBrowser() { | |
224 | if ($this->browser === NULL) { | |
225 | $cacheDir = NULL; | |
226 | if ($this->getDefaultContainer()) { | |
227 | $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR . 'cache'; | |
228 | } | |
229 | $this->browser = new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir); | |
230 | } | |
231 | return $this->browser; | |
232 | } | |
233 | ||
234 | /** | |
235 | * Get the service for loading code from remotely-available extensions | |
236 | * | |
237 | * @return CRM_Extension_Downloader | |
238 | */ | |
239 | public function getDownloader() { | |
240 | if ($this->downloader === NULL) { | |
241 | $basedir = ($this->getDefaultContainer() ? $this->getDefaultContainer()->getBaseDir() : NULL); | |
242 | $this->downloader = new CRM_Extension_Downloader( | |
243 | $this->getManager(), | |
244 | $basedir, | |
245 | CRM_Utils_File::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp'; | |
246 | ); | |
247 | } | |
248 | return $this->downloader; | |
249 | } | |
250 | ||
251 | /** | |
252 | * @return CRM_Utils_Cache_Interface | |
253 | */ | |
254 | public function getCache() { | |
255 | if ($this->cache === NULL) { | |
a4704404 TO |
256 | $cacheGroup = md5(serialize(array('ext', $this->parameters))); |
257 | // Extension system starts before container. Manage our own cache. | |
258 | $this->cache = CRM_Utils_Cache::create(array( | |
259 | 'name' => $cacheGroup, | |
a944a143 | 260 | 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'), |
a4704404 TO |
261 | 'prefetch' => TRUE, |
262 | )); | |
6a488035 TO |
263 | } |
264 | return $this->cache; | |
265 | } | |
266 | ||
267 | /** | |
fe482240 | 268 | * Determine the URL which provides a feed of available extensions. |
6a488035 TO |
269 | * |
270 | * @return string|FALSE | |
271 | */ | |
272 | public function getRepositoryUrl() { | |
273 | if (empty($this->_repoUrl) && $this->_repoUrl !== FALSE) { | |
7595b57f | 274 | $url = Civi::settings()->get('ext_repo_url'); |
6a488035 TO |
275 | |
276 | // boolean false means don't try to check extensions | |
4faa436b | 277 | // CRM-10575 |
8f24a46c | 278 | if ($url === FALSE) { |
b3a4b879 | 279 | $this->_repoUrl = FALSE; |
6a488035 TO |
280 | } |
281 | else { | |
efceedd4 | 282 | $this->_repoUrl = CRM_Utils_System::evalUrl($url); |
6a488035 TO |
283 | } |
284 | } | |
285 | return $this->_repoUrl; | |
286 | } | |
96025800 | 287 | |
9a5dc864 AS |
288 | /** |
289 | * Take an extension's raw XML info and add information about the | |
290 | * extension's status on the local system. | |
291 | * | |
292 | * The result format resembles the old CRM_Core_Extensions_Extension. | |
293 | * | |
294 | * @param CRM_Extension_Info $obj | |
295 | * | |
296 | * @return array | |
297 | */ | |
298 | public static function createExtendedInfo(CRM_Extension_Info $obj) { | |
299 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
300 | $manager = CRM_Extension_System::singleton()->getManager(); | |
301 | ||
302 | $extensionRow = (array) $obj; | |
303 | try { | |
304 | $extensionRow['path'] = $mapper->keyToBasePath($obj->key); | |
305 | } | |
306 | catch (CRM_Extension_Exception $e) { | |
307 | $extensionRow['path'] = ''; | |
308 | } | |
309 | $extensionRow['status'] = $manager->getStatus($obj->key); | |
310 | ||
311 | switch ($extensionRow['status']) { | |
312 | case CRM_Extension_Manager::STATUS_UNINSTALLED: | |
313 | $extensionRow['statusLabel'] = ''; // ts('Uninstalled'); | |
314 | break; | |
315 | ||
316 | case CRM_Extension_Manager::STATUS_DISABLED: | |
317 | $extensionRow['statusLabel'] = ts('Disabled'); | |
318 | break; | |
319 | ||
320 | case CRM_Extension_Manager::STATUS_INSTALLED: | |
321 | $extensionRow['statusLabel'] = ts('Enabled'); // ts('Installed'); | |
322 | break; | |
323 | ||
324 | case CRM_Extension_Manager::STATUS_DISABLED_MISSING: | |
325 | $extensionRow['statusLabel'] = ts('Disabled (Missing)'); | |
326 | break; | |
327 | ||
328 | case CRM_Extension_Manager::STATUS_INSTALLED_MISSING: | |
329 | $extensionRow['statusLabel'] = ts('Enabled (Missing)'); // ts('Installed'); | |
330 | break; | |
331 | ||
332 | default: | |
333 | $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')'; | |
334 | } | |
335 | return $extensionRow; | |
336 | } | |
830733d3 | 337 | |
6a488035 | 338 | } |