Files
backupscript/src/App.php

103 lines
3.0 KiB
PHP
Raw Normal View History

2023-05-31 14:34:35 +00:00
<?php
2023-06-08 12:44:59 +00:00
declare(strict_types=1);
2023-05-31 14:34:35 +00:00
namespace App;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
2023-05-31 14:42:43 +00:00
use Psr\Log\NullLogger;
use League\Config\Configuration;
use Nette\Schema\Expect;
2023-05-31 14:34:35 +00:00
2023-06-05 09:40:04 +00:00
/**
* Mostly working as a register pattern for the logging and configuration.
*/
2023-06-01 09:16:19 +00:00
class App
{
/// Logging instance
2023-05-31 14:34:35 +00:00
protected Logger $logger;
/// Configuration singleton
protected Configuration $config;
2023-05-31 14:34:35 +00:00
2023-06-05 09:40:04 +00:00
/**
* Create a new instance providing a config file.
2023-06-08 12:44:59 +00:00
*
2023-06-05 09:40:04 +00:00
* @param string $configFile Relative or full path to YML config.
* @SuppressWarnings(PHPMD.StaticAccess)
2023-06-05 09:40:04 +00:00
*/
2023-06-08 12:44:59 +00:00
public function __construct(string $configFile)
2023-05-31 14:34:35 +00:00
{
// Define your configuration schema
$this->config = new Configuration([
'rclone' => Expect::structure([
'path' => Expect::string()->default('rclone'),
'options' => Expect::arrayOf('string', 'string')
]),
'backup' => Expect::arrayOf(Expect::structure([
2023-11-14 09:09:15 +00:00
'title' => Expect::string()->required(),
'source' => Expect::string()->required(),
'destination' => Expect::string()->required(),
]))->required(),
'notification' => Expect::arrayOf(Expect::structure([
2023-11-14 09:09:15 +00:00
'type' => Expect::string()->required(),
'domain' => Expect::string()->required(),
'topic' => Expect::string()->required(),
])),
'log' => Expect::string()->assert(
function (string $path): bool {
2023-07-04 09:45:32 +00:00
return touch($path);
}
2023-07-04 10:15:34 +00:00
),
2023-08-29 09:44:05 +00:00
'templates' => Expect::structure(
[
2023-11-14 09:09:15 +00:00
'notify' => Expect::string()->required(),
'error' => Expect::string()->required()
2023-08-29 09:44:05 +00:00
]
2023-11-14 09:09:15 +00:00
)->required()
]);
2023-06-08 12:44:59 +00:00
$parser = new Yaml();
2023-07-05 14:36:56 +00:00
/** @var array<string, mixed> */
$parsedConfig = $parser->parseFile($configFile);
2023-07-04 10:15:34 +00:00
// Merge those values into the configuration schema:
$this->config->merge($parsedConfig);
2023-05-31 14:34:35 +00:00
$logger = new Logger('app');
if ($this->config->get('log')) {
2023-07-05 14:36:56 +00:00
$logger->pushHandler(new StreamHandler((string)$this->config->get('log')));
2023-07-04 10:15:34 +00:00
$logger->info("Logging enabled");
2023-05-31 14:42:43 +00:00
}
2023-05-31 14:34:35 +00:00
$logger->info("Initialization complete");
$this->logger = $logger;
}
2023-06-05 09:40:04 +00:00
/**
* Get configuration from key
2023-06-08 12:44:59 +00:00
*
* @param non-empty-string $key Key to fetch
* @return mixed Configuration value
2023-06-05 09:40:04 +00:00
*/
public function getConfig(string $key): mixed
2023-05-31 14:34:35 +00:00
{
2023-07-28 12:12:47 +00:00
/** @var string|array<string, string> */
2023-07-04 10:15:34 +00:00
$ret = $this->config->get($key);
2023-07-05 14:36:56 +00:00
$this->logger->debug("Fetching configuration key", [$key, $ret]);
2023-07-04 10:15:34 +00:00
return $ret;
2023-05-31 14:34:35 +00:00
}
2023-06-05 09:40:04 +00:00
/**
2023-06-08 12:44:59 +00:00
* Get the logger instance.
*
2023-06-05 09:40:04 +00:00
* @return Logger Instance of logger
*/
2023-06-08 12:44:59 +00:00
public function getLogger(): Logger
2023-05-31 14:34:35 +00:00
{
return $this->logger;
}
2023-06-08 12:44:59 +00:00
}