-
-
Notifications
You must be signed in to change notification settings - Fork 817
/
Basic.php
300 lines (275 loc) · 7.7 KB
/
Basic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
/**
* An extension container is a locally-accessible source tree which can be
* scanned for extensions.
*/
class CRM_Extension_Container_Basic implements CRM_Extension_Container_Interface {
/**
* @var string
*
* Note: Treat as private. This is only public to facilitate debugging.
*/
public $baseDir;
/**
* @var string
*
* Note: Treat as private. This is only public to facilitate debugging.
*/
public $baseUrl;
/**
* @var CRM_Utils_Cache_Interface|null
*
* Note: Treat as private. This is only public to facilitate debugging.
*/
public $cache;
/**
* @var string
* The cache key used for any data stored by this container
*
* Note: Treat as private. This is only public to facilitate debugging.
*/
public $cacheKey;
/**
* @var array
* ($key => $relPath)
*
* Note: Treat as private. This is only public to facilitate debugging.
*/
public $relPaths = FALSE;
/**
* @var array
* ($key => $relUrl)
*
* Derived from $relPaths. On Unix systems (where file-paths and
* URL-paths both use '/' separator), this isn't necessary. On Windows
* systems, this is derived from $relPaths.
*
* Note: Treat as private. This is only public to facilitate debugging.
*/
public $relUrls = FALSE;
/**
* @var array
* Array(function(CRM_Extension_Info $info): bool)
* List of callables which determine whether an extension is visible.
* Each function returns TRUE if the extension should be visible.
*/
protected $filters = [];
/**
* @param string $baseDir
* Local path to the container.
* @param string $baseUrl
* Public URL of the container.
* @param CRM_Utils_Cache_Interface $cache
* Cache in which to store extension metadata.
* @param string $cacheKey
* Unique name for this container.
*/
public function __construct($baseDir, $baseUrl, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
$this->cache = $cache;
$this->cacheKey = $cacheKey;
$this->baseDir = rtrim($baseDir, '/');
$this->baseUrl = rtrim($baseUrl, '/');
}
/**
* @inheritDoc
*
* @return array
*/
public function checkRequirements() {
$errors = [];
if (empty($this->baseDir) || !is_dir($this->baseDir)) {
$errors[] = [
'title' => ts('Invalid Base Directory'),
'message' => ts('An extension container has been defined with a blank directory.'),
];
}
if (empty($this->baseUrl)) {
$errors[] = [
'title' => ts('Invalid Base URL'),
'message' => ts('An extension container has been defined with a blank URL.'),
];
}
return $errors;
}
/**
* @inheritDoc
*
* @return array_keys
*/
public function getKeys() {
return array_keys($this->getRelPaths());
}
/**
* @inheritDoc
*/
public function getPath($key) {
return $this->baseDir . $this->getRelPath($key);
}
/**
* @inheritDoc
*/
public function getResUrl($key) {
if (!$this->baseUrl) {
CRM_Core_Session::setStatus(
ts('Failed to determine URL for extension (%1). Please update <a href="%2">Resource URLs</a>.',
[
1 => $key,
2 => CRM_Utils_System::url('civicrm/admin/setting/url', 'reset=1'),
]
)
);
}
return $this->baseUrl . $this->getRelUrl($key);
}
/**
* @inheritDoc
*/
public function refresh() {
$this->relPaths = NULL;
if ($this->cache) {
$this->cache->delete($this->cacheKey);
}
}
/**
* @return string
*/
public function getBaseDir() {
return $this->baseDir;
}
/**
* Determine the relative path of an extension directory.
*
* @param string $key
* Extension name.
*
* @throws CRM_Extension_Exception_MissingException
* @return string
*/
protected function getRelPath($key) {
$keypaths = $this->getRelPaths();
if (!isset($keypaths[$key])) {
throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
}
return $keypaths[$key];
}
/**
* Scan $basedir for a list of extension-keys
*
* @return array
* ($key => $relPath)
*/
protected function getRelPaths() {
if (!is_array($this->relPaths)) {
if ($this->cache) {
$this->relPaths = $this->cache->get($this->cacheKey);
}
if (!is_array($this->relPaths)) {
$this->relPaths = [];
$infoPaths = CRM_Utils_File::findFiles($this->baseDir, 'info.xml');
foreach ($infoPaths as $infoPath) {
$relPath = CRM_Utils_File::relativize(dirname($infoPath), $this->baseDir);
try {
$info = CRM_Extension_Info::loadFromFile($infoPath);
}
catch (CRM_Extension_Exception_ParseException $e) {
CRM_Core_Session::setStatus(ts('Parse error in extension: %1', [
1 => $e->getMessage(),
]), '', 'error');
CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage());
continue;
}
$visible = TRUE;
foreach ($this->filters as $filter) {
if (!$filter($info)) {
$visible = FALSE;
break;
}
}
if ($visible) {
$this->relPaths[$info->key] = $relPath;
}
}
if ($this->cache) {
$this->cache->set($this->cacheKey, $this->relPaths);
}
}
}
return $this->relPaths;
}
/**
* Determine the relative path of an extension directory.
*
* @param string $key
* Extension name.
*
* @throws CRM_Extension_Exception_MissingException
* @return string
*/
protected function getRelUrl($key) {
$relUrls = $this->getRelUrls();
if (!isset($relUrls[$key])) {
throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
}
return $relUrls[$key];
}
/**
* Scan $basedir for a list of extension-keys
*
* @return array
* ($key => $relUrl)
*/
protected function getRelUrls() {
if (DIRECTORY_SEPARATOR == '/') {
return $this->getRelPaths();
}
if (!is_array($this->relUrls)) {
$this->relUrls = self::convertPathsToUrls(DIRECTORY_SEPARATOR, $this->getRelPaths());
}
return $this->relUrls;
}
/**
* Register a filter which determine whether a copy of an extension
* appears as available.
*
* @param callable $callable
* function(CRM_Extension_Info $info): bool
* Each function returns TRUE if the extension should be visible.
* @return $this
*/
public function addFilter($callable) {
$this->filters[] = $callable;
return $this;
}
/**
* Convert a list of relative paths to relative URLs.
*
* Note: Treat as private. This is only public to facilitate testing.
*
* @param string $dirSep
* Directory separator ("/" or "\").
* @param array $relPaths
* Array($key => $relPath).
* @return array
* Array($key => $relUrl).
*/
public static function convertPathsToUrls($dirSep, $relPaths) {
$relUrls = [];
foreach ($relPaths as $key => $relPath) {
$relUrls[$key] = str_replace($dirSep, '/', $relPath);
}
return $relUrls;
}
}