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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2015
35 * An extension container is a locally-accessible source tree which can be
36 * scanned for extensions.
38 class CRM_Extension_Container_Basic
implements CRM_Extension_Container_Interface
{
43 * Note: Treat as private. This is only public to facilitate debugging.
50 * Note: Treat as private. This is only public to facilitate debugging.
55 * @var CRM_Utils_Cache_Interface|NULL
57 * Note: Treat as private. This is only public to facilitate debugging.
62 * @var string the cache key used for any data stored by this container
64 * Note: Treat as private. This is only public to facilitate debugging.
69 * @var array($key => $relPath)
71 * Note: Treat as private. This is only public to facilitate debugging.
73 public $relPaths = FALSE;
76 * @var array($key => $relUrl)
78 * Derived from $relPaths. On Unix systems (where file-paths and
79 * URL-paths both use '/' separator), this isn't necessary. On Windows
80 * systems, this is derived from $relPaths.
82 * Note: Treat as private. This is only public to facilitate debugging.
84 public $relUrls = FALSE;
87 * @param string $baseDir
88 * Local path to the container.
89 * @param string $baseUrl
90 * Public URL of the container.
91 * @param CRM_Utils_Cache_Interface $cache
92 * Cache in which to store extension metadata.
93 * @param string $cacheKey
94 * Unique name for this container.
96 public function __construct($baseDir, $baseUrl, CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL) {
97 $this->cache
= $cache;
98 $this->cacheKey
= $cacheKey;
99 $this->baseDir
= rtrim($baseDir, '/');
100 $this->baseUrl
= rtrim($baseUrl, '/');
106 public function checkRequirements() {
109 if (empty($this->baseDir
) ||
!is_dir($this->baseDir
)) {
111 'title' => ts('Invalid Base Directory'),
112 'message' => ts('An extension container has been defined with a blank directory.'),
115 if (empty($this->baseUrl
)) {
117 'title' => ts('Invalid Base URL'),
118 'message' => ts('An extension container has been defined with a blank URL.'),
128 public function getKeys() {
129 return array_keys($this->getRelPaths());
135 public function getPath($key) {
136 return $this->baseDir
. $this->getRelPath($key);
142 public function getResUrl($key) {
143 if (!$this->baseUrl
) {
144 CRM_Core_Session
::setStatus(
145 ts('Failed to determine URL for extension (%1). Please update <a href="%2">Resource URLs</a>.',
148 2 => CRM_Utils_System
::url('civicrm/admin/setting/url', 'reset=1'),
153 return $this->baseUrl
. $this->getRelUrl($key);
159 public function refresh() {
160 $this->relPaths
= NULL;
162 $this->cache
->delete($this->cacheKey
);
169 public function getBaseDir() {
170 return $this->baseDir
;
174 * Determine the relative path of an extension directory.
179 * @throws CRM_Extension_Exception_MissingException
182 protected function getRelPath($key) {
183 $keypaths = $this->getRelPaths();
184 if (!isset($keypaths[$key])) {
185 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
187 return $keypaths[$key];
191 * Scan $basedir for a list of extension-keys
196 protected function getRelPaths() {
197 if (!is_array($this->relPaths
)) {
199 $this->relPaths
= $this->cache
->get($this->cacheKey
);
201 if (!is_array($this->relPaths
)) {
202 $this->relPaths
= array();
203 $infoPaths = CRM_Utils_File
::findFiles($this->baseDir
, 'info.xml');
204 foreach ($infoPaths as $infoPath) {
205 $relPath = CRM_Utils_File
::relativize(dirname($infoPath), $this->baseDir
);
207 $info = CRM_Extension_Info
::loadFromFile($infoPath);
209 catch (CRM_Extension_Exception_ParseException
$e) {
210 CRM_Core_Session
::setStatus(ts('Parse error in extension: %1', array(
211 1 => $e->getMessage(),
213 CRM_Core_Error
::debug_log_message("Parse error in extension: " . $e->getMessage());
216 $this->relPaths
[$info->key
] = $relPath;
219 $this->cache
->set($this->cacheKey
, $this->relPaths
);
223 return $this->relPaths
;
227 * Determine the relative path of an extension directory.
232 * @throws CRM_Extension_Exception_MissingException
235 protected function getRelUrl($key) {
236 $relUrls = $this->getRelUrls();
237 if (!isset($relUrls[$key])) {
238 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
240 return $relUrls[$key];
244 * Scan $basedir for a list of extension-keys
249 protected function getRelUrls() {
250 if (DIRECTORY_SEPARATOR
== '/') {
251 return $this->getRelPaths();
253 if (!is_array($this->relUrls
)) {
254 $this->relUrls
= self
::convertPathsToUrls(DIRECTORY_SEPARATOR
, $this->getRelPaths());
256 return $this->relUrls
;
260 * Convert a list of relative paths to relative URLs.
262 * Note: Treat as private. This is only public to facilitate testing.
264 * @param string $dirSep
265 * Directory separator ("/" or "\").
266 * @param array $relPaths
267 * Array($key => $relPath).
269 * Array($key => $relUrl).
271 public static function convertPathsToUrls($dirSep, $relPaths) {
273 foreach ($relPaths as $key => $relPath) {
274 $relUrls[$key] = str_replace($dirSep, '/', $relPath);