3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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;
58 * @return CRM_Extension_System
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
);
66 self
::$singleton = new CRM_Extension_System();
69 return self
::$singleton;
73 * @param CRM_Extension_System $singleton
75 public static function setSingleton(CRM_Extension_System
$singleton) {
76 self
::$singleton = $singleton;
80 * @param array $parameters
82 public function __construct($parameters = array()) {
83 $config = CRM_Core_Config
::singleton();
84 if (!array_key_exists('extensionsDir', $parameters)) {
85 $parameters['extensionsDir'] = $config->extensionsDir
;
87 if (!array_key_exists('extensionsURL', $parameters)) {
88 $parameters['extensionsURL'] = $config->extensionsURL
;
90 $this->parameters
= $parameters;
94 * Get a container which represents all available extensions
96 * @return CRM_Extension_Container_Interface
98 public function getFullContainer() {
99 if ($this->fullContainer
=== NULL) {
100 $containers = array();
102 if ($this->getDefaultContainer()) {
103 $containers['default'] = $this->getDefaultContainer();
106 $config = CRM_Core_Config
::singleton();
107 global $civicrm_root;
108 $containers['civiroot'] = new CRM_Extension_Container_Basic($civicrm_root, $config->resourceBase
, $this->getCache(), 'civiroot');
110 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
111 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
112 // TODO: CRM_Extension_Container_Basic( /modules )
113 // TODO: CRM_Extension_Container_Basic( /vendors )
115 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
116 $cmsRootPath = $config->userSystem
->cmsRootPath();
117 if (NULL !== $cmsRootPath) {
118 $vendorPath = $cmsRootPath . DIRECTORY_SEPARATOR
. 'vendor';
119 if (is_dir($vendorPath)) {
120 $containers['cmsvendor'] = new CRM_Extension_Container_Basic($vendorPath, $config->userFrameworkBaseURL
. DIRECTORY_SEPARATOR
. 'vendor', $this->getCache(), 'cmsvendor');
124 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
126 return $this->fullContainer
;
130 * Get the container to which new extensions are installed
132 * This container should be a particular, writeable directory.
134 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
136 public function getDefaultContainer() {
137 if ($this->defaultContainer
=== NULL) {
138 if ($this->parameters
['extensionsDir']) {
139 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
142 $this->defaultContainer
= FALSE;
145 return $this->defaultContainer
;
149 * Get the service which provides runtime information about extensions
151 * @return CRM_Extension_Mapper
153 public function getMapper() {
154 if ($this->mapper
=== NULL) {
155 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
157 return $this->mapper
;
161 * Get the service for enabling and disabling extensions
163 * @return CRM_Extension_Manager
165 public function getManager() {
166 if ($this->manager
=== NULL) {
167 $typeManagers = array(
168 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
169 'report' => new CRM_Extension_Manager_Report(),
170 'search' => new CRM_Extension_Manager_Search(),
171 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
173 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
175 return $this->manager
;
179 * Get the service for finding remotely-available extensions
181 * @return CRM_Extension_Browser
183 public function getBrowser() {
184 if ($this->browser
=== NULL) {
186 if ($this->getDefaultContainer()) {
187 $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR
. 'cache';
189 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
191 return $this->browser
;
195 * Get the service for loading code from remotely-available extensions
197 * @return CRM_Extension_Downloader
199 public function getDownloader() {
200 if ($this->downloader
=== NULL) {
201 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
202 $this->downloader
= new CRM_Extension_Downloader(
205 CRM_Utils_File
::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
208 return $this->downloader
;
212 * @return CRM_Utils_Cache_Interface
214 public function getCache() {
215 if ($this->cache
=== NULL) {
216 if (defined('CIVICRM_DSN')) {
217 $this->cache
= new CRM_Utils_Cache_SqlGroup(array(
223 $this->cache
= new CRM_Utils_Cache_ArrayCache(array());
230 * Determine the URL which provides a feed of available extensions
232 * @return string|FALSE
234 public function getRepositoryUrl() {
235 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
236 $config = CRM_Core_Config
::singleton();
237 $url = CRM_Core_BAO_Setting
::getItem('Extension Preferences', 'ext_repo_url', NULL, CRM_Extension_Browser
::DEFAULT_EXTENSIONS_REPOSITORY
);
239 // boolean false means don't try to check extensions
240 // http://issues.civicrm.org/jira/browse/CRM-10575
241 if ($url === FALSE) {
242 $this->_repoUrl
= FALSE;
245 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
248 return $this->_repoUrl
;