10000 [Console] Improve speed NullOutput by tienvx · Pull Request #34869 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Improve speed NullOutput #34869

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

Merged
merged 1 commit into from
Dec 26, 2019
Merged
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
Diff view
72 changes: 72 additions & 0 deletions src/Symfony/Component/Console/Formatter/NullOutputFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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\Component\Console\Formatter;

/**
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
*/
final class NullOutputFormatter implements OutputFormatterInterface
{
private $style;

/**
* {@inheritdoc}
*/
public function format(?string $message): void
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function getStyle(string $name): OutputFormatterStyleInterface
{
if ($this->style) {
return $this->style;
}
// to comply with the interface we must return a OutputFormatterStyleInterface
return $this->style = new NullOutputFormatterStyle();
}

/**
* {@inheritdoc}
*/
public function hasStyle(string $name): bool
{
return false;
}

/**
* {@inheritdoc}
*/
public function isDecorated(): bool
{
return false;
}

/**
* {@inheritdoc}
*/
public function setDecorated(bool $decorated): void
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function setStyle(string $name, OutputFormatterStyleInterface $style): void
{
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Component\Console\Formatter;

/**
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
*/
final class NullOutputFormatterStyle implements OutputFormatterStyleInterface
{
/**
* {@inheritdoc}
*/
public function apply(string $text): string
{
return $text;
}

/**
* {@inheritdoc}
*/
public function setBackground(string $color = null): void
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function setForeground(string $color = null): void
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function setOption(string $option): void
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function setOptions(array $options): void
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function unsetOption(string $option): void
{
// do nothing
}
}
9 changes: 7 additions & 2 deletions src/Symfony/Component/Console/Output/NullOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Console\Output;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\NullOutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;

/**
Expand All @@ -24,6 +24,8 @@
*/
class NullOutput implements OutputInterface
{
private $formatter;

/**
* {@inheritdoc}
*/
Expand All @@ -37,8 +39,11 @@ public function setFormatter(OutputFormatterInterface $formatter)
*/
public function getFormatter()
{
if ($this->formatter) {
return $this->formatter;
}
// to comply with the interface we must return a OutputFormatterInterface
return new OutputFormatter();
return $this->formatter = new NullOutputFormatter();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Component\Console\Tests\Output;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Formatter\NullOutputFormatterStyle;

/**
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
*/
class NullOutputFormatterStyleTest extends TestCase
{
public function testApply()
{
$style = new NullOutputFormatterStyle();

$this->assertSame('foo', $style->apply('foo'));
}

public function testSetForeground()
{
$style = new NullOutputFormatterStyle();
$style->setForeground('black');
$this->assertSame('foo', $style->apply('foo'));
}

public function testSetBackground()
{
$style = new NullOutputFormatterStyle();
$style->setBackground('blue');
$this->assertSame('foo', $style->apply('foo'));
}

public function testOptions()
{
$style = new NullOutputFormatterStyle();

$style->setOptions(['reverse', 'conceal']);
$this->assertSame('foo', $style->apply('foo'));

$style->setOption('bold');
$this->assertSame('foo', $style->apply('foo'));

$style->unsetOption('reverse');
$this->assertSame('foo', $style->apply('foo'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\Component\Console\Tests\Output;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Formatter\NullOutputFormatter;
use Symfony\Component\Console\Formatter\NullOutputFormatterStyle;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

/**
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
*/
class NullOutputFormatterTest extends TestCase
{
public function testFormat()
{
$formatter = new NullOutputFormatter();

$message = 'this message will not be changed';
$formatter->format($message);

$this->assertSame('this message will not be changed', $message);
}

public function testGetStyle()
{
$formatter = new NullOutputFormatter();
$this->assertInstanceof(NullOutputFormatterStyle::class, $style = $formatter->getStyle('null'));
$this->assertSame($style, $formatter->getStyle('null'));
}

public function testSetStyle()
{
$formatter = new NullOutputFormatter();
$style = new OutputFormatterStyle();
$formatter->setStyle('null', $style);
$this->assertNotSame($style, $formatter->getStyle('null'));
< E758 span class='blob-code-inner blob-code-marker ' data-code-marker="+"> }

public function testHasStyle()
{
$formatter = new NullOutputFormatter();
$this->assertFalse($formatter->hasStyle('null'));
}

public function testIsDecorated()
{
$formatter = new NullOutputFormatter();
$this->assertFalse($formatter->isDecorated());
}

public function testSetDecorated()
{
$formatter = new NullOutputFormatter();
$formatter->setDecorated(true);
$this->assertFalse($formatter->isDecorated());
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/Console/Tests/Output/NullOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Output;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Formatter\NullOutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\Output;
Expand Down Expand Up @@ -40,6 +41,13 @@ public function testVerbosity()
$this->assertSame(OutputInterface::VERBOSITY_QUIET, $output->getVerbosity(), '->getVerbosity() always returns VERBOSITY_QUIET for NullOutput');
}

public function testGetFormatter()
{
$output = new NullOutput();
$this->assertInstanceof(NullOutputFormatter::class, $formatter = $output->getFormatter());
$this->assertSame($formatter, $output->getFormatter());
}

public function testSetFormatter()
{
$output = new NullOutput();
Expand Down
0