* @license https://opensource.org/licenses/gpl-license.php GNU Public License * @link https://jcktrue.dk */ class App { protected Logger $logger; protected Configuration $config; /** * Create a new instance providing a config file * * @param string $configFile Relative or full path to YML config. * @SuppressWarnings(PHPMD.StaticAccess) */ public function __construct(string $configFile) { // 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 { return touch($path); } ), 'templates' => Expect::structure(['notify' => Expect::string()]) ]); $parser = new Yaml(); $parsedConfig = $parser->parseFile($configFile); // Merge those values into the configuration schema: $this->config->merge($parsedConfig); $logger = new Logger('app'); if ($this->config->get('log')) { $logger->pushHandler(new StreamHandler($this->config->get('log'))); $logger->info("Logging enabled"); } $logger->info("Initialization complete"); $this->logger = $logger; } /** * Get configuration from key * * @param non-empty-string $key Key to fetch * @return mixed Configuration value */ public function getConfig(string $key): mixed { $ret = $this->config->get($key); $this->logger->debug("Fetching configuration key", [$key,$ret]); return $ret; } /** * Get the logger instance. * * @return Logger Instance of logger */ public function getLogger(): Logger { return $this->logger; } }