Skip to content

Commit

Permalink
Support arrays in config loader; add env TRAP_TCP_PORTS and `TRAP_T…
Browse files Browse the repository at this point in the history
…CP_HOST`
  • Loading branch information
roxblnfk committed May 1, 2024
1 parent 1fc81b1 commit 2060195
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
49 changes: 27 additions & 22 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Buggregator\Trap\Application;
use Buggregator\Trap\Bootstrap;
use Buggregator\Trap\Config\Server\SocketServer;
use Buggregator\Trap\Config\Server\TcpPorts;
use Buggregator\Trap\Info;
use Buggregator\Trap\Logger;
use Buggregator\Trap\Sender;
use Buggregator\Trap\Service\Container;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
Expand Down Expand Up @@ -38,18 +40,40 @@ public function configure(): void
'p',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Port to listen',
[9912],
);
$this->addOption(
'sender',
's',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Senders',
['console']
);
$this->addOption('ui', null, InputOption::VALUE_OPTIONAL, 'Enable WEB UI (experimental)', false);
}

/**
* Prepare port listeners
* @return SocketServer[]
*/
public function getServers(Container $container): array
{
$config = $container->get(TcpPorts::class);

$servers = [];
$ports = $config->ports ?: [9912];
/** @var scalar $port */
foreach ($ports as $port) {
\is_numeric($port) or throw new \InvalidArgumentException(
\sprintf('Invalid port `%s`. It must be a number.', (string)$port),
);
$port = (int)$port;
$port > 0 && $port < 65536 or throw new \InvalidArgumentException(
\sprintf('Invalid port `%s`. It must be in range 1-65535.', $port),
);
$servers[] = new SocketServer($port, $config->host, $config->type);
}
return $servers;
}

protected function execute(
InputInterface $input,
OutputInterface $output,
Expand All @@ -59,25 +83,6 @@ protected function execute(
$output->writeln(\sprintf('<fg=yellow;options=bold>%s</> <info>v%s</>', Info::NAME, Info::VERSION));
$output->write(Info::LOGO_CLI_COLOR . "\n", true, OutputInterface::OUTPUT_RAW);

/**
* Prepare port listeners
* @var SocketServer[] $servers
*/
$servers = [];
$ports = $input->getOption('port') ?: [9912];
\assert(\is_array($ports));
foreach ($ports as $port) {
\assert(\is_scalar($port));
\is_numeric($port) or throw new \InvalidArgumentException(
\sprintf('Invalid port `%s`. It must be a number.', (string)$port),
);
$port = (int)$port;
$port > 0 && $port < 65536 or throw new \InvalidArgumentException(
\sprintf('Invalid port `%s`. It must be in range 1-65535.', $port),
);
$servers[] = new SocketServer($port);
}

/** @var non-empty-string[] $senders */
$senders = (array)$input->getOption('sender');

Expand All @@ -93,7 +98,7 @@ protected function execute(
$container->set($input, InputInterface::class);
$container->set(new Logger($output));
$this->app = $container->get(Application::class, [
'map' => $servers,
'map' => $this->getServers($container),
'senders' => $registry->getSenders($senders),
'withFrontend' => $input->getOption('ui') !== false,
]);
Expand Down
36 changes: 36 additions & 0 deletions src/Config/Server/TcpPorts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Config\Server;

use Buggregator\Trap\Service\Config\Env;
use Buggregator\Trap\Service\Config\InputOption;

/**
* Config is a projection of plain TCP ports configuration via ENV and CLI
*
* @internal
* @psalm-internal Buggregator\Trap
*/
final class TcpPorts
{
/**
* List of TCP ports to listen
*
* @var list<int<1, 65535>>
*/
#[Env('TRAP_TCP_PORTS')]
#[InputOption('port')]
public array $ports = [9912, 9913];

/**
* Host to listen
*
* @var non-empty-string
*/
#[Env('TRAP_TCP_HOST')]
public string $host = '127.0.0.1';

public string $type = 'tcp';
}
7 changes: 6 additions & 1 deletion src/Service/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function injectValue(object $config, \ReflectionProperty $property, arra
default => null,
};

if ($value === null) {
if (\in_array($value, [null, []], true)) {
continue;
}

Expand All @@ -81,6 +81,11 @@ private function injectValue(object $config, \ReflectionProperty $property, arra
'int' => (int) $value,
'float' => (float) $value,
'bool' => \filter_var($value, FILTER_VALIDATE_BOOLEAN),
'array' => match (true) {
\is_array($value) => $value,
\is_string($value) => explode(',', $value),
default => [$value],
},
default => $value,
},
default => $value,
Expand Down

0 comments on commit 2060195

Please sign in to comment.