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