diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 1e515b8495..afea03640d 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -625,7 +625,7 @@ class Application $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 if (defined('HHVM_VERSION') && $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. * * @return int|null + * + * @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead. */ 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(); } @@ -699,9 +713,13 @@ class Application * Tries to figure out the terminal height in which this application runs. * * @return int|null + * + * @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead. */ 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(); } @@ -709,10 +727,14 @@ class Application * Tries to figure out the terminal dimensions based on the current environment. * * @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() { - 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 * * @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) { - $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; } diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 53a7951e01..8c572833c5 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -25,6 +25,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; +use Symfony\Component\Console\Terminal; /** * Output decorator helpers for the Symfony Style Guide. @@ -50,7 +51,8 @@ class SymfonyStyle extends OutputStyle $this->input = $input; $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. - $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); } @@ -401,14 +403,6 @@ class SymfonyStyle extends OutputStyle return $this->progressBar; } - private function getTerminalWidth() - { - $application = new Application(); - $dimensions = $application->getTerminalDimensions(); - - return $dimensions[0] ?: self::MAX_LINE_LENGTH; - } - private function autoPrependBlock() { $chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2); diff --git a/src/Symfony/Component/Console/Terminal.php b/src/Symfony/Component/Console/Terminal.php index 625a491018..8aec95bf70 100644 --- a/src/Symfony/Component/Console/Terminal.php +++ b/src/Symfony/Component/Console/Terminal.php @@ -17,7 +17,7 @@ class Terminal private $height; /** - * Tries to figure out the terminal width in which this application runs. + * Gets the terminal width. * * @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 */ diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index ee21ad23dc..ac3549f00a 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -476,11 +476,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase public function testSetCatchExceptions() { - $application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); + $application = new Application(); $application->setAutoExit(false); - $application->expects($this->any()) - ->method('getTerminalWidth') - ->will($this->returnValue(120)); + $application->getTerminal()->setWidth(120); $tester = new ApplicationTester($application); $application->setCatchExceptions(true); @@ -514,11 +512,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase public function testRenderException() { - $application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); + $application = new Application(); $application->setAutoExit(false); - $application->expects($this->any()) - ->method('getTerminalWidth') - ->will($this->returnValue(120)); + $application->getTerminal()->setWidth(120); $tester = new ApplicationTester($application); $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)); $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->expects($this->any()) - ->method('getTerminalWidth') - ->will($this->returnValue(32)); + $application->getTerminal()->setWidth(32); $tester = new ApplicationTester($application); $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() { - $application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); + $application = new Application(); $application->setAutoExit(false); - $application->expects($this->any()) - ->method('getTerminalWidth') - ->will($this->returnValue(120)); + $application->getTerminal()->setWidth(120); $application->register('foo')->setCode(function () { 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)); $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->expects($this->any()) - ->method('getTerminalWidth') - ->will($this->returnValue(32)); + $application->getTerminal()->setWidth(32); $application->register('foo')->setCode(function () { throw new \Exception('コマンドの実行中にエラーが発生しました。'); }); @@ -1023,6 +1013,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $this->assertEquals('some test value', $extraValue); } + /** + * @group legacy + */ public function testTerminalDimensions() { $application = new Application(); diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index bf7bb54d93..d78221efb7 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -523,7 +523,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase $output = $this->getOutputStream(); $bar = new ProgressBar($output); - $bar->getTerminal()->setDimensions(12, 50); + $bar->getTerminal()->setWidth(12); $bar->start(); $bar->advance(); diff --git a/src/Symfony/Component/Console/Tests/TerminalTest.php b/src/Symfony/Component/Console/Tests/TerminalTest.php index 959a82295f..edbb8a079c 100644 --- a/src/Symfony/Component/Console/Tests/TerminalTest.php +++ b/src/Symfony/Component/Console/Tests/TerminalTest.php @@ -15,14 +15,11 @@ use Symfony\Component\Console\Terminal; class TerminalTest extends \PHPUnit_Framework_TestCase { - public function testGetDimensions() + public function test() { $terminal = new Terminal(); - $dimensions = $terminal->getDimensions(); - $this->assertCount(2, $dimensions); - - $terminal->setDimensions(100, 50); - $this->assertSame(array(100, 50), $terminal->getDimensions()); + $terminal->setWidth(100); + $terminal->setHeight(50); $this->assertSame(100, $terminal->getWidth()); $this->assertSame(50, $terminal->getHeight()); }