mercureact/src/Configuration.php

102 lines
2.5 KiB
PHP
Raw Normal View History

2024-03-10 02:06:19 +00:00
<?php
namespace NoccyLabs\Mercureact;
2024-03-11 01:15:04 +00:00
use Symfony\Component\Yaml\Yaml;
2024-03-10 02:06:19 +00:00
class Configuration
{
private array $config = [];
2024-03-10 02:06:19 +00:00
2024-03-10 02:06:19 +00:00
public static function createDefault(): Configuration
{
return new Configuration();
}
2024-03-11 01:15:04 +00:00
public static function fromFile(string $file): Configuration
{
$config = new Configuration();
2024-03-11 22:30:25 +00:00
$file = realpath($file);
if (!file_exists($file)) {
throw new \Exception("Configuration file not found");
}
$data = file_get_contents($file);
$yaml = Yaml::parse($data);
2024-03-11 01:15:04 +00:00
$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);
2024-03-11 01:15:04 +00:00
return $config;
}
2024-03-10 02:06:19 +00:00
public function setPublicUrl(string $publicUrl): self
{
$this->config['server.public_url'] = $publicUrl;
2024-03-10 02:06:19 +00:00
return $this;
}
public function getPublicUrl(): ?string
{
return $this->config['server.public_url']??null;
2024-03-10 02:06:19 +00:00
}
public function setJwtSecret(string $secret): self
{
$this->config['security.jwt_secret'] = $secret;
2024-03-10 02:06:19 +00:00
return $this;
}
public function getJwtSecret(): ?string
{
return $this->config['security.jwt_secret']??null;
2024-03-10 02:06:19 +00:00
}
2024-03-10 23:50:15 +00:00
function getAllowAnonymousSubscribe():bool
{
return $this->config['subscribe.allow_anonymous']??false;
2024-03-10 23:50:15 +00:00
}
function setAllowAnonymousSubscribe(bool $allowAnonymousSubscribe): self
{
$this->config['subscribe.allow_anonymous'] = $allowAnonymousSubscribe;
2024-03-10 23:50:15 +00:00
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;
}
2024-03-10 02:06:19 +00:00
}