3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * An extension container is a locally-accessible source tree which can be
19 * scanned for extensions.
21 class CRM_Extension_Container_Basic
implements CRM_Extension_Container_Interface
{
26 * Note: Treat as private. This is only public to facilitate debugging.
33 * Note: Treat as private. This is only public to facilitate debugging.
38 * @var CRM_Utils_Cache_Interface|null
40 * Note: Treat as private. This is only public to facilitate debugging.
46 * The cache key used for any data stored by this container
48 * Note: Treat as private. This is only public to facilitate debugging.
56 * Note: Treat as private. This is only public to facilitate debugging.
58 public $relPaths = FALSE;
64 * Derived from $relPaths. On Unix systems (where file-paths and
65 * URL-paths both use '/' separator), this isn't necessary. On Windows
66 * systems, this is derived from $relPaths.
68 * Note: Treat as private. This is only public to facilitate debugging.
70 public $relUrls = FALSE;
74 * Array(function(CRM_Extension_Info $info): bool)
75 * List of callables which determine whether an extension is visible.
76 * Each function returns TRUE if the extension should be visible.
78 protected $filters = [];
81 * @param string $baseDir
82 * Local path to the container.
83 * @param string $baseUrl
84 * Public URL of the container.
85 * @param CRM_Utils_Cache_Interface $cache
86 * Cache in which to store extension metadata.
87 * @param string $cacheKey
88 * Unique name for this container.
90 public function __construct($baseDir, $baseUrl, CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL) {
91 $this->cache
= $cache;
92 $this->cacheKey
= $cacheKey;
93 $this->baseDir
= rtrim($baseDir, '/');
94 $this->baseUrl
= rtrim($baseUrl, '/');
102 public function checkRequirements() {
105 if (empty($this->baseDir
) ||
!is_dir($this->baseDir
)) {
107 'title' => ts('Invalid Base Directory'),
108 'message' => ts('An extension container has been defined with a blank directory.'),
111 if (empty($this->baseUrl
)) {
113 'title' => ts('Invalid Base URL'),
114 'message' => ts('An extension container has been defined with a blank URL.'),
126 public function getKeys() {
127 return array_keys($this->getRelPaths());
133 public function getPath($key) {
134 return $this->baseDir
. $this->getRelPath($key);
140 public function getResUrl($key) {
141 if (!$this->baseUrl
) {
142 CRM_Core_Session
::setStatus(
143 ts('Failed to determine URL for extension (%1). Please update <a href="%2">Resource URLs</a>.',
146 2 => CRM_Utils_System
::url('civicrm/admin/setting/url', 'reset=1'),
151 return $this->baseUrl
. $this->getRelUrl($key);
157 public function refresh() {
158 $this->relPaths
= NULL;
160 $this->cache
->delete($this->cacheKey
);
167 public function getBaseDir() {
168 return $this->baseDir
;
172 * Determine the relative path of an extension directory.
177 * @throws CRM_Extension_Exception_MissingException
180 protected function getRelPath($key) {
181 $keypaths = $this->getRelPaths();
182 if (!isset($keypaths[$key])) {
183 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
185 return $keypaths[$key];
189 * Scan $basedir for a list of extension-keys
194 protected function getRelPaths() {
195 if (!is_array($this->relPaths
)) {
197 $this->relPaths
= $this->cache
->get($this->cacheKey
);
199 if (!is_array($this->relPaths
)) {
200 $this->relPaths
= [];
201 $infoPaths = CRM_Utils_File
::findFiles($this->baseDir
, 'info.xml');
202 foreach ($infoPaths as $infoPath) {
203 $relPath = CRM_Utils_File
::relativize(dirname($infoPath), $this->baseDir
);
205 $info = CRM_Extension_Info
::loadFromFile($infoPath);
207 catch (CRM_Extension_Exception_ParseException
$e) {
208 CRM_Core_Session
::setStatus(ts('Parse error in extension: %1', [
209 1 => $e->getMessage(),
211 CRM_Core_Error
::debug_log_message("Parse error in extension: " . $e->getMessage());
215 foreach ($this->filters
as $filter) {
216 if (!$filter($info)) {
222 $this->relPaths
[$info->key
] = $relPath;
226 $this->cache
->set($this->cacheKey
, $this->relPaths
);
230 return $this->relPaths
;
234 * Determine the relative path of an extension directory.
239 * @throws CRM_Extension_Exception_MissingException
242 protected function getRelUrl($key) {
243 $relUrls = $this->getRelUrls();
244 if (!isset($relUrls[$key])) {
245 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
247 return $relUrls[$key];
251 * Scan $basedir for a list of extension-keys
256 protected function getRelUrls() {
257 if (DIRECTORY_SEPARATOR
== '/') {
258 return $this->getRelPaths();
260 if (!is_array($this->relUrls
)) {
261 $this->relUrls
= self
::convertPathsToUrls(DIRECTORY_SEPARATOR
, $this->getRelPaths());
263 return $this->relUrls
;
267 * Register a filter which determine whether a copy of an extension
268 * appears as available.
270 * @param callable $callable
271 * function(CRM_Extension_Info $info): bool
272 * Each function returns TRUE if the extension should be visible.
275 public function addFilter($callable) {
276 $this->filters
[] = $callable;
281 * Convert a list of relative paths to relative URLs.
283 * Note: Treat as private. This is only public to facilitate testing.
285 * @param string $dirSep
286 * Directory separator ("/" or "\").
287 * @param array $relPaths
288 * Array($key => $relPath).
290 * Array($key => $relUrl).
292 public static function convertPathsToUrls($dirSep, $relPaths) {
294 foreach ($relPaths as $key => $relPath) {
295 $relUrls[$key] = str_replace($dirSep, '/', $relPath);