mercureact/src/Configuration.php

102 lines
2.5 KiB
PHP

<?php
namespace NoccyLabs\Mercureact;
use Symfony\Component\Yaml\Yaml;
class Configuration
{
private array $config = [];
public static function createDefault(): Configuration
{
return new Configuration();
}
public static function fromFile(string $file): Configuration
{
$config = new Configuration();
$file = realpath($file);
if (!file_exists($file)) {
throw new \Exception("Configuration file not found");
}
$data = file_get_contents($file);
$yaml = Yaml::parse($data);
$unwrap = null; // mute IDE complains about $unwrap not defined
$unwrap = function (array $array, Configuration $target, array $path=[]) use (&$unwrap) {
foreach ($array as $key=>$value) {
if (is_array($value)) {
$unwrap($value, $target, [ ...$path, $key ]);
} else {
$key = join(".", [ ...$path, $key ]);
$target->config[$key] = $value;
}
}
};
$unwrap($yaml, $config);
return $config;
}
public function setPublicUrl(string $publicUrl): self
{
$this->config['server.public_url'] = $publicUrl;
return $this;
}
public function getPublicUrl(): ?string
{
return $this->config['server.public_url']??null;
}
public function setJwtSecret(string $secret): self
{
$this->config['security.jwt_secret'] = $secret;
return $this;
}
public function getJwtSecret(): ?string
{
return $this->config['security.jwt_secret']??null;
}
function getAllowAnonymousSubscribe():bool
{
return $this->config['subscribe.allow_anonymous']??false;
}
function setAllowAnonymousSubscribe(bool $allowAnonymousSubscribe): self
{
$this->config['subscribe.allow_anonymous'] = $allowAnonymousSubscribe;
return $this;
}
public function setListenAddress(string $address): self
{
$this->config['server.address'] = $address;
return $this;
}
public function getListenAddress(): ?string
{
return $this->config['server.address']??null;
}
public function setAllowOriginHeader(string $value): self
{
$this->config['headers.allow_origin'] = $value;
return $this;
}
public function setContentSecurityPolicyHeader(string $value): self
{
$this->config['headers.csp'] = $value;
return $this;
}
}