Merge pull request #6597 from jitendrapurohit/CRM-12078
[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 $parameters['extensionsDir'] = CRM_Utils_Array::value('extensionsDir', $parameters, $config->extensionsDir);
96 $parameters['extensionsURL'] = CRM_Utils_Array::value('extensionsURL', $parameters, $config->extensionsURL);
97 $parameters['resourceBase'] = CRM_Utils_Array::value('resourceBase', $parameters, $config->resourceBase);
98 $parameters['userFrameworkBaseURL'] = CRM_Utils_Array::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL);
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 if (!array_key_exists('domain_id', $parameters)) {
106 $parameters['domain_id'] = CRM_Core_Config::domainID();
107 }
108 ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters))
109
110 $this->parameters = $parameters;
111 }
112
113 /**
114 * Get a container which represents all available extensions.
115 *
116 * @return CRM_Extension_Container_Interface
117 */
118 public function getFullContainer() {
119 if ($this->fullContainer === NULL) {
120 $containers = array();
121
122 if ($this->getDefaultContainer()) {
123 $containers['default'] = $this->getDefaultContainer();
124 }
125
126 $containers['civiroot'] = new CRM_Extension_Container_Basic(
127 $this->parameters['civicrm_root'],
128 $this->parameters['resourceBase'],
129 $this->getCache(),
130 'civiroot'
131 );
132
133 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
134 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
135 // TODO: CRM_Extension_Container_Basic( /modules )
136 // TODO: CRM_Extension_Container_Basic( /vendors )
137
138 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
139 if (NULL !== $this->parameters['cmsRootPath']) {
140 $vendorPath = $this->parameters['cmsRootPath'] . DIRECTORY_SEPARATOR . 'vendor';
141 if (is_dir($vendorPath)) {
142 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
143 $vendorPath,
144 $this->parameters['userFrameworkBaseURL'] . DIRECTORY_SEPARATOR . 'vendor',
145 $this->getCache(),
146 'cmsvendor'
147 );
148 }
149 }
150
151 $this->fullContainer = new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
152 }
153 return $this->fullContainer;
154 }
155
156 /**
157 * Get the container to which new extensions are installed.
158 *
159 * This container should be a particular, writeable directory.
160 *
161 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
162 */
163 public function getDefaultContainer() {
164 if ($this->defaultContainer === NULL) {
165 if ($this->parameters['extensionsDir']) {
166 $this->defaultContainer = new CRM_Extension_Container_Default($this->parameters['extensionsDir'], $this->parameters['extensionsURL'], $this->getCache(), 'default');
167 }
168 else {
169 $this->defaultContainer = FALSE;
170 }
171 }
172 return $this->defaultContainer;
173 }
174
175 /**
176 * Get the service which provides runtime information about extensions.
177 *
178 * @return CRM_Extension_Mapper
179 */
180 public function getMapper() {
181 if ($this->mapper === NULL) {
182 $this->mapper = new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
183 }
184 return $this->mapper;
185 }
186
187 /**
188 * Get the service for enabling and disabling extensions.
189 *
190 * @return CRM_Extension_Manager
191 */
192 public function getManager() {
193 if ($this->manager === NULL) {
194 $typeManagers = array(
195 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
196 'report' => new CRM_Extension_Manager_Report(),
197 'search' => new CRM_Extension_Manager_Search(),
198 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
199 );
200 $this->manager = new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
201 }
202 return $this->manager;
203 }
204
205 /**
206 * Get the service for finding remotely-available extensions
207 *
208 * @return CRM_Extension_Browser
209 */
210 public function getBrowser() {
211 if ($this->browser === NULL) {
212 $cacheDir = NULL;
213 if ($this->getDefaultContainer()) {
214 $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR . 'cache';
215 }
216 $this->browser = new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
217 }
218 return $this->browser;
219 }
220
221 /**
222 * Get the service for loading code from remotely-available extensions
223 *
224 * @return CRM_Extension_Downloader
225 */
226 public function getDownloader() {
227 if ($this->downloader === NULL) {
228 $basedir = ($this->getDefaultContainer() ? $this->getDefaultContainer()->getBaseDir() : NULL);
229 $this->downloader = new CRM_Extension_Downloader(
230 $this->getManager(),
231 $basedir,
232 CRM_Utils_File::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
233 );
234 }
235 return $this->downloader;
236 }
237
238 /**
239 * @return CRM_Utils_Cache_Interface
240 */
241 public function getCache() {
242 if ($this->cache === NULL) {
243 $cacheGroup = md5(serialize(array('ext', $this->parameters)));
244 // Extension system starts before container. Manage our own cache.
245 $this->cache = CRM_Utils_Cache::create(array(
246 'name' => $cacheGroup,
247 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'),
248 'prefetch' => TRUE,
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 }