2022-01-26 19:01:52 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-31 21:00:24 +01:00
|
|
|
|
2022-01-19 21:07:22 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
use App\Repository\DomainRepository;
|
2022-09-17 15:50:36 +02:00
|
|
|
use App\Repository\DynDNSRepository;
|
2022-01-31 21:00:24 +01:00
|
|
|
use DI\Container;
|
|
|
|
use DI\ContainerBuilder;
|
2022-09-21 15:59:09 +02:00
|
|
|
use DI\DependencyException;
|
|
|
|
use DI\NotFoundException;
|
|
|
|
use Exception;
|
2022-02-06 17:59:43 +01:00
|
|
|
use Monolog\Formatter\LineFormatter;
|
|
|
|
use Monolog\Handler\StreamHandler;
|
2022-09-21 15:59:09 +02:00
|
|
|
use Monolog\Level;
|
2022-02-06 17:59:43 +01:00
|
|
|
use Monolog\Logger;
|
2022-01-31 21:00:24 +01:00
|
|
|
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;
|
2022-01-31 21:00:24 +01:00
|
|
|
private Container $container;
|
2022-01-20 19:55:04 +01:00
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
|
|
|
|
/**
|
2022-09-21 15:59:09 +02:00
|
|
|
* @throws Exception
|
2022-01-25 20:31:31 +01:00
|
|
|
*/
|
2022-09-21 15:59:09 +02:00
|
|
|
public function __construct($verbose = false, $quiet = false)
|
2022-01-25 20:31:31 +01:00
|
|
|
{
|
2022-09-17 15:50:36 +02:00
|
|
|
// init the logger
|
2022-02-06 17:59:43 +01:00
|
|
|
$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
|
|
|
}
|
2022-02-06 17:59:43 +01: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');
|
|
|
|
|
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
$containerBuilder = new ContainerBuilder();
|
|
|
|
$containerBuilder->addDefinitions([
|
2022-09-17 15:50:36 +02:00
|
|
|
ConfigController::class => autowire()
|
2022-09-21 15:59:09 +02:00
|
|
|
->constructorParameter(parameter: 'quiet', value: $quiet)
|
2022-09-17 15:50:36 +02:00
|
|
|
->constructorParameter(parameter: 'verbose', value: $verbose),
|
|
|
|
CLIController::class => autowire()
|
|
|
|
->constructorParameter(parameter: 'logger', value: $this->logger),
|
2022-02-06 17:59:43 +01:00
|
|
|
DomainController::class => autowire()
|
2022-09-17 15:50:36 +02:00
|
|
|
->constructorParameter(parameter: 'logger', value: $this->logger),
|
2022-02-06 17:59:43 +01:00
|
|
|
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)
|
2022-01-31 21:00:24 +01:00
|
|
|
]);
|
|
|
|
$this->container = $containerBuilder->build();
|
|
|
|
}
|
|
|
|
|
2022-02-06 17:59:43 +01:00
|
|
|
|
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-01-25 20:31:31 +01:00
|
|
|
{
|
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-01-31 21:00:24 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 16:26:08 +02:00
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
/**
|
2022-09-21 15:59:09 +02:00
|
|
|
* @throws DependencyException
|
|
|
|
* @throws NotFoundException
|
2022-01-31 21:00:24 +01:00
|
|
|
*/
|
2022-09-17 15:50:36 +02:00
|
|
|
public function handleRequest(string $requestMethod, array $uri): void
|
2022-01-31 21:00:24 +01:00
|
|
|
{
|
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
|
|
|
}
|