8000 [Console] Add a stream helper by louismariegaborit · Pull Request #53518 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add a stream helper #53518

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
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Diff view
Prev Previous commit
[Monolog] Use stream helper and test server:log command
  • Loading branch information
louismariegaborit committed Jan 15, 2024
commit 2cb735c0c185b62d35bcf6e91c5e6d62033f0cf2
62 changes: 20 additions & 42 deletions src/Symfony/Bridge/Monolog/Command/ServerLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -34,7 +33,6 @@ class ServerLogCommand extends Command
{
private const BG_COLOR = ['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow'];

private ExpressionLanguage $el;
private HandlerInterface $handler;

public function isEnabled(): bool
Expand Down Expand Up @@ -78,12 +76,13 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$el = null;
$filter = $input->getOption('filter');
if ($filter) {
if (!class_exists(ExpressionLanguage::class)) {
throw new LogicException('Package "symfony/expression-language" is required to use the "filter" option. Try running "composer require symfony/expression-language".');
}
$this->el = new ExpressionLanguage();
$el = new ExpressionLanguage();
}

$this->handler = new ConsoleHandler($output, true, [
Expand All @@ -101,49 +100,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$host = 'tcp://'.$host;
}

if (!$socket = stream_socket_server($host, $errno, $errstr)) {
throw new RuntimeException(sprintf('Server start failed on "%s": ', $host).$errstr.' '.$errno);
}

foreach ($this->getLogs($socket) as $clientId => $message) {
$record = unserialize(base64_decode($message));
$streamHelper = $this->getHelper('stream');
$streamHelper->listen(
$input,
$output,
$host,
function (int $clientId, string $message) use ($el, $filter, $output) {
$record = unserialize(base64_decode($message));

// Impossible to decode the message, give up.
if (false === $record) {
return;
}

// Impossible to decode the message, give up.
if (false === $record) {
continue;
}
if ($filter && !$el->evaluate($filter, $record)) {
return;
}

if ($filter && !$this->el->evaluate($filter, $record)) {
continue;
$this->displayLog($output, $clientId, $record);
}
);

$this->displayLog($output, $clientId, $record);
}

return 0;
}

private function getLogs($socket): iterable
{
$sockets = [(int) $socket => $socket];
$write = [];

while (true) {
$read = $sockets;
stream_select($read, $write, $write, null);

foreach ($read as $stream) {
if ($socket === $stream) {
$stream = stream_socket_accept($socket);
$sockets[(int) $stream] = $stream;
} elseif (feof($stream)) {
unset($sockets[(int) $stream]);
fclose($stream);
} else {
yield (int) $stream => fgets($stream);
}
}
}
return Command::SUCCESS;
}

private function displayLog(OutputInterface $output, int $clientId, array $record): void
Expand Down
55 changes: 55 additions & 0 deletions src/Symfony/Bridge/Monolog/Tests/Command/ServerLogCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\Command;

use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Command\ServerLogCommand;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\StreamHelper;
use Symfony\Component\Console\Tester\CommandTester;

class ServerLogCommandTest extends TestCase
{
public function testServerLogSuccess()
{
$command = $this->createCommand();
$commandTester = new CommandTester($command);

$record = new LogRecord(
new \DateTimeImmutable('2024-01-02 18:05'),
'console',
Level::Info,
'test log command',
);
$recordFormatted = $record->toArray();
$input = base64_encode(serialize($recordFormatted))."\n";

$commandTester->setInputs([$input]);

$commandTester->execute([]);

$commandTester->assertCommandIsSuccessful();

$output = $commandTester->getDisplay();
$this->assertStringContainsString('18:05:00 INFO [console] test log command', $output);
}

private function createCommand(): ServerLogCommand
{
$command = new ServerLogCommand();
$command->setHelperSet(new HelperSet([new StreamHelper()]));

return $command;
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Monolog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"symfony/http-kernel": "^6.4|^7.0"
},
"require-dev": {
"symfony/console": "^6.4|^7.0",
"symfony/console": "^7.1",
"symfony/http-client": "^6.4|^7.0",
"symfony/security-core": "^6.4|^7.0",
"symfony/var-dumper": "^6.4|^7.0",
Expand All @@ -31,7 +31,7 @@
"symfony/messenger": "^6.4|^7.0"
},
"conflict": {
"symfony/console": "<6.4",
"symfony/console": "<7.1",
"symfony/http-foundation": "<6.4",
"symfony/security-core": "<6.4"
},
Expand Down
0