Skip to content

Commit

Permalink
Merge branch '4.3' into 4.4
Browse files Browse the repository at this point in the history
* 4.3:
  [Twig] Remove dead code
  Add gitignore file for Symfony 4.3
  Add gitignore file for Symfony 3.4
  [Inflector] Add .gitignore file
  [Messenger] Fix exception message of failed message is dropped on retry
  Add default value for Accept header
  [HttpClient] Add .gitignore file
  [Finder] Adjust regex to correctly match comments in gitignore contents
  [Security] Removed unused argument in Test
  [Console] Get dimensions from stty on windows if possible
  [Inflector] add support 'see' to 'ee' for singularize 'fees' to 'fee'
  • Loading branch information
fabpot committed Sep 17, 2019
2 parents 4c470a9 + 9072ba8 commit 3c78fde
Show file tree
Hide file tree
Showing 33 changed files with 234 additions and 87 deletions.
Expand Up @@ -34,9 +34,7 @@ public function testCompile($source, $expected)
$stream = $env->tokenize($source);
$parser = new Parser($env);

if (method_exists($expected, 'setSourceContext')) {
$expected->setSourceContext($source);
}
$expected->setSourceContext($source);

$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
}
Expand Down
11 changes: 4 additions & 7 deletions src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php
Expand Up @@ -112,13 +112,10 @@ public function testExtractSyntaxError($resources)
try {
$extractor->extract($resources, new MessageCatalogue('en'));
} catch (Error $e) {
if (method_exists($e, 'getSourceContext')) {
$this->assertSame(\dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', \DIRECTORY_SEPARATOR), $e->getFile());
$this->assertSame(1, $e->getLine());
$this->assertSame('Unclosed "block".', $e->getMessage());
} else {
$this->expectExceptionMessageRegExp('/Unclosed "block" in ".*extractor(\\/|\\\\)syntax_error\\.twig" at line 1/');
}
$this->assertSame(\dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', \DIRECTORY_SEPARATOR), $e->getFile());
$this->assertSame(1, $e->getLine());
$this->assertSame('Unclosed "block".', $e->getMessage());

throw $e;
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Bridge/Twig/Translation/TwigExtractor.php
Expand Up @@ -61,11 +61,7 @@ public function extract($resource, MessageCatalogue $catalogue)
if ($file instanceof \SplFileInfo) {
$path = $file->getRealPath() ?: $file->getPathname();
$name = $file instanceof SplFileInfo ? $file->getRelativePathname() : $path;
if (method_exists($e, 'setSourceContext')) {
$e->setSourceContext(new Source('', $name, $path));
} else {
$e->setTemplateName($name);
}
$e->setSourceContext(new Source('', $name, $path));
}

throw $e;
Expand Down
23 changes: 0 additions & 23 deletions src/Symfony/Bundle/TwigBundle/TwigEngine.php
Expand Up @@ -15,7 +15,6 @@

use Symfony\Bridge\Twig\TwigEngine as BaseEngine;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\TemplateNameParserInterface;
Expand All @@ -40,28 +39,6 @@ public function __construct(Environment $environment, TemplateNameParserInterfac
$this->locator = $locator;
}

/**
* {@inheritdoc}
*/
public function render($name, array $parameters = [])
{
try {
return parent::render($name, $parameters);
} catch (Error $e) {
if ($name instanceof TemplateReference && !method_exists($e, 'setSourceContext')) {
try {
// try to get the real name of the template where the error occurred
$name = $e->getTemplateName();
$path = (string) $this->locator->locate($this->parser->parse($name));
$e->setTemplateName($path);
} catch (\Exception $e2) {
}
}

throw $e;
}
}

/**
* {@inheritdoc}
*
Expand Down
19 changes: 3 additions & 16 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Terminal;

/**
* The QuestionHelper class provides helpers to interact with the user.
Expand Down Expand Up @@ -117,7 +118,7 @@ private function doAsk(OutputInterface $output, Question $question)
$inputStream = $this->inputStream ?: STDIN;
$autocomplete = $question->getAutocompleterCallback();

if (null === $autocomplete || !$this->hasSttyAvailable()) {
if (null === $autocomplete || !Terminal::hasSttyAvailable()) {
$ret = false;
if ($question->isHidden()) {
try {
Expand Down Expand Up @@ -381,7 +382,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
return $value;
}

if ($this->hasSttyAvailable()) {
if (Terminal::hasSttyAvailable()) {
$sttyMode = shell_exec('stty -g');

shell_exec('stty -echo');
Expand Down Expand Up @@ -467,18 +468,4 @@ private function getShell()

return self::$shell;
}

/**
* Returns whether Stty is available or not.
*/
private function hasSttyAvailable(): bool
{
if (null !== self::$stty) {
return self::$stty;
}

exec('stty 2>&1', $output, $exitcode);

return self::$stty = 0 === $exitcode;
}
}
28 changes: 27 additions & 1 deletion src/Symfony/Component/Console/Terminal.php
Expand Up @@ -15,6 +15,7 @@ class Terminal
{
private static $width;
private static $height;
private static $stty;

/**
* Gets the terminal width.
Expand Down Expand Up @@ -54,6 +55,22 @@ public function getHeight()
return self::$height ?: 50;
}

/**
* @internal
*
* @return bool
*/
public static function hasSttyAvailable()
{
if (null !== self::$stty) {
return self::$stty;
}

exec('stty 2>&1', $output, $exitcode);

return self::$stty = 0 === $exitcode;
}

private static function initDimensions()
{
if ('\\' === \DIRECTORY_SEPARATOR) {
Expand All @@ -62,12 +79,21 @@ private static function initDimensions()
// or [w, h] from "wxh"
self::$width = (int) $matches[1];
self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
} elseif (self::hasSttyAvailable()) {
self::initDimensionsUsingStty();
} elseif (null !== $dimensions = self::getConsoleMode()) {
// extract [w, h] from "wxh"
self::$width = (int) $dimensions[0];
self::$height = (int) $dimensions[1];
}
} elseif ($sttyString = self::getSttyColumns()) {
} else {
self::initDimensionsUsingStty();
}
}

private static function initDimensionsUsingStty()
{
if ($sttyString = self::getSttyColumns()) {
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
// extract [w, h] from "rows h; columns w;"
self::$width = (int) $matches[2];
Expand Down
20 changes: 7 additions & 13 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Terminal;

/**
* @group tty
Expand Down Expand Up @@ -181,7 +182,7 @@ public function testAskNonTrimmed()

public function testAskWithAutocomplete()
{
if (!$this->hasSttyAvailable()) {
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

Expand Down Expand Up @@ -291,7 +292,7 @@ function ($word) use ($suggestionBase) {

public function testAskWithAutocompleteWithNonSequentialKeys()
{
if (!$this->hasSttyAvailable()) {
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

Expand All @@ -310,7 +311,7 @@ public function testAskWithAutocompleteWithNonSequentialKeys()

public function testAskWithAutocompleteWithExactMatch()
{
if (!$this->hasSttyAvailable()) {
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

Expand Down Expand Up @@ -346,7 +347,7 @@ public function getInputs()
*/
public function testAskWithAutocompleteWithMultiByteCharacter($character)
{
if (!$this->hasSttyAvailable()) {
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

Expand All @@ -370,7 +371,7 @@ public function testAskWithAutocompleteWithMultiByteCharacter($character)

public function testAutocompleteWithTrailingBackslash()
{
if (!$this->hasSttyAvailable()) {
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

Expand Down Expand Up @@ -732,7 +733,7 @@ public function testEmptyChoices()

public function testTraversableAutocomplete()
{
if (!$this->hasSttyAvailable()) {
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

Expand Down Expand Up @@ -817,13 +818,6 @@ protected function createInputInterfaceMock($interactive = true)

return $mock;
}

private function hasSttyAvailable()
{
exec('stty 2>&1', $output, $exitcode);

return 0 === $exitcode;
}
}

class AutocompleteValues implements \IteratorAggregate
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Console/Tests/TerminalTest.php
Expand Up @@ -18,17 +18,31 @@ class TerminalTest extends TestCase
{
private $colSize;
private $lineSize;
private $ansiCon;

protected function setUp(): void
{
$this->colSize = getenv('COLUMNS');
$this->lineSize = getenv('LINES');
$this->ansiCon = getenv('ANSICON');
$this->resetStatics();
}

protected function tearDown(): void
{
putenv($this->colSize ? 'COLUMNS='.$this->colSize : 'COLUMNS');
putenv($this->lineSize ? 'LINES' : 'LINES='.$this->lineSize);
putenv($this->ansiCon ? 'ANSICON='.$this->ansiCon : 'ANSICON');
$this->resetStatics();
}

private function resetStatics()
{
foreach (['height', 'width', 'stty'] as $name) {
$property = new \ReflectionProperty(Terminal::class, $name);
$property->setAccessible(true);
$property->setValue(null);
}
}

public function test()
Expand Down Expand Up @@ -56,4 +70,28 @@ public function test_zero_values()
$this->assertSame(0, $terminal->getWidth());
$this->assertSame(0, $terminal->getHeight());
}

public function testSttyOnWindows()
{
if ('\\' !== \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Must be on windows');
}

$sttyString = exec('(stty -a | grep columns) 2>&1', $output, $exitcode);
if (0 !== $exitcode) {
$this->markTestSkipped('Must have stty support');
}

$matches = [];
if (0 === preg_match('/columns.(\d+)/i', $sttyString, $matches)) {
$this->fail('Could not determine existing stty columns');
}

putenv('COLUMNS');
putenv('LINES');
putenv('ANSICON');

$terminal = new Terminal();
$this->assertSame((int) $matches[1], $terminal->getWidth());
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/Gitignore.php
Expand Up @@ -25,7 +25,7 @@ class Gitignore
*/
public static function toRegex(string $gitignoreFileContent): string
{
$gitignoreFileContent = preg_replace('/^[^\\\\]*#.*/', '', $gitignoreFileContent);
$gitignoreFileContent = preg_replace('/^[^\\\r\n]*#.*/m', '', $gitignoreFileContent);
$gitignoreLines = preg_split('/\r\n|\r|\n/', $gitignoreFileContent);
$gitignoreLines = array_map('trim', $gitignoreLines);
$gitignoreLines = array_filter($gitignoreLines);
Expand Down
26 changes: 25 additions & 1 deletion src/Symfony/Component/Finder/Tests/GitignoreTest.php
Expand Up @@ -107,7 +107,31 @@ public function provider(): array
#IamComment
/app/cache/',
['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
['a/app/cache/file.txt'],
['a/app/cache/file.txt', '#IamComment', 'IamComment'],
],
[
'
/app/cache/
#LastLineIsComment',
['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
['a/app/cache/file.txt', '#LastLineIsComment', 'LastLineIsComment'],
],
[
'
/app/cache/
\#file.txt
#LastLineIsComment',
['app/cache/file.txt', 'app/cache/subdir/ile.txt', '#file.txt'],
['a/app/cache/file.txt', '#LastLineIsComment', 'LastLineIsComment'],
],
[
'
/app/cache/
\#file.txt
#IamComment
another_file.txt',
['app/cache/file.txt', 'app/cache/subdir/ile.txt', '#file.txt', 'another_file.txt'],
['a/app/cache/file.txt', 'IamComment', '#IamComment'],
],
];
}
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpClient/.gitignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Expand Up @@ -54,6 +54,10 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
}
}

if (!isset($options['normalized_headers']['accept'])) {
$options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: *'];
}

if (isset($options['body'])) {
$options['body'] = self::normalizeBody($options['body']);
}
Expand Down
Expand Up @@ -172,7 +172,7 @@ public function provideRemoveDotSegments()
public function testAuthBearerOption()
{
[, $options] = self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foobar'], HttpClientInterface::OPTIONS_DEFAULTS);
$this->assertSame(['Authorization: Bearer foobar'], $options['headers']);
$this->assertSame(['Accept: *', 'Authorization: Bearer foobar'], $options['headers']);
$this->assertSame(['Authorization: Bearer foobar'], $options['normalized_headers']['authorization']);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Inflector/.gitignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
3 changes: 3 additions & 0 deletions src/Symfony/Component/Inflector/Inflector.php
Expand Up @@ -120,6 +120,9 @@ final class Inflector
// bureaus (bureau)
['suae', 4, false, true, 'eau'],

// fees (fee), trees (tree), employees (employee)
['see', 3, true, true, 'ee'],

// roses (rose), garages (garage), cassettes (cassette),
// waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
// shoes (shoe)
Expand Down

0 comments on commit 3c78fde

Please sign in to comment.