bindAPI/src/Controller/BindAPI.php

95 lines
2.9 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2022-01-19 21:07:22 +01:00
namespace App\Controller;
error_reporting(error_level: E_ALL);
use App\Repository\DomainRepository;
2022-09-17 15:50:36 +02:00
use App\Repository\DynDNSRepository;
use DI\Container;
use DI\ContainerBuilder;
2022-09-21 15:59:09 +02:00
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
2022-09-21 15:59:09 +02:00
use Monolog\Level;
use Monolog\Logger;
use function DI\autowire;
2022-01-19 21:07:22 +01:00
/**
*
*/
class BindAPI
{
2022-09-17 15:50:36 +02:00
private Logger $logger;
private Container $container;
2022-09-22 19:30:45 +02:00
/**
2022-09-21 15:59:09 +02:00
* @throws Exception
*/
2024-04-14 20:14:41 +02:00
public function __construct(bool $quiet)
{
2022-09-17 15:50:36 +02:00
// init the logger
$dateFormat = "Y:m:d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
2024-04-14 20:14:41 +02:00
$debug = (new ConfigController(quiet: $quiet))->getConfig(configKey: 'debug');
2022-09-17 15:50:36 +02:00
if ($debug) {
2022-09-21 15:59:09 +02:00
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Debug);
2022-09-17 15:50:36 +02:00
} else {
2022-09-21 15:59:09 +02:00
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Info);
2022-09-17 15:50:36 +02:00
}
$stream->setFormatter(formatter: $formatter);
2022-09-17 15:50:36 +02:00
$this->logger = new Logger(name: 'bindAPI');
$this->logger->pushHandler(handler: $stream);
$this->logger->debug(message: 'bindAPI started');
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
2024-04-14 20:14:41 +02:00
ConfigController::class => autowire()
->constructorParameter(parameter: 'quiet', value: $quiet),
2022-09-17 15:50:36 +02:00
CLIController::class => autowire()
2024-04-16 18:24:38 +02:00
->constructorParameter(parameter: 'logger', value: $this->logger)
->constructorParameter(parameter: 'quiet', value: $quiet),
DomainController::class => autowire()
2024-04-14 20:14:41 +02:00
->constructorParameter(parameter: 'logger', value: $this->logger)
->constructorParameter(parameter: 'quiet', value: $quiet),
DomainRepository::class => autowire()
2022-09-17 15:50:36 +02:00
->constructorParameter(parameter: 'logger', value: $this->logger),
DynDnsRepository::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger),
RequestController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger)
]);
$this->container = $containerBuilder->build();
}
2022-09-17 15:50:36 +02:00
/**
2022-09-21 15:59:09 +02:00
* @throws DependencyException
* @throws NotFoundException
2022-09-17 15:50:36 +02:00
*/
2022-09-17 18:57:23 +02:00
public function runCommand(array $arguments): void
{
2022-09-17 15:50:36 +02:00
$this->logger->debug(message: 'runCommand()');
$cliController = $this->container->get(name: CLIController::class);
2022-09-17 18:57:23 +02:00
$cliController->runCommand(arguments: $arguments);
}
/**
2022-09-21 15:59:09 +02:00
* @throws DependencyException
* @throws NotFoundException
*/
2022-09-17 15:50:36 +02:00
public function handleRequest(string $requestMethod, array $uri): void
{
2022-09-17 15:50:36 +02:00
$this->logger->debug(message: 'handleRequest()');
$requestController = $this->container->get(name: RequestController::class);
$requestController->handleRequest(requestMethod: $requestMethod, uri: $uri);
2022-01-20 19:55:04 +01:00
}
2022-01-19 21:07:22 +01:00
}