Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 33 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
34 | * $Id$ |
35 | * | |
36 | */ | |
37 | class CRM_Extension_System { | |
38 | private static $singleton; | |
39 | ||
40 | private $cache = NULL; | |
41 | private $fullContainer = NULL; | |
42 | private $defaultContainer = NULL; | |
43 | private $mapper = NULL; | |
44 | private $manager = NULL; | |
45 | private $browser = NULL; | |
46 | private $downloader = NULL; | |
47 | ||
48 | /** | |
fe482240 | 49 | * The URL of the remote extensions repository. |
6a488035 TO |
50 | * |
51 | * @var string|FALSE | |
52 | */ | |
53 | private $_repoUrl = NULL; | |
54 | ||
adada472 TO |
55 | /** |
56 | * @var array | |
57 | * Construction parameters. These are primarily retained so | |
58 | * that they can influence the cache name. | |
59 | */ | |
60 | protected $parameters; | |
61 | ||
6a488035 | 62 | /** |
6c8f6e67 | 63 | * @param bool $fresh |
8f24a46c | 64 | * TRUE to force creation of a new system. |
6c8f6e67 | 65 | * |
6a488035 TO |
66 | * @return CRM_Extension_System |
67 | */ | |
68 | public static function singleton($fresh = FALSE) { | |
8f24a46c | 69 | if (!self::$singleton || $fresh) { |
6a488035 TO |
70 | if (self::$singleton) { |
71 | self::$singleton = new CRM_Extension_System(self::$singleton->parameters); | |
8f24a46c TO |
72 | } |
73 | else { | |
6a488035 TO |
74 | self::$singleton = new CRM_Extension_System(); |
75 | } | |
76 | } | |
77 | return self::$singleton; | |
78 | } | |
79 | ||
e0ef6999 EM |
80 | /** |
81 | * @param CRM_Extension_System $singleton | |
8f24a46c | 82 | * The new, singleton extension system. |
e0ef6999 | 83 | */ |
6a488035 TO |
84 | public static function setSingleton(CRM_Extension_System $singleton) { |
85 | self::$singleton = $singleton; | |
86 | } | |
87 | ||
e0ef6999 EM |
88 | /** |
89 | * @param array $parameters | |
6a2d9900 TO |
90 | * List of configuration values required by the extension system. |
91 | * Missing values will be guessed based on $config. | |
e0ef6999 | 92 | */ |
6a488035 TO |
93 | public function __construct($parameters = array()) { |
94 | $config = CRM_Core_Config::singleton(); | |
6a2d9900 TO |
95 | $configKeys = array( |
96 | 'extensionsDir', | |
97 | 'extensionsURL', | |
98 | 'resourceBase', | |
99 | 'userFrameworkBaseURL', | |
100 | ); | |
101 | foreach ($configKeys as $key) { | |
102 | if (!array_key_exists($key, $parameters)) { | |
103 | $parameters[$key] = $config->{$key}; | |
104 | } | |
105 | } | |
106 | if (!array_key_exists('civicrm_root', $parameters)) { | |
107 | $parameters['civicrm_root'] = $GLOBALS['civicrm_root']; | |
6a488035 | 108 | } |
6a2d9900 TO |
109 | if (!array_key_exists('cmsRootPath', $parameters)) { |
110 | $parameters['cmsRootPath'] = $config->userSystem->cmsRootPath(); | |
6a488035 | 111 | } |
adada472 TO |
112 | if (!array_key_exists('domain_id', $parameters)) { |
113 | $parameters['domain_id'] = CRM_Core_Config::domainID(); | |
114 | } | |
6a2d9900 TO |
115 | ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters)) |
116 | ||
6a488035 TO |
117 | $this->parameters = $parameters; |
118 | } | |
119 | ||
120 | /** | |
fe482240 | 121 | * Get a container which represents all available extensions. |
6a488035 TO |
122 | * |
123 | * @return CRM_Extension_Container_Interface | |
124 | */ | |
125 | public function getFullContainer() { | |
126 | if ($this->fullContainer === NULL) { | |
127 | $containers = array(); | |
128 | ||
129 | if ($this->getDefaultContainer()) { | |
130 | $containers['default'] = $this->getDefaultContainer(); | |
131 | } | |
132 | ||
6a2d9900 TO |
133 | $containers['civiroot'] = new CRM_Extension_Container_Basic( |
134 | $this->parameters['civicrm_root'], | |
135 | $this->parameters['resourceBase'], | |
136 | $this->getCache(), | |
137 | 'civiroot' | |
138 | ); | |
6a488035 TO |
139 | |
140 | // TODO: CRM_Extension_Container_Basic( /sites/all/modules ) | |
141 | // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules | |
142 | // TODO: CRM_Extension_Container_Basic( /modules ) | |
143 | // TODO: CRM_Extension_Container_Basic( /vendors ) | |
144 | ||
e29aefb4 | 145 | // At time of writing, D6, D7, and WP support cmsRootPath() but J does not |
6a2d9900 TO |
146 | if (NULL !== $this->parameters['cmsRootPath']) { |
147 | $vendorPath = $this->parameters['cmsRootPath'] . DIRECTORY_SEPARATOR . 'vendor'; | |
e29aefb4 | 148 | if (is_dir($vendorPath)) { |
6a2d9900 TO |
149 | $containers['cmsvendor'] = new CRM_Extension_Container_Basic( |
150 | $vendorPath, | |
151 | $this->parameters['userFrameworkBaseURL'] . DIRECTORY_SEPARATOR . 'vendor', | |
152 | $this->getCache(), | |
153 | 'cmsvendor' | |
154 | ); | |
e29aefb4 TO |
155 | } |
156 | } | |
157 | ||
6a488035 TO |
158 | $this->fullContainer = new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full'); |
159 | } | |
160 | return $this->fullContainer; | |
161 | } | |
162 | ||
163 | /** | |
fe482240 | 164 | * Get the container to which new extensions are installed. |
6a488035 TO |
165 | * |
166 | * This container should be a particular, writeable directory. | |
167 | * | |
70d331a6 | 168 | * @return CRM_Extension_Container_Default|FALSE (false if not configured) |
6a488035 TO |
169 | */ |
170 | public function getDefaultContainer() { | |
171 | if ($this->defaultContainer === NULL) { | |
172 | if ($this->parameters['extensionsDir']) { | |
70d331a6 | 173 | $this->defaultContainer = new CRM_Extension_Container_Default($this->parameters['extensionsDir'], $this->parameters['extensionsURL'], $this->getCache(), 'default'); |
8f24a46c TO |
174 | } |
175 | else { | |
6a488035 TO |
176 | $this->defaultContainer = FALSE; |
177 | } | |
178 | } | |
179 | return $this->defaultContainer; | |
180 | } | |
181 | ||
182 | /** | |
fe482240 | 183 | * Get the service which provides runtime information about extensions. |
6a488035 TO |
184 | * |
185 | * @return CRM_Extension_Mapper | |
186 | */ | |
187 | public function getMapper() { | |
188 | if ($this->mapper === NULL) { | |
189 | $this->mapper = new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper'); | |
190 | } | |
191 | return $this->mapper; | |
192 | } | |
193 | ||
194 | /** | |
fe482240 | 195 | * Get the service for enabling and disabling extensions. |
6a488035 TO |
196 | * |
197 | * @return CRM_Extension_Manager | |
198 | */ | |
199 | public function getManager() { | |
200 | if ($this->manager === NULL) { | |
201 | $typeManagers = array( | |
202 | 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()), | |
203 | 'report' => new CRM_Extension_Manager_Report(), | |
204 | 'search' => new CRM_Extension_Manager_Search(), | |
205 | 'module' => new CRM_Extension_Manager_Module($this->getMapper()), | |
206 | ); | |
207 | $this->manager = new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers); | |
208 | } | |
209 | return $this->manager; | |
210 | } | |
211 | ||
212 | /** | |
213 | * Get the service for finding remotely-available extensions | |
214 | * | |
215 | * @return CRM_Extension_Browser | |
216 | */ | |
217 | public function getBrowser() { | |
218 | if ($this->browser === NULL) { | |
219 | $cacheDir = NULL; | |
220 | if ($this->getDefaultContainer()) { | |
221 | $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR . 'cache'; | |
222 | } | |
223 | $this->browser = new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir); | |
224 | } | |
225 | return $this->browser; | |
226 | } | |
227 | ||
228 | /** | |
229 | * Get the service for loading code from remotely-available extensions | |
230 | * | |
231 | * @return CRM_Extension_Downloader | |
232 | */ | |
233 | public function getDownloader() { | |
234 | if ($this->downloader === NULL) { | |
235 | $basedir = ($this->getDefaultContainer() ? $this->getDefaultContainer()->getBaseDir() : NULL); | |
236 | $this->downloader = new CRM_Extension_Downloader( | |
237 | $this->getManager(), | |
238 | $basedir, | |
239 | CRM_Utils_File::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp'; | |
240 | ); | |
241 | } | |
242 | return $this->downloader; | |
243 | } | |
244 | ||
245 | /** | |
246 | * @return CRM_Utils_Cache_Interface | |
247 | */ | |
248 | public function getCache() { | |
249 | if ($this->cache === NULL) { | |
250 | if (defined('CIVICRM_DSN')) { | |
6a2d9900 | 251 | $cacheGroup = md5(serialize(array('ext', $this->parameters))); |
6a488035 | 252 | $this->cache = new CRM_Utils_Cache_SqlGroup(array( |
6a2d9900 | 253 | 'group' => $cacheGroup, |
6a488035 TO |
254 | 'prefetch' => TRUE, |
255 | )); | |
6a2d9900 TO |
256 | } |
257 | else { | |
6a488035 TO |
258 | $this->cache = new CRM_Utils_Cache_ArrayCache(array()); |
259 | } | |
260 | } | |
261 | return $this->cache; | |
262 | } | |
263 | ||
264 | /** | |
fe482240 | 265 | * Determine the URL which provides a feed of available extensions. |
6a488035 TO |
266 | * |
267 | * @return string|FALSE | |
268 | */ | |
269 | public function getRepositoryUrl() { | |
270 | if (empty($this->_repoUrl) && $this->_repoUrl !== FALSE) { | |
271 | $config = CRM_Core_Config::singleton(); | |
272 | $url = CRM_Core_BAO_Setting::getItem('Extension Preferences', 'ext_repo_url', NULL, CRM_Extension_Browser::DEFAULT_EXTENSIONS_REPOSITORY); | |
273 | ||
274 | // boolean false means don't try to check extensions | |
275 | // http://issues.civicrm.org/jira/browse/CRM-10575 | |
8f24a46c | 276 | if ($url === FALSE) { |
b3a4b879 | 277 | $this->_repoUrl = FALSE; |
6a488035 TO |
278 | } |
279 | else { | |
efceedd4 | 280 | $this->_repoUrl = CRM_Utils_System::evalUrl($url); |
6a488035 TO |
281 | } |
282 | } | |
283 | return $this->_repoUrl; | |
284 | } | |
96025800 | 285 | |
6a488035 | 286 | } |