1
0
Fork 0

Merge pull request #6788 from GawainLynch/symfony-4

Symfony 4
pull/6842/merge
Jordi Boggiano 2017-11-28 18:30:31 +01:00 committed by GitHub
commit 9850621c6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 63 deletions

View File

@ -28,10 +28,10 @@
"composer/semver": "^1.0", "composer/semver": "^1.0",
"composer/spdx-licenses": "^1.0", "composer/spdx-licenses": "^1.0",
"seld/jsonlint": "^1.4", "seld/jsonlint": "^1.4",
"symfony/console": "^2.7 || ^3.0", "symfony/console": "^2.7 || ^3.0 || ^4.0",
"symfony/finder": "^2.7 || ^3.0", "symfony/finder": "^2.7 || ^3.0 || ^4.0",
"symfony/process": "^2.7 || ^3.0", "symfony/process": "^2.7 || ^3.0 || ^4.0",
"symfony/filesystem": "^2.7 || ^3.0", "symfony/filesystem": "^2.7 || ^3.0 || ^4.0",
"seld/phar-utils": "^1.0", "seld/phar-utils": "^1.0",
"seld/cli-prompt": "^1.0", "seld/cli-prompt": "^1.0",
"psr/log": "^1.0" "psr/log": "^1.0"

View File

@ -12,32 +12,33 @@
namespace Composer\Command; namespace Composer\Command;
use Composer\DependencyResolver\Pool; use Composer\Composer;
use Composer\DependencyResolver\DefaultPolicy; use Composer\DependencyResolver\DefaultPolicy;
use Composer\DependencyResolver\Pool;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Package\CompletePackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Package\BasePackage; use Composer\Package\BasePackage;
use Composer\Package\CompletePackageInterface;
use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Package\Version\VersionSelector; use Composer\Package\Version\VersionSelector;
use Composer\Plugin\CommandEvent; use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginEvents;
use Composer\Package\PackageInterface; use Composer\Repository\ArrayRepository;
use Composer\Repository\ComposerRepository;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositoryFactory;
use Composer\Repository\RepositoryInterface;
use Composer\Semver\Constraint\ConstraintInterface; use Composer\Semver\Constraint\ConstraintInterface;
use Composer\Semver\Semver;
use Composer\Spdx\SpdxLicenses;
use Composer\Util\Platform; use Composer\Util\Platform;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Composer\Repository\ArrayRepository; use Symfony\Component\Console\Terminal;
use Composer\Repository\CompositeRepository;
use Composer\Repository\ComposerRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositoryFactory;
use Composer\Spdx\SpdxLicenses;
use Composer\Composer;
use Composer\Semver\Semver;
/** /**
* @author Robert Schönthal <seroscho@googlemail.com> * @author Robert Schönthal <seroscho@googlemail.com>
@ -255,7 +256,13 @@ EOT
$packageListFilter = $this->getRootRequires(); $packageListFilter = $this->getRootRequires();
} }
if (class_exists('Symfony\Component\Console\Terminal')) {
$terminal = new Terminal();
$width = $terminal->getWidth();
} else {
// For versions of Symfony console before 3.2
list($width) = $this->getApplication()->getTerminalDimensions(); list($width) = $this->getApplication()->getTerminalDimensions();
}
if (null === $width) { if (null === $width) {
// In case the width is not detected, we're probably running the command // In case the width is not detected, we're probably running the command
// outside of a real terminal, use space without a limit // outside of a real terminal, use space without a limit

View File

@ -12,11 +12,12 @@
namespace Composer\IO; namespace Composer\IO;
use Composer\Question\StrictConfirmationQuestion;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Question\ChoiceQuestion;
use Composer\Question\StrictConfirmationQuestion;
use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\Question;
/** /**
@ -281,11 +282,14 @@ class ConsoleIO extends BaseIO
*/ */
public function select($question, $choices, $default, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) public function select($question, $choices, $default, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
{ {
if ($this->isInteractive()) { /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
return $this->helperSet->get('dialog')->select($this->getErrorOutput(), $question, $choices, $default, $attempts, $errorMessage, $multiselect); $helper = $this->helperSet->get('question');
} $question = new ChoiceQuestion($question, $choices, $default);
$question->setMaxAttempts($attempts ?: null); // IOInterface requires false, and Question requires null or int
$question->setErrorMessage($errorMessage);
$question->setMultiselect($multiselect);
return $default; return $helper->ask($this->input, $this->getErrorOutput(), $question);
} }
/** /**

View File

@ -12,9 +12,9 @@
namespace Composer\Util; namespace Composer\Util;
use Composer\IO\IOInterface;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessUtils; use Symfony\Component\Process\ProcessUtils;
use Composer\IO\IOInterface;
/** /**
* @author Robert Schönthal <seroscho@googlemail.com> * @author Robert Schönthal <seroscho@googlemail.com>
@ -131,6 +131,59 @@ class ProcessExecutor
*/ */
public static function escape($argument) public static function escape($argument)
{ {
if (method_exists('Symfony\Component\Process\ProcessUtils', 'escapeArgument')) {
return ProcessUtils::escapeArgument($argument); return ProcessUtils::escapeArgument($argument);
} }
return self::escapeArgument($argument);
}
/**
* Copy of ProcessUtils::escapeArgument() that is removed in Symfony 4.
*
* @param string $argument
*
* @return string
*/
private static function escapeArgument($argument)
{
//Fix for PHP bug #43784 escapeshellarg removes % from given string
//Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
//@see https://bugs.php.net/bug.php?id=43784
//@see https://bugs.php.net/bug.php?id=49446
if ('\\' === DIRECTORY_SEPARATOR) {
if ('' === $argument) {
return escapeshellarg($argument);
}
$escapedArgument = '';
$quote = false;
foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
if ('"' === $part) {
$escapedArgument .= '\\"';
} elseif (self::isSurroundedBy($part, '%')) {
// Avoid environment variable expansion
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
} else {
// escape trailing backslash
if ('\\' === substr($part, -1)) {
$part .= '\\';
}
$quote = true;
$escapedArgument .= $part;
}
}
if ($quote) {
$escapedArgument = '"'.$escapedArgument.'"';
}
return $escapedArgument;
}
return "'".str_replace("'", "'\\''", $argument)."'";
}
private static function isSurroundedBy($arg, $char)
{
return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
}
} }

