commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Extension / System.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * @param bool $fresh
57 * TRUE to force creation of a new system.
58 *
59 * @return CRM_Extension_System
60 */
61 public static function singleton($fresh = FALSE) {
62 if (!self::$singleton || $fresh) {
63 if (self::$singleton) {
64 self::$singleton = new CRM_Extension_System(self::$singleton->parameters);
65 }
66 else {
67 self::$singleton = new CRM_Extension_System();
68 }
69 }
70 return self::$singleton;
71 }
72
73 /**
74 * @param CRM_Extension_System $singleton
75 * The new, singleton extension system.
76 */
77 public static function setSingleton(CRM_Extension_System $singleton) {
78 self::$singleton = $singleton;
79 }
80
81 /**
82 * @param array $parameters
83 * List of configuration values required by the extension system.
84 * Missing values will be guessed based on $config.
85 */
86 public function __construct($parameters = array()) {
87 $config = CRM_Core_Config::singleton();
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'];
101 }
102 if (!array_key_exists('cmsRootPath', $parameters)) {
103 $parameters['cmsRootPath'] = $config->userSystem->cmsRootPath();
104 }
105 ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters))
106
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
123 $containers['civiroot'] = new CRM_Extension_Container_Basic(
124 $this->parameters['civicrm_root'],
125 $this->parameters['resourceBase'],
126 $this->getCache(),
127 'civiroot'
128 );
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
135 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
136 if (NULL !== $this->parameters['cmsRootPath']) {
137 $vendorPath = $this->parameters['cmsRootPath'] . DIRECTORY_SEPARATOR . 'vendor';
138 if (is_dir($vendorPath)) {
139 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
140 $vendorPath,
141 $this->parameters['userFrameworkBaseURL'] . DIRECTORY_SEPARATOR . 'vendor',
142 $this->getCache(),
143 'cmsvendor'
144 );
145 }
146 }
147
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 *
158 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
159 */
160 public function getDefaultContainer() {
161 if ($this->defaultContainer === NULL) {
162 if ($this->parameters['extensionsDir']) {
163 $this->defaultContainer = new CRM_Extension_Container_Default($this->parameters['extensionsDir'], $this->parameters['extensionsURL'], $this->getCache(), 'default');
164 }
165 else {
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')) {
241 $cacheGroup = md5(serialize(array('ext', $this->parameters)));
242 $this->cache = new CRM_Utils_Cache_SqlGroup(array(
243 'group' => $cacheGroup,
244 'prefetch' => TRUE,
245 ));
246 }
247 else {
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
266 if ($url === FALSE) {
267 $this->_repoUrl = FALSE;
268 }
269 else {
270 $this->_repoUrl = CRM_Utils_System::evalUrl($url);
271 }
272 }
273 return $this->_repoUrl;
274 }
275
276 }