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