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