Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM_Extension_System - Only allow test.* extensions during headless testing. Hide from regular users. #14392

Merged
merged 2 commits into from
Jun 1, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
CRM_Extension_Container_Basic - Add mechanism for filtering
This function allows one to filter down the list of extensions which are visible.

A few notes:

* The result of the scanning/filtering is stored in cache.  On a typical
  page-load, the system uses this cache -- which happily means that
  filtering shouldn't result in any extra page-load time.

* The extension-system is generally boot-critical.  I could imagine some
  scope-creep around making making the filter mechanism hookable, but that's
  a separate+complicated issue.  Optimistically...  if `array $containers`
  somehow becomes hookable, then one could use the same mechanism to
  manipulate the filters.
  • Loading branch information
totten committed Jun 1, 2019
commit 7bded065dca98125930174c60ec2a7b10b0307c1
33 changes: 32 additions & 1 deletion CRM/Extension/Container/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ class CRM_Extension_Container_Basic implements CRM_Extension_Container_Interface
*/
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.
Expand Down Expand Up @@ -216,7 +224,16 @@ protected function getRelPaths() {
CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage());
continue;
}
$this->relPaths[$info->key] = $relPath;
$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);
Expand Down Expand Up @@ -259,6 +276,20 @@ protected function getRelUrls() {
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.
*
Expand Down