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