2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
2018-11-13 22:26:56 +03:00
|
|
|
|
2022-05-08 04:58:42 +03:00
|
|
|
final class BridgeFactory
|
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
private $bridgeClassNames = [];
|
2023-06-11 04:16:03 +03:00
|
|
|
private $enabledBridges = [];
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2023-06-09 00:04:16 +03:00
|
|
|
public function __construct()
|
2022-07-01 16:10:30 +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) {
|
|
|
|
throw new \Exception('No bridges are enabled... wtf?');
|
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;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2023-08-01 20:35:15 +03:00
|
|
|
$bridgeClassName = $this->createBridgeClassName($enabledBridge);
|
|
|
|
if ($bridgeClassName) {
|
|
|
|
$this->enabledBridges[] = $bridgeClassName;
|
|
|
|
} else {
|
|
|
|
Logger::info(sprintf('Bridge not found: %s', $enabledBridge));
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2022-05-08 04:58:42 +03:00
|
|
|
public function create(string $name): BridgeInterface
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
return new $name();
|
2018-11-11 00:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-06-11 04:16:03 +03:00
|
|
|
public function isEnabled(string $bridgeName): bool
|
2022-05-08 04:58:42 +03:00
|
|
|
{
|
2023-06-11 04:16:03 +03:00
|
|
|
return in_array($bridgeName, $this->enabledBridges);
|
2018-11-11 00:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-06-11 04:16:03 +03:00
|
|
|
public function createBridgeClassName(string $bridgeName): ?string
|
2022-05-08 04:58:42 +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-05-08 04:58:42 +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-05-08 04:58:42 +03:00
|
|
|
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2022-07-08 13:54:23 +03:00
|
|
|
if (!preg_match('/(Bridge)$/i', $name)) {
|
|
|
|
$name = sprintf('%sBridge', $name);
|
2022-05-08 04:58:42 +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;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2016-08-24 21:48:12 +03:00
|
|
|
}
|