2023-05-31 14:34:35 +00:00
|
|
|
<?php
|
2023-06-08 12:44:59 +00:00
|
|
|
|
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;
|
2023-07-04 09:43:27 +00:00
|
|
|
use League\Config\Configuration;
|
|
|
|
use Nette\Schema\Expect;
|
2023-05-31 14:34:35 +00:00
|
|
|
|
2023-06-05 09:40:04 +00:00
|
|
|
/**
|
|
|
|
* Application class
|
2023-06-08 12:44:59 +00:00
|
|
|
*
|
2023-06-05 09:40:04 +00:00
|
|
|
* Mostly working as a register pattern for the logging and configuration.
|
2023-06-08 12:44:59 +00:00
|
|
|
*
|
2023-06-05 09:40:04 +00:00
|
|
|
* @author Jens True <jens.chr.true@gmail.com>
|
|
|
|
* @license https://opensource.org/licenses/gpl-license.php GNU Public License
|
2023-07-04 09:43:27 +00:00
|
|
|
* @link https://jcktrue.dk
|
2023-06-05 09:40:04 +00:00
|
|
|
*/
|
2023-06-01 09:16:19 +00:00
|
|
|
class App
|
|
|
|
{
|
2023-05-31 14:34:35 +00:00
|
|
|
protected Logger $logger;
|
2023-07-04 09:43:27 +00:00
|
|
|
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.
|
2023-07-04 09:43:27 +00:00
|
|
|
* @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
|
|
|
{
|
2023-07-04 09:43:27 +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([
|
|
|
|
'title' => Expect::string(),
|
|
|
|
'source' => Expect::string(),
|
|
|
|
'destination' => Expect::string(),
|
|
|
|
])),
|
|
|
|
'notification' => Expect::arrayOf(Expect::structure([
|
|
|
|
'type' => Expect::string(),
|
|
|
|
'domain' => Expect::string(),
|
|
|
|
'topic' => Expect::string(),
|
|
|
|
])),
|
|
|
|
'log' => Expect::string()->assert(
|
|
|
|
function (string $path): bool {
|
2023-07-04 09:45:32 +00:00
|
|
|
return touch($path);
|
2023-07-04 09:43:27 +00:00
|
|
|
}
|
|
|
|
)->required(),
|
|
|
|
'templates' => Expect::structure(['notify' => Expect::string()])
|
|
|
|
]);
|
|
|
|
|
2023-06-08 12:44:59 +00:00
|
|
|
$parser = new Yaml();
|
2023-07-04 09:43:27 +00:00
|
|
|
$parsedConfig = $parser->parseFile($configFile);
|
|
|
|
|
|
|
|
|
|
|
|
// Merge those values into your configuration schema:
|
|
|
|
$this->config->merge($parsedConfig);
|
|
|
|
|
2023-05-31 14:34:35 +00:00
|
|
|
$logger = new Logger('app');
|
2023-07-04 09:43:27 +00:00
|
|
|
if ($this->config->get('log')) {
|
|
|
|
$logger->pushHandler(new StreamHandler($this->config->get('log')));
|
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
|
|
|
/**
|
2023-07-04 09:43:27 +00:00
|
|
|
* Get configuration from key
|
2023-06-08 12:44:59 +00:00
|
|
|
*
|
2023-07-04 09:43:27 +00:00
|
|
|
* @param non-empty-string $key Key to fetch
|
|
|
|
* @return mixed Configuration value
|
2023-06-05 09:40:04 +00:00
|
|
|
*/
|
2023-07-04 09:43:27 +00:00
|
|
|
public function getConfig(string $key): mixed
|
2023-05-31 14:34:35 +00:00
|
|
|
{
|
2023-07-04 09:43:27 +00:00
|
|
|
return $this->config->get($key);
|
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
|
|
|
}
|