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-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-06-05 09:43:50 +00:00
|
|
|
* @link https://jcktrue.dks
|
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-06-08 12:44:59 +00:00
|
|
|
protected mixed $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-06-08 12:44:59 +00:00
|
|
|
public function __construct(string $configFile)
|
2023-05-31 14:34:35 +00:00
|
|
|
{
|
2023-06-08 12:44:59 +00:00
|
|
|
$parser = new Yaml();
|
|
|
|
$this->config = $parser->parseFile($configFile);
|
2023-05-31 14:34:35 +00:00
|
|
|
$logger = new Logger('app');
|
2023-06-01 09:16:19 +00:00
|
|
|
if (isset($this->config['log'])) {
|
2023-05-31 14:42:43 +00:00
|
|
|
$logger->pushHandler(new StreamHandler($this->getConfig()['log']));
|
|
|
|
}
|
2023-05-31 14:34:35 +00:00
|
|
|
$logger->info("Initialization complete");
|
|
|
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
2023-06-05 09:40:04 +00:00
|
|
|
/**
|
|
|
|
* Get the full configuration
|
2023-06-08 12:44:59 +00:00
|
|
|
*
|
|
|
|
* @return mixed Full configuration structure
|
2023-06-05 09:40:04 +00:00
|
|
|
*/
|
2023-06-08 12:44:59 +00:00
|
|
|
public function getConfig(): mixed
|
2023-05-31 14:34:35 +00:00
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|