View File

@ -229,27 +229,27 @@ class ConsoleIOTest extends TestCase
{ {
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
$dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper'); $helperMock = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); $setMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
$inputMock->expects($this->once()) $helperMock
->method('isInteractive') ->expects($this->once())
->will($this->returnValue(true)); ->method('ask')
$dialogMock->expects($this->once()) ->with(
->method('select') $this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'), $this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
$this->equalTo('Select item'), $this->isInstanceOf('Symfony\Component\Console\Question\Question')
$this->equalTo(array("item1", "item2")), )
$this->equalTo(null), ;
$this->equalTo(false),
$this->equalTo("Error message"), $setMock
$this->equalTo(true)); ->expects($this->once())
$helperMock->expects($this->once())
->method('get') ->method('get')
->with($this->equalTo('dialog')) ->with($this->equalTo('question'))
->will($this->returnValue($dialogMock)); ->will($this->returnValue($helperMock))
;
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); $consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
$consoleIO->select('Select item', array("item1", "item2"), null, false, "Error message", true); $consoleIO->select('Select item', array("item1", "item2"), null, false, "Error message", true);
} }

View File

@ -16,6 +16,8 @@ use Composer\Question\StrictConfirmationQuestion;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\StreamableInputInterface;
use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Output\StreamOutput;
/** /**
@ -42,11 +44,11 @@ class StrictConfirmationQuestionTest extends TestCase
*/ */
public function testAskConfirmationBadAnswer($answer) public function testAskConfirmationBadAnswer($answer)
{ {
$dialog = new QuestionHelper(); list($input, $dialog) = $this->createInput($answer."\n");
$dialog->setInputStream($this->getInputStream($answer."\n"));
$question = new StrictConfirmationQuestion('Do you like French fries?'); $question = new StrictConfirmationQuestion('Do you like French fries?');
$question->setMaxAttempts(1); $question->setMaxAttempts(1);
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); $dialog->ask($input, $this->createOutputInterface(), $question);
} }
/** /**
@ -54,11 +56,10 @@ class StrictConfirmationQuestionTest extends TestCase
*/ */
public function testAskConfirmation($question, $expected, $default = true) public function testAskConfirmation($question, $expected, $default = true)
{ {
$dialog = new QuestionHelper(); list($input, $dialog) = $this->createInput($question."\n");
$dialog->setInputStream($this->getInputStream($question."\n"));
$question = new StrictConfirmationQuestion('Do you like French fries?', $default); $question = new StrictConfirmationQuestion('Do you like French fries?', $default);
$this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel')); $this->assertEquals($expected, $dialog->ask($input, $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
} }
public function getAskConfirmationData() public function getAskConfirmationData()
@ -75,13 +76,13 @@ class StrictConfirmationQuestionTest extends TestCase
public function testAskConfirmationWithCustomTrueAndFalseAnswer() public function testAskConfirmationWithCustomTrueAndFalseAnswer()
{ {
$dialog = new QuestionHelper();
$question = new StrictConfirmationQuestion('Do you like French fries?', false, '/^ja$/i', '/^nein$/i'); $question = new StrictConfirmationQuestion('Do you like French fries?', false, '/^ja$/i', '/^nein$/i');
$dialog->setInputStream($this->getInputStream("ja\n"));
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); list($input, $dialog) = $this->createInput("ja\n");
$dialog->setInputStream($this->getInputStream("nein\n")); $this->assertTrue($dialog->ask($input, $this->createOutputInterface(), $question));
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
list($input, $dialog) = $this->createInput("nein\n");
$this->assertFalse($dialog->ask($input, $this->createOutputInterface(), $question));
} }
protected function getInputStream($input) protected function getInputStream($input)
@ -98,13 +99,19 @@ class StrictConfirmationQuestionTest extends TestCase
return new StreamOutput(fopen('php://memory', 'r+', false)); return new StreamOutput(fopen('php://memory', 'r+', false));
} }
protected function createInputInterfaceMock($interactive = true) protected function createInput($entry)
{ {
$mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $stream = $this->getInputStream($entry);
$mock->expects($this->any()) $input = new ArrayInput(array('--no-interaction'));
->method('isInteractive') $dialog = new QuestionHelper();
->will($this->returnValue($interactive));
return $mock; if (method_exists($dialog, 'setInputStream')) {
$dialog->setInputStream($stream);
}
if ($input instanceof StreamableInputInterface) {
$input->setStream($stream);
}
return array($input, $dialog);
} }
} }