deprecated some Console Application methods

This commit is contained in:
Fabien Potencier 2016-06-09 17:22:51 +02:00
parent 8f206c86d7
commit a589635643
6 changed files with 52 additions and 41 deletions

View File

@ -625,7 +625,7 @@ class Application
$len = $this->stringWidth($title); $len = $this->stringWidth($title);
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX; $width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327 // HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
if (defined('HHVM_VERSION') && $width > 1 << 31) { if (defined('HHVM_VERSION') && $width > 1 << 31) {
$width = 1 << 31; $width = 1 << 31;
@ -685,13 +685,27 @@ class Application
} }
} }
/**
* Returns the current terminal.
*
* @return Terminal
*/
public function getTerminal()
{
return $this->terminal;
}
/** /**
* Tries to figure out the terminal width in which this application runs. * Tries to figure out the terminal width in which this application runs.
* *
* @return int|null * @return int|null
*
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
*/ */
protected function getTerminalWidth() protected function getTerminalWidth()
{ {
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
return $this->terminal->getWidth(); return $this->terminal->getWidth();
} }
@ -699,9 +713,13 @@ class Application
* Tries to figure out the terminal height in which this application runs. * Tries to figure out the terminal height in which this application runs.
* *
* @return int|null * @return int|null
*
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
*/ */
protected function getTerminalHeight() protected function getTerminalHeight()
{ {
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
return $this->terminal->getHeight(); return $this->terminal->getHeight();
} }
@ -709,10 +727,14 @@ class Application
* Tries to figure out the terminal dimensions based on the current environment. * Tries to figure out the terminal dimensions based on the current environment.
* *
* @return array Array containing width and height * @return array Array containing width and height
*
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
*/ */
public function getTerminalDimensions() public function getTerminalDimensions()
{ {
return $this->terminal->getDimensions(); @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
return array($this->terminal->getWidth(), $this->terminal->getHeight());
} }
/** /**
@ -724,10 +746,15 @@ class Application
* @param int $height The height * @param int $height The height
* *
* @return Application The current application * @return Application The current application
*
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
*/ */
public function setTerminalDimensions($width, $height) public function setTerminalDimensions($width, $height)
{ {
$this->terminal->setDimensions($width, $height); @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
$this->terminal->setWidth($width);
$this->terminal->setHeight($height);
return $this; return $this;
} }

View File

@ -25,6 +25,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Terminal;
/** /**
* Output decorator helpers for the Symfony Style Guide. * Output decorator helpers for the Symfony Style Guide.
@ -50,7 +51,8 @@ class SymfonyStyle extends OutputStyle
$this->input = $input; $this->input = $input;
$this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter()); $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
// Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not. // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
$this->lineLength = min($this->getTerminalWidth() - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH); $width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
$this->lineLength = min($width - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
parent::__construct($output); parent::__construct($output);
} }
@ -401,14 +403,6 @@ class SymfonyStyle extends OutputStyle
return $this->progressBar; return $this->progressBar;
} }
private function getTerminalWidth()
{
$application = new Application();
$dimensions = $application->getTerminalDimensions();
return $dimensions[0] ?: self::MAX_LINE_LENGTH;
}
private function autoPrependBlock() private function autoPrependBlock()
{ {
$chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2); $chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);

View File

@ -17,7 +17,7 @@ class Terminal
private $height; private $height;
/** /**
* Tries to figure out the terminal width in which this application runs. * Gets the terminal width.
* *
* @return int|null * @return int|null
*/ */
@ -41,7 +41,7 @@ class Terminal
} }
/** /**
* Tries to figure out the terminal height in which this application runs. * Gets the terminal height.
* *
* @return int|null * @return int|null
*/ */

View File

