Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Extension / System.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 glues together the various parts of the extension
30 * system.
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2015
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 /**
56 * @var array
57 * Construction parameters. These are primarily retained so
58 * that they can influence the cache name.
59 */
60 protected $parameters;
61
62 /**
63 * @param bool $fresh
64 * TRUE to force creation of a new system.
65 *
66 * @return CRM_Extension_System
67 */
68 public static function singleton($fresh = FALSE) {
69 if (!self::$singleton || $fresh) {
70 if (self::$singleton) {
71 self::$singleton = new CRM_Extension_System(self::$singleton->parameters);
72 }
73 else {
74 self::$singleton = new CRM_Extension_System();
75 }
76 }
77 return self::$singleton;
78 }
79
80 /**
81 * @param CRM_Extension_System $singleton
82 * The new, singleton extension system.
83 */
84 public static function setSingleton(CRM_Extension_System $singleton) {
85 self::$singleton = $singleton;
86 }
87
88 /**
89 * @param array $parameters
90 * List of configuration values required by the extension system.
91 * Missing values will be guessed based on $config.
92 */
93 public function __construct($parameters = array()) {
94 $config = CRM_Core_Config::singleton();
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'];
108 }
109 if (!array_key_exists('cmsRootPath', $parameters)) {
110 $parameters['cmsRootPath'] = $config->userSystem->cmsRootPath();
111 }
112 if (!array_key_exists('domain_id', $parameters)) {
113 $parameters['domain_id'] = CRM_Core_Config::domainID();
114 }
115 ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters))
116
117 $this->parameters = $parameters;
118 }
119
120 /**
121 * Get a container which represents all available extensions.
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
133 $containers['civiroot'] = new CRM_Extension_Container_Basic(
134 $this->parameters['civicrm_root'],
135 $this->parameters['resourceBase'],
136 $this->getCache(),
137 'civiroot'
138 );
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
145 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
146 if (NULL !== $this->parameters['cmsRootPath']) {
147 $vendorPath = $this->parameters['cmsRootPath'] . DIRECTORY_SEPARATOR . 'vendor';
148 if (is_dir($vendorPath)) {
149 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
150 $vendorPath,
151 $this->parameters['userFrameworkBaseURL'] . DIRECTORY_SEPARATOR . 'vendor',
152 $this->getCache(),
153 'cmsvendor'
154 );
155 }
156 }
157
158 $this->fullContainer = new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
159 }
160 return $this->fullContainer;
161 }
162
163 /**
164 * Get the container to which new extensions are installed.
165 *
166 * This container should be a particular, writeable directory.
167 *
168 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
169 */
170 public function getDefaultContainer() {
171 if ($this->defaultContainer === NULL) {
172 if ($this->parameters['extensionsDir']) {
173 $this->defaultContainer = new CRM_Extension_Container_Default($this->parameters['extensionsDir'], $this->parameters['extensionsURL'], $this->getCache(), 'default');
174 }
175 else {
176 $this->defaultContainer = FALSE;
177 }
178 }
179 return $this->defaultContainer;
180 }
181
182 /**
183 * Get the service which provides runtime information about extensions.
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 /**
195 * Get the service for enabling and disabling extensions.
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')) {
251 $cacheGroup = md5(serialize(array('ext', $this->parameters)));
252 $this->cache = new CRM_Utils_Cache_SqlGroup(array(
253 'group' => $cacheGroup,
254 'prefetch' => TRUE,
255 ));
256 }
257 else {
258 $this->cache = new CRM_Utils_Cache_ArrayCache(array());
259 }
260 }
261 return $this->cache;
262 }
263
264 /**
265 * Determine the URL which provides a feed of available extensions.
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
276 if ($url === FALSE) {
277 $this->_repoUrl = FALSE;
278 }
279 else {
280 $this->_repoUrl = CRM_Utils_System::evalUrl($url);
281 }
282 }
283 return $this->_repoUrl;
284 }
285
286 }