2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2018-11-13 20:26:56 +01:00
|
|
|
|
2022-07-01 15:10:30 +02:00
|
|
|
final class BridgeFactory
|
|
|
|
{
|
2023-09-21 22:05:55 +02:00
|
|
|
private CacheInterface $cache;
|
|
|
|
private Logger $logger;
|
2023-10-01 19:23:30 +02:00
|
|
|
private array $bridgeClassNames = [];
|
|
|
|
private array $enabledBridges = [];
|
|
|
|
private array $missingEnabledBridges = [];
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2024-08-29 22:48:59 +02:00
|
|
|
public function __construct(
|
|
|
|
CacheInterface $cache,
|
|
|
|
Logger $logger
|
|
|
|
) {
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->logger = $logger;
|
2023-09-21 22:05:55 +02:00
|
|
|
|
2023-06-11 03:16:03 +02:00
|
|
|
// Create all possible bridge class names from fs
|
2023-06-08 23:04:16 +02:00
|
|
|
foreach (scandir(__DIR__ . '/../bridges/') as $file) {
|
2022-07-08 12:54:23 +02:00
|
|
|
if (preg_match('/^([^.]+Bridge)\.php$/U', $file, $m)) {
|
|
|
|
$this->bridgeClassNames[] = $m[1];
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2023-06-11 03:16:03 +02:00
|
|
|
$enabledBridges = Configuration::getConfig('system', 'enabled_bridges');
|
|
|
|
if ($enabledBridges === null) {
|
2023-10-01 19:23:30 +02:00
|
|
|
throw new \Exception('No bridges are enabled...');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2023-06-11 03:16:03 +02:00
|
|
|
foreach ($enabledBridges as $enabledBridge) {
|
|
|
|
if ($enabledBridge === '*') {
|
|
|
|
$this->enabledBridges = $this->bridgeClassNames;
|
|
|
|
break;
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2023-08-01 19:35:15 +02:00
|
|
|
$bridgeClassName = $this->createBridgeClassName($enabledBridge);
|
|
|
|
if ($bridgeClassName) {
|
|
|
|
$this->enabledBridges[] = $bridgeClassName;
|
|
|
|
} else {
|
2023-08-03 03:10:24 +02:00
|
|
|
$this->missingEnabledBridges[] = $enabledBridge;
|
2023-09-21 22:05:55 +02:00
|
|
|
$this->logger->info(sprintf('Bridge not found: %s', $enabledBridge));
|
2023-08-01 19:35:15 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2023-09-20 02:45:48 +02:00
|
|
|
public function create(string $name): BridgeAbstract
|
2022-07-01 15:10:30 +02:00
|
|
|
{
|
2023-09-21 22:05:55 +02:00
|
|
|
return new $name($this->cache, $this->logger);
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2018-11-10 22:26:56 +01:00
|
|
|
|
2023-06-11 03:16:03 +02:00
|
|
|
public function isEnabled(string $bridgeName): bool
|
2022-07-01 15:10:30 +02:00
|
|
|
{
|
2023-06-11 03:16:03 +02:00
|
|
|
return in_array($bridgeName, $this->enabledBridges);
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2018-11-10 22:26:56 +01:00
|
|
|
|
2023-06-11 03:16:03 +02:00
|
|
|
public function createBridgeClassName(string $bridgeName): ?string
|
2022-07-01 15:10:30 +02:00
|
|
|
{
|
2023-06-11 03:16:03 +02:00
|
|
|
$name = self::normalizeBridgeName($bridgeName);
|
|
|
|
$namesLoweredCase = array_map('strtolower', $this->bridgeClassNames);
|
|
|
|
$nameLoweredCase = strtolower($name);
|
|
|
|
if (! in_array($nameLoweredCase, $namesLoweredCase)) {
|
2023-08-01 19:35:15 +02:00
|
|
|
return null;
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2023-06-11 03:16:03 +02:00
|
|
|
$index = array_search($nameLoweredCase, $namesLoweredCase);
|
|
|
|
return $this->bridgeClassNames[$index];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function normalizeBridgeName(string $name)
|
|
|
|
{
|
2022-07-01 15:10:30 +02:00
|
|
|
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
2022-07-08 12:54:23 +02:00
|
|
|
if (!preg_match('/(Bridge)$/i', $name)) {
|
|
|
|
$name = sprintf('%sBridge', $name);
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2023-06-11 03:16:03 +02:00
|
|
|
return $name;
|
|
|
|
}
|
2019-06-06 20:59:29 +02:00
|
|
|
|
2023-06-11 03:16:03 +02:00
|
|
|
public function getBridgeClassNames(): array
|
|
|
|
{
|
|
|
|
return $this->bridgeClassNames;
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2023-08-03 03:10:24 +02:00
|
|
|
|
|
|
|
public function getMissingEnabledBridges(): array
|
|
|
|
{
|
|
|
|
return $this->missingEnabledBridges;
|
|
|
|
}
|
2016-08-24 20:48:12 +02:00
|
|
|
}
|