3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This class glues together the various parts of the extension
33 * @copyright CiviCRM LLC (c) 2004-2015
37 class CRM_Extension_System
{
38 private static $singleton;
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;
49 * The URL of the remote extensions repository.
53 private $_repoUrl = NULL;
57 * Construction parameters. These are primarily retained so
58 * that they can influence the cache name.
60 protected $parameters;
64 * TRUE to force creation of a new system.
66 * @return CRM_Extension_System
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
);
74 self
::$singleton = new CRM_Extension_System();
77 return self
::$singleton;
81 * @param CRM_Extension_System $singleton
82 * The new, singleton extension system.
84 public static function setSingleton(CRM_Extension_System
$singleton) {
85 self
::$singleton = $singleton;
89 * @param array $parameters
90 * List of configuration values required by the extension system.
91 * Missing values will be guessed based on $config.
93 public function __construct($parameters = array()) {
94 $config = CRM_Core_Config
::singleton();
99 'userFrameworkBaseURL',
101 foreach ($configKeys as $key) {
102 if (!array_key_exists($key, $parameters)) {
103 $parameters[$key] = $config->{$key};
106 if (!array_key_exists('civicrm_root', $parameters)) {
107 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
109 if (!array_key_exists('cmsRootPath', $parameters)) {
110 $parameters['cmsRootPath'] = $config->userSystem
->cmsRootPath();
112 if (!array_key_exists('domain_id', $parameters)) {
113 $parameters['domain_id'] = CRM_Core_Config
::domainID();
115 ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters))
117 $this->parameters
= $parameters;
121 * Get a container which represents all available extensions.
123 * @return CRM_Extension_Container_Interface
125 public function getFullContainer() {
126 if ($this->fullContainer
=== NULL) {
127 $containers = array();
129 if ($this->getDefaultContainer()) {
130 $containers['default'] = $this->getDefaultContainer();
133 $containers['civiroot'] = new CRM_Extension_Container_Basic(
134 $this->parameters
['civicrm_root'],
135 $this->parameters
['resourceBase'],
140 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
141 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
142 // TODO: CRM_Extension_Container_Basic( /modules )
143 // TODO: CRM_Extension_Container_Basic( /vendors )
145 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
146 if (NULL !== $this->parameters
['cmsRootPath']) {
147 $vendorPath = $this->parameters
['cmsRootPath'] . DIRECTORY_SEPARATOR
. 'vendor';
148 if (is_dir($vendorPath)) {
149 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
151 $this->parameters
['userFrameworkBaseURL'] . DIRECTORY_SEPARATOR
. 'vendor',
158 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
160 return $this->fullContainer
;
164 * Get the container to which new extensions are installed.
166 * This container should be a particular, writeable directory.
168 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
170 public function getDefaultContainer() {
171 if ($this->defaultContainer
=== NULL) {
172 if ($this->parameters
['extensionsDir']) {
173 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
176 $this->defaultContainer
= FALSE;
179 return $this->defaultContainer
;
183 * Get the service which provides runtime information about extensions.
185 * @return CRM_Extension_Mapper
187 public function getMapper() {
188 if ($this->mapper
=== NULL) {
189 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
191 return $this->mapper
;
195 * Get the service for enabling and disabling extensions.
197 * @return CRM_Extension_Manager
199 public function getManager() {
200 if ($this->manager
=== NULL) {
201 $typeManagers = array(
202 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
203 'report' => new CRM_Extension_Manager_Report(),
204 'search' => new CRM_Extension_Manager_Search(),
205 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
207 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
209 return $this->manager
;
213 * Get the service for finding remotely-available extensions
215 * @return CRM_Extension_Browser
217 public function getBrowser() {
218 if ($this->browser
=== NULL) {
220 if ($this->getDefaultContainer()) {
221 $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR
. 'cache';
223 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
225 return $this->browser
;
229 * Get the service for loading code from remotely-available extensions
231 * @return CRM_Extension_Downloader
233 public function getDownloader() {
234 if ($this->downloader
=== NULL) {
235 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
236 $this->downloader
= new CRM_Extension_Downloader(
239 CRM_Utils_File
::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
242 return $this->downloader
;
246 * @return CRM_Utils_Cache_Interface
248 public function getCache() {
249 if ($this->cache
=== NULL) {
250 if (defined('CIVICRM_DSN')) {
251 $cacheGroup = md5(serialize(array('ext', $this->parameters
)));
252 $this->cache
= new CRM_Utils_Cache_SqlGroup(array(
253 'group' => $cacheGroup,
258 $this->cache
= new CRM_Utils_Cache_ArrayCache(array());
265 * Determine the URL which provides a feed of available extensions.
267 * @return string|FALSE
269 public function getRepositoryUrl() {
270 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
271 $config = CRM_Core_Config
::singleton();
272 $url = CRM_Core_BAO_Setting
::getItem('Extension Preferences', 'ext_repo_url', NULL, CRM_Extension_Browser
::DEFAULT_EXTENSIONS_REPOSITORY
);
274 // boolean false means don't try to check extensions
275 // http://issues.civicrm.org/jira/browse/CRM-10575
276 if ($url === FALSE) {
277 $this->_repoUrl
= FALSE;
280 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
283 return $this->_repoUrl
;