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