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;
57 * TRUE to force creation of a new system.
59 * @return CRM_Extension_System
61 public static function singleton($fresh = FALSE) {
62 if (!self
::$singleton ||
$fresh) {
63 if (self
::$singleton) {
64 self
::$singleton = new CRM_Extension_System(self
::$singleton->parameters
);
67 self
::$singleton = new CRM_Extension_System();
70 return self
::$singleton;
74 * @param CRM_Extension_System $singleton
75 * The new, singleton extension system.
77 public static function setSingleton(CRM_Extension_System
$singleton) {
78 self
::$singleton = $singleton;
82 * @param array $parameters
83 * List of configuration values required by the extension system.
84 * Missing values will be guessed based on $config.
86 public function __construct($parameters = array()) {
87 $config = CRM_Core_Config
::singleton();
92 'userFrameworkBaseURL',
94 foreach ($configKeys as $key) {
95 if (!array_key_exists($key, $parameters)) {
96 $parameters[$key] = $config->{$key};
99 if (!array_key_exists('civicrm_root', $parameters)) {
100 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
102 if (!array_key_exists('cmsRootPath', $parameters)) {
103 $parameters['cmsRootPath'] = $config->userSystem
->cmsRootPath();
105 ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters))
107 $this->parameters
= $parameters;
111 * Get a container which represents all available extensions
113 * @return CRM_Extension_Container_Interface
115 public function getFullContainer() {
116 if ($this->fullContainer
=== NULL) {
117 $containers = array();
119 if ($this->getDefaultContainer()) {
120 $containers['default'] = $this->getDefaultContainer();
123 $containers['civiroot'] = new CRM_Extension_Container_Basic(
124 $this->parameters
['civicrm_root'],
125 $this->parameters
['resourceBase'],
130 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
131 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
132 // TODO: CRM_Extension_Container_Basic( /modules )
133 // TODO: CRM_Extension_Container_Basic( /vendors )
135 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
136 if (NULL !== $this->parameters
['cmsRootPath']) {
137 $vendorPath = $this->parameters
['cmsRootPath'] . DIRECTORY_SEPARATOR
. 'vendor';
138 if (is_dir($vendorPath)) {
139 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
141 $this->parameters
['userFrameworkBaseURL'] . DIRECTORY_SEPARATOR
. 'vendor',
148 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
150 return $this->fullContainer
;
154 * Get the container to which new extensions are installed
156 * This container should be a particular, writeable directory.
158 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
160 public function getDefaultContainer() {
161 if ($this->defaultContainer
=== NULL) {
162 if ($this->parameters
['extensionsDir']) {
163 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
166 $this->defaultContainer
= FALSE;
169 return $this->defaultContainer
;
173 * Get the service which provides runtime information about extensions
175 * @return CRM_Extension_Mapper
177 public function getMapper() {
178 if ($this->mapper
=== NULL) {
179 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
181 return $this->mapper
;
185 * Get the service for enabling and disabling extensions
187 * @return CRM_Extension_Manager
189 public function getManager() {
190 if ($this->manager
=== NULL) {
191 $typeManagers = array(
192 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
193 'report' => new CRM_Extension_Manager_Report(),
194 'search' => new CRM_Extension_Manager_Search(),
195 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
197 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
199 return $this->manager
;
203 * Get the service for finding remotely-available extensions
205 * @return CRM_Extension_Browser
207 public function getBrowser() {
208 if ($this->browser
=== NULL) {
210 if ($this->getDefaultContainer()) {
211 $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR
. 'cache';
213 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
215 return $this->browser
;
219 * Get the service for loading code from remotely-available extensions
221 * @return CRM_Extension_Downloader
223 public function getDownloader() {
224 if ($this->downloader
=== NULL) {
225 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
226 $this->downloader
= new CRM_Extension_Downloader(
229 CRM_Utils_File
::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
232 return $this->downloader
;
236 * @return CRM_Utils_Cache_Interface
238 public function getCache() {
239 if ($this->cache
=== NULL) {
240 if (defined('CIVICRM_DSN')) {
241 $cacheGroup = md5(serialize(array('ext', $this->parameters
)));
242 $this->cache
= new CRM_Utils_Cache_SqlGroup(array(
243 'group' => $cacheGroup,
248 $this->cache
= new CRM_Utils_Cache_ArrayCache(array());
255 * Determine the URL which provides a feed of available extensions
257 * @return string|FALSE
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
);
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;
270 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
273 return $this->_repoUrl
;