bindAPI/src/Controller/BindAPI.php

93 lines
2.8 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
*/
public function __construct($quiet = false)
{
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);
2022-09-17 15:50:36 +02:00
$debug = (new ConfigController)->getConfig(configKey: 'debug');
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([
2022-09-17 15:50:36 +02:00
ConfigController::class => autowire()
->constructorParameter(parameter: 'quiet', value: $quiet),
2022-09-17 15:50:36 +02:00
CLIController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger),
DomainController::class => autowire()
2022-09-17 15:50:36 +02:00
->constructorParameter(parameter: 'logger', value: $this->logger),
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
}