3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
);
65 self
::$singleton = new CRM_Extension_System();
68 return self
::$singleton;
71 public static function setSingleton(CRM_Extension_System
$singleton) {
72 self
::$singleton = $singleton;
75 public function __construct($parameters = array()) {
76 $config = CRM_Core_Config
::singleton();
77 if (!array_key_exists('extensionsDir', $parameters)) {
78 $parameters['extensionsDir'] = $config->extensionsDir
;
80 if (!array_key_exists('extensionsURL', $parameters)) {
81 $parameters['extensionsURL'] = $config->extensionsURL
;
83 $this->parameters
= $parameters;
87 * Get a container which represents all available extensions
89 * @return CRM_Extension_Container_Interface
91 public function getFullContainer() {
92 if ($this->fullContainer
=== NULL) {
93 $containers = array();
95 if ($this->getDefaultContainer()) {
96 $containers['default'] = $this->getDefaultContainer();
99 $config = CRM_Core_Config
::singleton();
100 global $civicrm_root;
101 $containers['civiroot'] = new CRM_Extension_Container_Basic($civicrm_root, $config->resourceBase
, $this->getCache(), 'civiroot');
103 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
104 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
105 // TODO: CRM_Extension_Container_Basic( /modules )
106 // TODO: CRM_Extension_Container_Basic( /vendors )
108 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
109 $cmsRootPath = $config->userSystem
->cmsRootPath();
110 if (NULL !== $cmsRootPath) {
111 $vendorPath = $cmsRootPath . DIRECTORY_SEPARATOR
. 'vendor';
112 if (is_dir($vendorPath)) {
113 $containers['cmsvendor'] = new CRM_Extension_Container_Basic($vendorPath, $config->userFrameworkBaseURL
. DIRECTORY_SEPARATOR
. 'vendor', $this->getCache(), 'cmsvendor');
117 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
119 return $this->fullContainer
;
123 * Get the container to which new extensions are installed
125 * This container should be a particular, writeable directory.
127 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
129 public function getDefaultContainer() {
130 if ($this->defaultContainer
=== NULL) {
131 if ($this->parameters
['extensionsDir']) {
132 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
134 $this->defaultContainer
= FALSE;
137 return $this->defaultContainer
;
141 * Get the service which provides runtime information about extensions
143 * @return CRM_Extension_Mapper
145 public function getMapper() {
146 if ($this->mapper
=== NULL) {
147 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
149 return $this->mapper
;
153 * Get the service for enabling and disabling extensions
155 * @return CRM_Extension_Manager
157 public function getManager() {
158 if ($this->manager
=== NULL) {
159 $typeManagers = array(
160 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
161 'report' => new CRM_Extension_Manager_Report(),
162 'search' => new CRM_Extension_Manager_Search(),
163 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
165 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
167 return $this->manager
;
171 * Get the service for finding remotely-available extensions
173 * @return CRM_Extension_Browser
175 public function getBrowser() {
176 if ($this->browser
=== NULL) {
178 if ($this->getDefaultContainer()) {
179 $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR
. 'cache';
181 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
183 return $this->browser
;
187 * Get the service for loading code from remotely-available extensions
189 * @return CRM_Extension_Downloader
191 public function getDownloader() {
192 if ($this->downloader
=== NULL) {
193 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
194 $this->downloader
= new CRM_Extension_Downloader(
197 CRM_Utils_File
::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
200 return $this->downloader
;
204 * @return CRM_Utils_Cache_Interface
206 public function getCache() {
207 if ($this->cache
=== NULL) {
208 if (defined('CIVICRM_DSN')) {
209 $this->cache
= new CRM_Utils_Cache_SqlGroup(array(
214 $this->cache
= new CRM_Utils_Cache_ArrayCache(array());
221 * Determine the URL which provides a feed of available extensions
223 * @return string|FALSE
225 public function getRepositoryUrl() {
226 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
227 $config = CRM_Core_Config
::singleton();
228 $url = CRM_Core_BAO_Setting
::getItem('Extension Preferences', 'ext_repo_url', NULL, CRM_Extension_Browser
::DEFAULT_EXTENSIONS_REPOSITORY
);
230 // boolean false means don't try to check extensions
231 // http://issues.civicrm.org/jira/browse/CRM-10575
233 $this->_repoUrl
= false;
236 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
239 return $this->_repoUrl
;