rss-bridge/lib/BridgeFactory.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.0 KiB
PHP
Raw Normal View History

2013-08-11 13:30:41 +02:00
<?php
2022-05-08 03:58:42 +02:00
final class BridgeFactory
{
private $folder;
/** @var array<class-string<BridgeInterface>> */
private $bridgeClassNames = [];
/** @var array<class-string<BridgeInterface>> */
2022-05-08 03:58:42 +02:00
private $whitelist = [];
2022-05-08 03:58:42 +02:00
public function __construct(string $folder = PATH_LIB_BRIDGES)
{
2022-05-08 03:58:42 +02:00
$this->folder = $folder;
2022-05-08 03:58:42 +02:00
// create names
foreach (scandir($this->folder) as $file) {
if (preg_match('/^([^.]+Bridge)\.php$/U', $file, $m)) {
$this->bridgeClassNames[] = $m[1];
}
}
2022-05-08 03:58:42 +02: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 {
2022-05-08 03:58:42 +02:00
$contents = '';
}
if ($contents === '*') {
// Whitelist all bridges
$this->whitelist = $this->getBridgeClassNames();
} else {
2022-05-08 03:58:42 +02:00
foreach (explode("\n", $contents) as $bridgeName) {
$bridgeClassName = $this->sanitizeBridgeName($bridgeName);
if ($bridgeClassName !== null) {
$this->whitelist[] = $bridgeClassName;
}
}
}
}
/**
* @param class-string<BridgeInterface> $name
*/
2022-05-08 03:58:42 +02:00
public function create(string $name): BridgeInterface
{
return new $name();
}
/**
* @return array<class-string<BridgeInterface>>
*/
public function getBridgeClassNames(): array
2022-05-08 03:58:42 +02:00
{
return $this->bridgeClassNames;
}
/**
* @param class-string<BridgeInterface>|null $name
*/
public function isWhitelisted(string $name): bool
2022-05-08 03:58:42 +02:00
{
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
{
2022-05-08 03:58:42 +02:00
if (!is_string($name)) {
return null;
}
2022-05-08 03:58:42 +02:00
// 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);
2022-05-08 03:58:42 +02:00
}
2022-05-08 03:58:42 +02:00
// Improve performance for correctly written bridge names
if (in_array($name, $this->getBridgeClassNames())) {
$index = array_search($name, $this->getBridgeClassNames());
return $this->getBridgeClassNames()[$index];
2022-05-08 03:58:42 +02:00
}
2022-05-08 03:58:42 +02:00
// 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];
2022-05-08 03:58:42 +02:00
}
2022-05-08 03:58:42 +02:00
return null;
}
}