@ -476,11 +476,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
public function testSetCatchExceptions() public function testSetCatchExceptions()
{ {
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); $application = new Application();
$application->setAutoExit(false); $application->setAutoExit(false);
$application->expects($this->any()) $application->getTerminal()->setWidth(120);
->method('getTerminalWidth')
->will($this->returnValue(120));
$tester = new ApplicationTester($application); $tester = new ApplicationTester($application);
$application->setCatchExceptions(true); $application->setCatchExceptions(true);
@ -514,11 +512,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
public function testRenderException() public function testRenderException()
{ {
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); $application = new Application();
$application->setAutoExit(false); $application->setAutoExit(false);
$application->expects($this->any()) $application->getTerminal()->setWidth(120);
->method('getTerminalWidth')
->will($this->returnValue(120));
$tester = new ApplicationTester($application); $tester = new ApplicationTester($application);
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true)); $tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
@ -546,11 +542,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$tester->run(array('command' => 'foo3:bar'), array('decorated' => true, 'capture_stderr_separately' => true)); $tester->run(array('command' => 'foo3:bar'), array('decorated' => true, 'capture_stderr_separately' => true));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions'); $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); $application = new Application();
$application->setAutoExit(false); $application->setAutoExit(false);
$application->expects($this->any()) $application->getTerminal()->setWidth(32);
->method('getTerminalWidth')
->will($this->returnValue(32));
$tester = new ApplicationTester($application); $tester = new ApplicationTester($application);
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true)); $tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
@ -559,11 +553,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
public function testRenderExceptionWithDoubleWidthCharacters() public function testRenderExceptionWithDoubleWidthCharacters()
{ {
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); $application = new Application();
$application->setAutoExit(false); $application->setAutoExit(false);
$application->expects($this->any()) $application->getTerminal()->setWidth(120);
->method('getTerminalWidth')
->will($this->returnValue(120));
$application->register('foo')->setCode(function () { $application->register('foo')->setCode(function () {
throw new \Exception('エラーメッセージ'); throw new \Exception('エラーメッセージ');
}); });
@ -575,11 +567,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$tester->run(array('command' => 'foo'), array('decorated' => true, 'capture_stderr_separately' => true)); $tester->run(array('command' => 'foo'), array('decorated' => true, 'capture_stderr_separately' => true));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions'); $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); $application = new Application();
$application->setAutoExit(false); $application->setAutoExit(false);
$application->expects($this->any()) $application->getTerminal()->setWidth(32);
->method('getTerminalWidth')
->will($this->returnValue(32));
$application->register('foo')->setCode(function () { $application->register('foo')->setCode(function () {
throw new \Exception('コマンドの実行中にエラーが発生しました。'); throw new \Exception('コマンドの実行中にエラーが発生しました。');
}); });
@ -1023,6 +1013,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('some test value', $extraValue); $this->assertEquals('some test value', $extraValue);
} }
/**
* @group legacy
*/
public function testTerminalDimensions() public function testTerminalDimensions()
{ {
$application = new Application(); $application = new Application();

View File

@ -523,7 +523,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
$output = $this->getOutputStream(); $output = $this->getOutputStream();
$bar = new ProgressBar($output); $bar = new ProgressBar($output);
$bar->getTerminal()->setDimensions(12, 50); $bar->getTerminal()->setWidth(12);
$bar->start(); $bar->start();
$bar->advance(); $bar->advance();

View File

@ -15,14 +15,11 @@ use Symfony\Component\Console\Terminal;
class TerminalTest extends \PHPUnit_Framework_TestCase class TerminalTest extends \PHPUnit_Framework_TestCase
{ {
public function testGetDimensions() public function test()
{ {
$terminal = new Terminal(); $terminal = new Terminal();
$dimensions = $terminal->getDimensions(); $terminal->setWidth(100);
$this->assertCount(2, $dimensions); $terminal->setHeight(50);
$terminal->setDimensions(100, 50);
$this->assertSame(array(100, 50), $terminal->getDimensions());
$this->assertSame(100, $terminal->getWidth()); $this->assertSame(100, $terminal->getWidth());
$this->assertSame(50, $terminal->getHeight()); $this->assertSame(50, $terminal->getHeight());
} }