mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-21 11:14:56 +02:00
feat: enable bridges using env var (#3428)
* refactor: bridgefactory, add tests * refactor: move defaultly enabled bridges to config * refactor * refactor * feat: add support for enabling bridges with env var
This commit is contained in:
parent
d9490c6518
commit
0a8fe57003
19 changed files with 179 additions and 182 deletions
|
@ -2,101 +2,69 @@
|
|||
|
||||
final class BridgeFactory
|
||||
{
|
||||
/** @var array<class-string<BridgeInterface>> */
|
||||
private $bridgeClassNames = [];
|
||||
|
||||
/** @var array<class-string<BridgeInterface>> */
|
||||
private $whitelist = [];
|
||||
private $enabledBridges = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// create names
|
||||
// Create all possible bridge class names from fs
|
||||
foreach (scandir(__DIR__ . '/../bridges/') as $file) {
|
||||
if (preg_match('/^([^.]+Bridge)\.php$/U', $file, $m)) {
|
||||
$this->bridgeClassNames[] = $m[1];
|
||||
}
|
||||
}
|
||||
|
||||
// create whitelist
|
||||
if (file_exists(WHITELIST)) {
|
||||
$contents = trim(file_get_contents(WHITELIST));
|
||||
} elseif (file_exists(WHITELIST_DEFAULT)) {
|
||||
$contents = trim(file_get_contents(WHITELIST_DEFAULT));
|
||||
} else {
|
||||
$contents = '';
|
||||
$enabledBridges = Configuration::getConfig('system', 'enabled_bridges');
|
||||
if ($enabledBridges === null) {
|
||||
throw new \Exception('No bridges are enabled... wtf?');
|
||||
}
|
||||
|
||||
if ($contents === '*') {
|
||||
// Whitelist all bridges
|
||||
$this->whitelist = $this->getBridgeClassNames();
|
||||
} else {
|
||||
foreach (explode("\n", $contents) as $bridgeName) {
|
||||
$bridgeClassName = $this->sanitizeBridgeName($bridgeName);
|
||||
if ($bridgeClassName !== null) {
|
||||
$this->whitelist[] = $bridgeClassName;
|
||||
}
|
||||
foreach ($enabledBridges as $enabledBridge) {
|
||||
if ($enabledBridge === '*') {
|
||||
$this->enabledBridges = $this->bridgeClassNames;
|
||||
break;
|
||||
}
|
||||
$this->enabledBridges[] = $this->createBridgeClassName($enabledBridge);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<BridgeInterface> $name
|
||||
*/
|
||||
public function create(string $name): BridgeInterface
|
||||
{
|
||||
return new $name();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<BridgeInterface>>
|
||||
*/
|
||||
public function isEnabled(string $bridgeName): bool
|
||||
{
|
||||
return in_array($bridgeName, $this->enabledBridges);
|
||||
}
|
||||
|
||||
public function createBridgeClassName(string $bridgeName): ?string
|
||||
{
|
||||
$name = self::normalizeBridgeName($bridgeName);
|
||||
$namesLoweredCase = array_map('strtolower', $this->bridgeClassNames);
|
||||
$nameLoweredCase = strtolower($name);
|
||||
|
||||
if (! in_array($nameLoweredCase, $namesLoweredCase)) {
|
||||
throw new \Exception(sprintf('Bridge name invalid: %s', $bridgeName));
|
||||
}
|
||||
|
||||
$index = array_search($nameLoweredCase, $namesLoweredCase);
|
||||
|
||||
return $this->bridgeClassNames[$index];
|
||||
}
|
||||
|
||||
public static function normalizeBridgeName(string $name)
|
||||
{
|
||||
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
||||
$name = $matches[1];
|
||||
}
|
||||
if (!preg_match('/(Bridge)$/i', $name)) {
|
||||
$name = sprintf('%sBridge', $name);
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
|
||||
public function getBridgeClassNames(): array
|
||||
{
|
||||
return $this->bridgeClassNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<BridgeInterface>|null $name
|
||||
*/
|
||||
public function isWhitelisted(string $name): bool
|
||||
{
|
||||
return in_array($name, $this->whitelist);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to turn a potentially human produced bridge name into a class name.
|
||||
*
|
||||
* @param mixed $name
|
||||
* @return class-string<BridgeInterface>|null
|
||||
*/
|
||||
public function sanitizeBridgeName($name): ?string
|
||||
{
|
||||
if (!is_string($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Trim trailing '.php' if exists
|
||||
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
||||
$name = $matches[1];
|
||||
}
|
||||
|
||||
// Append 'Bridge' suffix if not present.
|
||||
if (!preg_match('/(Bridge)$/i', $name)) {
|
||||
$name = sprintf('%sBridge', $name);
|
||||
}
|
||||
|
||||
// Improve performance for correctly written bridge names
|
||||
if (in_array($name, $this->getBridgeClassNames())) {
|
||||
$index = array_search($name, $this->getBridgeClassNames());
|
||||
return $this->getBridgeClassNames()[$index];
|
||||
}
|
||||
|
||||
// The name is valid if a corresponding bridge file is found on disk
|
||||
if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()))) {
|
||||
$index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()));
|
||||
return $this->getBridgeClassNames()[$index];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue