Commit 396d7d64 authored by Ludwig Ruderstaller's avatar Ludwig Ruderstaller
Browse files

Merge branch 'develop' into 'master'

Develop

See merge request !8
parents b128ad6b 4fd2b2af
Pipeline #19947 passed with stage
in 3 minutes and 23 seconds
......@@ -30,6 +30,8 @@ class Configuration implements ConfigurationInterface
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('cwd_power_dns_client');
$rootNode->children()
->variableNode('hosts')->defaultValue([])->end()
->variableNode('uri')->defaultValue('http://localhost')->end()
->variableNode('api_key')->defaultValue(null)->end()
->variableNode('default_server')->defaultValue('localhost')->end()
......
......@@ -27,6 +27,8 @@ class CwdPowerDNSClientExtension extends Extension
$container->setParameter('cwd_power_dns_client.api_key', $config['api_key']);
$container->setParameter('cwd_power_dns_client.default_server', $config['default_server']);
$container->setParameter('cwd_power_dns_client.clients', $config['hosts']);
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
......
......@@ -16,6 +16,65 @@ use Doctrine\Common\Annotations\Reader;
class PowerDNSClientFactory
{
private $config = [];
private $reader;
const SLAVE = 'slave';
const MASTER = 'master';
/**
* @var array<string,PowerDNSClient>
*/
private $clients = [];
public function __construct(array $config = [], Reader $reader)
{
$this->config = $config;
$this->reader = $reader;
}
public function getClient(string $name): PowerDNSClient
{
if (isset($this->clients[$name]) && $this->clients[$name] instanceof PowerDNSClient) {
return $this->clients[$name];
}
foreach ($this->config as $configName => $config) {
if ($name === $configName) {
$client = new Client($config['uri'], $config['api_key'], null, $this->reader);
$this->clients[$name] = new PowerDNSClient($client);
return $this->clients[$name];
}
}
throw new \RuntimeException(sprintf('No configuration for "%s% is found', $name));
}
/**
* @return array<PowerDNSClient>
*/
public function getSlaves(): array
{
$clients = [];
foreach ($this->config as $configName => $config) {
if (isset($config['type']) && $config['type'] == self::SLAVE) {
$clients[] = $this->getClient($configName);
}
}
return $clients;
}
/**
* @param string $uri
* @param string $apiKey
* @param string|null $defaultServer
* @param Reader $reader
* @return PowerDNSClient
*/
static public function createClient(string $uri, string $apiKey, ?string $defaultServer = null, Reader $reader): PowerDNSClient
{
$client = new Client($uri, $apiKey, null, $reader);
......
services:
Cwd\PowerDNSClient\PowerDNSClientFactory: ~
Cwd\PowerDNSClient\PowerDNSClientFactory:
public: true
arguments:
- '%cwd_power_dns_client.clients%'
- '@annotation_reader'
Cwd\PowerDNSClient\PowerDNSClient:
public: true
......
......@@ -21,7 +21,6 @@ declare(strict_types=1);
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
return $loader;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment