8000 [MonologBridge] Add completion for server:log by StaffNowa · Pull Request #43892 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[MonologBridge] Add completion for server:log #43892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
35 changes: 35 additions & 0 deletions src/Symfony/Bridge/Monolog/Command/ServerLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -29,6 +31,24 @@
class ServerLogCommand extends Command
{
private const BG_COLOR = ['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow'];
private const HOST_OPTIONS = [
'0.0.0.0:9911',
'127.0.0.1:80',
'127.0.0.1:443',
'127.0.0.1:8000',
'http://0.0.0.0:9911',
'http://127.0.0.1:80',
'https://127.0.0.1:443',
'http://127.0.0.1:8000',
];

private const FORMAT_OPTIONS = [
'%datetime% %start_tag%%level_name%%end_tag% <comment>[%channel%]</> %message%%context%%extra%\n',
];

private const DATE_FORMAT_OPTIONS = [
'H:i:s',
];

private $el;
private $handler;
Expand Down Expand Up @@ -123,6 +143,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('host')) {
$suggestions->suggestValues(self::HOST_OPTIONS);
}

if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(self::FORMAT_OPTIONS);
}

if ($input->mustSuggestOptionValuesFor('date-format')) {
$suggestions->suggestValues(self::DATE_FORMAT_OPTIONS);
}
}

private function getLogs($socket): iterable
{
$sockets = [(int) $socket => $socket];
Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Bridge/Monolog/Tests/ServerLogCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Command\ServerLogCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandCompletionTester;

class ServerLogCommandTest extends TestCase
{
/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
if (!class_exists(CommandCompletionTester::class)) {
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
}

$application = new Application();
$application->add(new ServerLogCommand());

$tester = new CommandCompletionTester($application->find('server:log'));
$suggestions = $tester->complete($input, 2);
$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions(): iterable
{
yield 'option --host' => [['--host', ''], [
'0.0.0.0:9911',
'127.0.0.1:80',
'127.0.0.1:443',
'127.0.0.1:8000',
'http://0.0.0.0:9911',
'http://127.0.0.1:80',
'https://127.0.0.1:443',
'http://127.0.0.1:8000',
]];
yield 'option --format' => [['--format', ''], [
'%datetime% %start_tag%%level_name%%end_tag% <comment>[%channel%]</> %message%%context%%extra%\n',
]];
yield 'option --date-format' => [['--date-format', ''], [
'H:i:s',
]];
}
}
0