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.
45 * @var string the cache key used for any data stored by this container
47 * Note: Treat as private. This is only public to facilitate debugging.
52 * @var array($key => $relPath)
54 * Note: Treat as private. This is only public to facilitate debugging.
56 public $relPaths = FALSE;
59 * @var array($key => $relUrl)
61 * Derived from $relPaths. On Unix systems (where file-paths and
62 * URL-paths both use '/' separator), this isn't necessary. On Windows
63 * systems, this is derived from $relPaths.
65 * Note: Treat as private. This is only public to facilitate debugging.
67 public $relUrls = FALSE;
71 * Array(function(CRM_Extension_Info $info): bool)
72 * List of callables which determine whether an extension is visible.
73 * Each function returns TRUE if the extension should be visible.
75 protected $filters = [];
78 * @param string $baseDir
79 * Local path to the container.
80 * @param string $baseUrl
81 * Public URL of the container.
82 * @param CRM_Utils_Cache_Interface $cache
83 * Cache in which to store extension metadata.
84 * @param string $cacheKey
85 * Unique name for this container.
87 public function __construct($baseDir, $baseUrl, CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL) {
88 $this->cache
= $cache;
89 $this->cacheKey
= $cacheKey;
90 $this->baseDir
= rtrim($baseDir, '/');
91 $this->baseUrl
= rtrim($baseUrl, '/');
99 public function checkRequirements() {
102 if (empty($this->baseDir
) ||
!is_dir($this->baseDir
)) {
104 'title' => ts('Invalid Base Directory'),
105 'message' => ts('An extension container has been defined with a blank directory.'),
108 if (empty($this->baseUrl
)) {
110 'title' => ts('Invalid Base URL'),
111 'message' => ts('An extension container has been defined with a blank URL.'),
123 public function getKeys() {
124 return array_keys($this->getRelPaths());
130 public function getPath($key) {
131 return $this->baseDir
. $this->getRelPath($key);
137 public function getResUrl($key) {
138 if (!$this->baseUrl
) {
139 CRM_Core_Session
::setStatus(
140 ts('Failed to determine URL for extension (%1). Please update <a href="%2">Resource URLs</a>.',
143 2 => CRM_Utils_System
::url('civicrm/admin/setting/url', 'reset=1'),
148 return $this->baseUrl
. $this->getRelUrl($key);
154 public function refresh() {
155 $this->relPaths
= NULL;
157 $this->cache
->delete($this->cacheKey
);
164 public function getBaseDir() {
165 return $this->baseDir
;
169 * Determine the relative path of an extension directory.
174 * @throws CRM_Extension_Exception_MissingException
177 protected function getRelPath($key) {
178 $keypaths = $this->getRelPaths();
179 if (!isset($keypaths[$key])) {
180 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
182 return $keypaths[$key];
186 * Scan $basedir for a list of extension-keys
191 protected function getRelPaths() {
192 if (!is_array($this->relPaths
)) {
194 $this->relPaths
= $this->cache
->get($this->cacheKey
);
196 if (!is_array($this->relPaths
)) {
197 $this->relPaths
= [];
198 $infoPaths = CRM_Utils_File
::findFiles($this->baseDir
, 'info.xml');
199 foreach ($infoPaths as $infoPath) {
200 $relPath = CRM_Utils_File
::relativize(dirname($infoPath), $this->baseDir
);
202 $info = CRM_Extension_Info
::loadFromFile($infoPath);
204 catch (CRM_Extension_Exception_ParseException
$e) {
205 CRM_Core_Session
::setStatus(ts('Parse error in extension: %1', [
206 1 => $e->getMessage(),
208 CRM_Core_Error
::debug_log_message("Parse error in extension: " . $e->getMessage());
212 foreach ($this->filters
as $filter) {
213 if (!$filter($info)) {
219 $this->relPaths
[$info->key
] = $relPath;
223 $this->cache
->set($this->cacheKey
, $this->relPaths
);
227 return $this->relPaths
;
231 * Determine the relative path of an extension directory.
236 * @throws CRM_Extension_Exception_MissingException
239 protected function getRelUrl($key) {
240 $relUrls = $this->getRelUrls();
241 if (!isset($relUrls[$key])) {
242 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
244 return $relUrls[$key];
248 * Scan $basedir for a list of extension-keys
253 protected function getRelUrls() {
254 if (DIRECTORY_SEPARATOR
== '/') {
255 return $this->getRelPaths();
257 if (!is_array($this->relUrls
)) {
258 $this->relUrls
= self
::convertPathsToUrls(DIRECTORY_SEPARATOR
, $this->getRelPaths());
260 return $this->relUrls
;
264 * Register a filter which determine whether a copy of an extension
265 * appears as available.
267 * @param callable $callable
268 * function(CRM_Extension_Info $info): bool
269 * Each function returns TRUE if the extension should be visible.
272 public function addFilter($callable) {
273 $this->filters
[] = $callable;
278 * Convert a list of relative paths to relative URLs.
280 * Note: Treat as private. This is only public to facilitate testing.
282 * @param string $dirSep
283 * Directory separator ("/" or "\").
284 * @param array $relPaths
285 * Array($key => $relPath).
287 * Array($key => $relUrl).
289 public static function convertPathsToUrls($dirSep, $relPaths) {
291 foreach ($relPaths as $key => $relPath) {
292 $relUrls[$key] = str_replace($dirSep, '/', $relPath);