mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-02-12 04:29:36 +00:00
Added logic to delegate the info retrieval logic to another provider when giving an URL
This commit is contained in:
parent
47c7ee9f07
commit
10acc2e130
5 changed files with 169 additions and 11 deletions
|
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||
namespace App\Services\InfoProviderSystem;
|
||||
|
||||
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
|
||||
use App\Services\InfoProviderSystem\Providers\URLHandlerInfoProviderInterface;
|
||||
|
||||
/**
|
||||
* This class keeps track of all registered info providers and allows to find them by their key
|
||||
|
|
@ -47,6 +48,8 @@ final class ProviderRegistry
|
|||
*/
|
||||
private array $providers_disabled = [];
|
||||
|
||||
private array $providers_by_domain = [];
|
||||
|
||||
/**
|
||||
* @var bool Whether the registry has been initialized
|
||||
*/
|
||||
|
|
@ -78,6 +81,14 @@ final class ProviderRegistry
|
|||
$this->providers_by_name[$key] = $provider;
|
||||
if ($provider->isActive()) {
|
||||
$this->providers_active[$key] = $provider;
|
||||
if ($provider instanceof URLHandlerInfoProviderInterface) {
|
||||
foreach ($provider->getHandledDomains() as $domain) {
|
||||
if (isset($this->providers_by_domain[$domain])) {
|
||||
throw new \LogicException("Domain $domain is already handled by another provider");
|
||||
}
|
||||
$this->providers_by_domain[$domain] = $provider;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->providers_disabled[$key] = $provider;
|
||||
}
|
||||
|
|
@ -139,4 +150,29 @@ final class ProviderRegistry
|
|||
|
||||
return $this->providers_disabled;
|
||||
}
|
||||
}
|
||||
|
||||
public function getProviderHandlingDomain(string $domain): (InfoProviderInterface&URLHandlerInfoProviderInterface)|null
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initStructures();
|
||||
}
|
||||
|
||||
//Check if the domain is directly existing:
|
||||
if (isset($this->providers_by_domain[$domain])) {
|
||||
return $this->providers_by_domain[$domain];
|
||||
}
|
||||
|
||||
//Otherwise check for subdomains:
|
||||
$parts = explode('.', $domain);
|
||||
while (count($parts) > 2) {
|
||||
array_shift($parts);
|
||||
$check_domain = implode('.', $parts);
|
||||
if (isset($this->providers_by_domain[$check_domain])) {
|
||||
return $this->providers_by_domain[$check_domain];
|
||||
}
|
||||
}
|
||||
|
||||
//If we found nothing, return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue