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