8000 Merge branch '2.7' into 2.8 · symfony/symfony@4d2cfd0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d2cfd0

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [TwigBridge] removed Twig null nodes (deprecated as of Twig 1.25) [Console] Fix empty optionnal options with = separator in argv
2 parents 0ae3838 + 79e25a9 commit 4d2cfd0

File tree

7 files changed

+76
-23
lines changed

7 files changed

+76
-23
lines changed

src/Symfony/Bridge/Twig/Node/DumpNode.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ class DumpNode extends \Twig_Node
2020

2121
public function __construct($varPrefix, \Twig_Node $values = null, $lineno, $tag = null)
2222
{
23-
parent::__construct(array('values' => $values), array(), $lineno, $tag);
23+
$nodes = array();
24+
if (null !== $values) {
25+
$nodes['values'] = $values;
26+
}
27+
28+
parent::__construct($nodes, array(), $lineno, $tag);
2429
$this->varPrefix = $varPrefix;
2530
}
2631

@@ -33,9 +38,7 @@ public function compile(\Twig_Compiler $compiler)
3338
->write("if (\$this->env->isDebug()) {\n")
3439
->indent();
3540

36-
$values = $this->getNode('values');
37-
38-
if (null === $values) {
41+
if (!$this->hasNode('values')) {
3942
// remove embedded templates (macros) from the context
4043
$compiler
4144
->write(sprintf('$%svars = array();'."\n", $this->varPrefix))
@@ -50,7 +53,7 @@ public function compile(\Twig_Compiler $compiler)
5053
->write("}\n")
5154
->addDebugInfo($this)
5255
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix));
53-
} elseif (1 === $values->count()) {
56+
} elseif (($values = $this->getNode('values')) && 1 === $values->count()) {
5457
$compiler
5558
->addDebugInfo($this)
5659
->write('\Symfony\Component\VarDumper\VarDumper::dump(')

src/Symfony/Bridge/Twig/Node/StopwatchNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class StopwatchNode extends \Twig_Node
2020
{
21-
public function __construct(\Twig_Node $name, $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null)
21+
public function __construct(\Twig_Node $name, \Twig_Node $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null)
2222
{
2323
parent::__construct(array('body' => $body, 'name' => $name, 'var' => $var), array(), $lineno, $tag);
2424
}

src/Symfony/Bridge/Twig/Node/TransNode.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@ class TransNode extends \Twig_Node
1818
{
1919
public function __construct(\Twig_Node $body, \Twig_Node $domain = null, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
2020
{
21-
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
21+
$nodes = array('body' => $body);
22+
if (null !== $domain) {
23+
$nodes['domain'] = $domain;
24+
}
25+
if (null !== $count) {
26+
$nodes['count'] = $count;
27+
}
28+
if (null !== $vars) {
29+
$nodes['vars'] = $vars;
30+
}
31+
if (null !== $locale) {
32+
$nodes['locale'] = $locale;
33+
}
34+
35+
parent::__construct($nodes, array(), $lineno, $tag);
2236
}
2337

2438
/**
@@ -30,15 +44,14 @@ public function compile(\Twig_Compiler $compiler)
3044
{
3145
$compiler->addDebugInfo($this);
3246

33-
$vars = $this->getNode('vars');
3447
$defaults = new \Twig_Node_Expression_Array(array(), -1);
35-
if ($vars instanceof \Twig_Node_Expression_Array) {
48+
if ($this->hasNode('vars') && ($vars = $this->getNode('vars')) instanceof \Twig_Node_Expression_Array) {
3649
$defaults = $this->getNode('vars');
3750
$vars = null;
3851
}
3952
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
4053

41-
$method = null === $this->getNode('count') ? 'trans' : 'transChoice';
54+
$method = !$this->hasNode('count') ? 'trans' : 'transChoice';
4255

4356
$compiler
4457
->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
@@ -47,7 +60,7 @@ public function compile(\Twig_Compiler $compiler)
4760

4861
$compiler->raw(', ');
4962

50-
if (null !== $this->getNode('count')) {
63+
if ($this->hasNode('count')) {
5164
$compiler
5265
->subcompile($this->getNode('count'))
5366
->raw(', ')
@@ -68,13 +81,13 @@ public function compile(\Twig_Compiler $compiler)
6881

6982
$compiler->raw(', ');
7083

71-
if (null === $this->getNode('domain')) {
84+
if (!$this->hasNode('domain')) {
7285
$compiler->repr('messages');
7386
} else {
7487
$compiler->subcompile($this->getNode('domain'));
7588
}
7689

77-
if (null !== $this->getNode('locale')) {
90+
if ($this->hasNode('locale')) {
7891
$compiler
7992
->raw(', ')
8093
->subcompile($this->getNode('locale'))
@@ -98,7 +111,7 @@ protected function compileString(\Twig_Node $body, \Twig_Node_Expression_Array $
98111
foreach ($matches[1] as $var) {
99112
$key = new \Twig_Node_Expression_Constant('%'.$var.'%', $body->getLine());
100113
if (!$vars->hasElement($key)) {
101-
if ('count' === $var && null !== $this->getNode('count')) {
114+
if ('count' === $var && $this->hasNode('count')) {
102115
$vars->addElement($this->getNode('count'), $key);
103116
} else {
104117
$varExpr = new \Twig_Node_Expression_Name($var, $body->getLine());

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
7878
}
7979
}
8080
} elseif ($node instanceof TransNode) {
81-
if (null === $node->getNode('domain')) {
81+
if (!$node->hasNode('domain')) {
8282
$node->setNode('domain', $this->scope->get('domain'));
8383
}
8484
}

src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
7575
// extract trans nodes
7676
$this->messages[] = array(
7777
$node->getNode('body')->getAttribute('data'),
78-
$this->getReadDomainFromNode($node->getNode('domain')),
78+
$node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : null,
7979
);
8080
}
8181

@@ -122,12 +122,8 @@ private function getReadDomainFromArguments(\Twig_Node $arguments, $index)
122122
*
123123
* @return string|null
124124
*/
125-
private function getReadDomainFromNode(\Twig_Node $node = null)
125+
private function getReadDomainFromNode(\Twig_Node $node)
126126
{
127-
if (null === $node) {
128-
return;
129-
}
130-
131127
if ($node instanceof \Twig_Node_Expression_Constant) {
132128
return $node->getAttribute('value');
133129
}

src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ private function parseLongOption($token)
147147
$name = substr($token, 2);
148148

149149
if (false !== $pos = strpos($name, '=')) {
150-
$this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
150+
if (0 === strlen($value = substr($name, $pos + 1))) {
151+
array_unshift($this->parsed, null);
152+
}
153+
$this->addLongOption(substr($name, 0, $pos), $value);
151154
} else {
152155
$this->addLongOption($name, null);
153156
}
@@ -234,7 +237,7 @@ private function addLongOption($name, $value)
234237
if (isset($next[0]) && '-' !== $next[0]) {
235238
$value = $next;
236239
} elseif (empty($next)) {
237-
$value = '';
240+
$value = null;
238241
} else {
239242
array_unshift($this->parsed, $next);
240243
}

src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public function provideOptions()
7171
array('foo' => 'bar'),
7272
'->parse() parses long options with a required value (with a space separator)',
7373
),
74+
array(
75+
array('cli.php', '--foo='),
76+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
77+
array('foo' => null),
78+
'->parse() parses long options with optional value which is empty (with a = separator) as null',
79+
),
80+
array(
81+
array('cli.php', '--foo=', 'bar'),
82+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
83+
array('foo' => null),
84+
'->parse() parses long options with optional value which is empty (with a = separator) followed by an argument',
85+
),
7486
array(
7587
array('cli.php', '-f'),
7688
array(new InputOption('foo', 'f')),
@@ -324,4 +336,30 @@ public function testParseSingleDashAsArgument()
324336
$input->bind(new InputDefinition(array(new InputArgument('file'))));
325337
$this->assertEquals(array('file' => '-'), $input->getArguments(), '->parse() parses single dash as an argument');
326338
}
339+
340+
public function testParseOptionWithValueOptionalGivenEmptyAndRequiredArgument()
341+
{
342+
$input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
343+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
344+
$this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
345+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
346+
347+
$input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
348+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
349+
$this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
350+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
351+
}
352+
353+
public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
354+
{
355+
$input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
356+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
357+
$this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
358+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
359+
360+
$input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
361+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
362+
$this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
363+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
364+
}
327365
}

0 commit comments

Comments
 (0)
0