[Console] Improve speed NullOutput

This commit is contained in:
tien.xuan.vo 2019-12-07 12:39:34 +07:00
parent b138fbcb89
commit 75387433be
6 changed files with 275 additions and 2 deletions

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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;
/**
@ -24,6 +24,8 @@ use Symfony\Component\Console\Formatter\OutputFormatterInterface;
*/
class NullOutput implements OutputInterface
{
private $formatter;
/**
* {@inheritdoc}
*/
@ -37,8 +39,11 @@ class NullOutput implements OutputInterface
*/
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();
}
/**

View File

@ -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'));
}
}

View File

@ -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'));
}
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());
}
}

View File

@ -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;
@ -40,6 +41,13 @@ class NullOutputTest extends TestCase
$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();