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
|
|
|
|
{
|
|
|
|
private $folder;
|
2022-07-08 13:54:23 +03:00
|
|
|
/** @var array<class-string<BridgeInterface>> */
|
|
|
|
private $bridgeClassNames = [];
|
|
|
|
/** @var array<class-string<BridgeInterface>> */
|
2022-07-01 16:10:30 +03:00
|
|
|
private $whitelist = [];
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
public function __construct(string $folder = PATH_LIB_BRIDGES)
|
|
|
|
{
|
|
|
|
$this->folder = $folder;
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// create names
|
|
|
|
foreach (scandir($this->folder) 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
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// 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 = '';
|
|
|
|
}
|
|
|
|
if ($contents === '*') { // Whitelist all bridges
|
2022-07-08 13:54:23 +03:00
|
|
|
$this->whitelist = $this->getBridgeClassNames();
|
2022-07-01 16:10:30 +03:00
|
|
|
} else {
|
|
|
|
foreach (explode("\n", $contents) as $bridgeName) {
|
2022-07-08 13:54:23 +03:00
|
|
|
$bridgeClassName = $this->sanitizeBridgeName($bridgeName);
|
|
|
|
if ($bridgeClassName !== null) {
|
|
|
|
$this->whitelist[] = $bridgeClassName;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
/**
|
|
|
|
* @param class-string<BridgeInterface> $name
|
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
public function create(string $name): BridgeInterface
|
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
return new $name();
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
/**
|
|
|
|
* @return array<class-string<BridgeInterface>>
|
|
|
|
*/
|
|
|
|
public function getBridgeClassNames(): array
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
return $this->bridgeClassNames;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
/**
|
|
|
|
* @param class-string<BridgeInterface>|null $name
|
|
|
|
*/
|
|
|
|
public function isWhitelisted(string $name): bool
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
return in_array($name, $this->whitelist);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
|
|
|
if (!is_string($name)) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Trim trailing '.php' if exists
|
|
|
|
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
// Append 'Bridge' suffix if not present.
|
|
|
|
if (!preg_match('/(Bridge)$/i', $name)) {
|
|
|
|
$name = sprintf('%sBridge', $name);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-06-06 21:59:29 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Improve performance for correctly written bridge names
|
2022-07-08 13:54:23 +03:00
|
|
|
if (in_array($name, $this->getBridgeClassNames())) {
|
|
|
|
$index = array_search($name, $this->getBridgeClassNames());
|
|
|
|
return $this->getBridgeClassNames()[$index];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// The name is valid if a corresponding bridge file is found on disk
|
2022-07-08 13:54:23 +03:00
|
|
|
if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()))) {
|
|
|
|
$index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()));
|
|
|
|
return $this->getBridgeClassNames()[$index];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
Debug::log('Invalid bridge name specified: "' . $name . '"!');
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-24 21:48:12 +03:00
|
|
|
}
|