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
35 class CRM_Extension_System
{
36 private static $singleton;
38 private $cache = NULL;
39 private $fullContainer = NULL;
40 private $defaultContainer = NULL;
41 private $mapper = NULL;
42 private $manager = NULL;
43 private $browser = NULL;
44 private $downloader = NULL;
47 * The URL of the remote extensions repository.
51 private $_repoUrl = NULL;
55 * Construction parameters. These are primarily retained so
56 * that they can influence the cache name.
58 protected $parameters;
62 * TRUE to force creation of a new system.
64 * @return CRM_Extension_System
66 public static function singleton($fresh = FALSE) {
67 if (!self
::$singleton ||
$fresh) {
68 if (self
::$singleton) {
69 self
::$singleton = new CRM_Extension_System(self
::$singleton->parameters
);
72 self
::$singleton = new CRM_Extension_System();
75 return self
::$singleton;
79 * @param CRM_Extension_System $singleton
80 * The new, singleton extension system.
82 public static function setSingleton(CRM_Extension_System
$singleton) {
83 self
::$singleton = $singleton;
87 * @param array $parameters
88 * List of configuration values required by the extension system.
89 * Missing values will be guessed based on $config.
91 public function __construct($parameters = array()) {
92 $config = CRM_Core_Config
::singleton();
93 $parameters['extensionsDir'] = CRM_Utils_Array
::value('extensionsDir', $parameters, $config->extensionsDir
);
94 $parameters['extensionsURL'] = CRM_Utils_Array
::value('extensionsURL', $parameters, $config->extensionsURL
);
95 $parameters['resourceBase'] = CRM_Utils_Array
::value('resourceBase', $parameters, $config->resourceBase
);
96 $parameters['userFrameworkBaseURL'] = CRM_Utils_Array
::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL
);
97 if (!array_key_exists('civicrm_root', $parameters)) {
98 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
100 if (!array_key_exists('cmsRootPath', $parameters)) {
101 $parameters['cmsRootPath'] = $config->userSystem
->cmsRootPath();
103 if (!array_key_exists('domain_id', $parameters)) {
104 $parameters['domain_id'] = CRM_Core_Config
::domainID();
106 ksort($parameters); // guaranteed ordering - useful for md5(serialize($parameters))
108 $this->parameters
= $parameters;
112 * Get a container which represents all available extensions.
114 * @return CRM_Extension_Container_Interface
116 public function getFullContainer() {
117 if ($this->fullContainer
=== NULL) {
118 $containers = array();
120 if ($this->getDefaultContainer()) {
121 $containers['default'] = $this->getDefaultContainer();
124 $containers['civiroot'] = new CRM_Extension_Container_Basic(
125 $this->parameters
['civicrm_root'],
126 $this->parameters
['resourceBase'],
131 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
132 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
133 // TODO: CRM_Extension_Container_Basic( /modules )
134 // TODO: CRM_Extension_Container_Basic( /vendors )
136 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
137 if (NULL !== $this->parameters
['cmsRootPath']) {
138 $vendorPath = $this->parameters
['cmsRootPath'] . DIRECTORY_SEPARATOR
. 'vendor';
139 if (is_dir($vendorPath)) {
140 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
142 $this->parameters
['userFrameworkBaseURL'] . DIRECTORY_SEPARATOR
. 'vendor',
149 $this->fullContainer
= new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
151 return $this->fullContainer
;
155 * Get the container to which new extensions are installed.
157 * This container should be a particular, writeable directory.
159 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
161 public function getDefaultContainer() {
162 if ($this->defaultContainer
=== NULL) {
163 if ($this->parameters
['extensionsDir']) {
164 $this->defaultContainer
= new CRM_Extension_Container_Default($this->parameters
['extensionsDir'], $this->parameters
['extensionsURL'], $this->getCache(), 'default');
167 $this->defaultContainer
= FALSE;
170 return $this->defaultContainer
;
174 * Get the service which provides runtime information about extensions.
176 * @return CRM_Extension_Mapper
178 public function getMapper() {
179 if ($this->mapper
=== NULL) {
180 $this->mapper
= new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
182 return $this->mapper
;
186 * Get the service for enabling and disabling extensions.
188 * @return CRM_Extension_Manager
190 public function getManager() {
191 if ($this->manager
=== NULL) {
192 $typeManagers = array(
193 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
194 'report' => new CRM_Extension_Manager_Report(),
195 'search' => new CRM_Extension_Manager_Search(),
196 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
198 $this->manager
= new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
200 return $this->manager
;
204 * Get the service for finding remotely-available extensions
206 * @return CRM_Extension_Browser
208 public function getBrowser() {
209 if ($this->browser
=== NULL) {
211 if ($this->getDefaultContainer()) {
212 $cacheDir = $this->getDefaultContainer()->getBaseDir() . DIRECTORY_SEPARATOR
. 'cache';
214 $this->browser
= new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
216 return $this->browser
;
220 * Get the service for loading code from remotely-available extensions
222 * @return CRM_Extension_Downloader
224 public function getDownloader() {
225 if ($this->downloader
=== NULL) {
226 $basedir = ($this->getDefaultContainer() ?
$this->getDefaultContainer()->getBaseDir() : NULL);
227 $this->downloader
= new CRM_Extension_Downloader(
230 CRM_Utils_File
::tempdir() // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
233 return $this->downloader
;
237 * @return CRM_Utils_Cache_Interface
239 public function getCache() {
240 if ($this->cache
=== NULL) {
241 $cacheGroup = md5(serialize(array('ext', $this->parameters
)));
242 // Extension system starts before container. Manage our own cache.
243 $this->cache
= CRM_Utils_Cache
::create(array(
244 'name' => $cacheGroup,
245 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'),
253 * Determine the URL which provides a feed of available extensions.
255 * @return string|FALSE
257 public function getRepositoryUrl() {
258 if (empty($this->_repoUrl
) && $this->_repoUrl
!== FALSE) {
259 $url = Civi
::settings()->get('ext_repo_url');
261 // boolean false means don't try to check extensions
263 if ($url === FALSE) {
264 $this->_repoUrl
= FALSE;
267 $this->_repoUrl
= CRM_Utils_System
::evalUrl($url);
270 return $this->_repoUrl
;