8000 bug #12030 Fix expression language in the container when using the "c… · src-run/symfony@da4e85e · GitHub
[go: up one dir, main page]

Skip to content

Commit da4e85e

Browse files
committed
bug symfony#12030 Fix expression language in the container when using the "container" variable (fabpot)
This PR was squashed before being merged into the 2.4 branch (closes symfony#12030). Discussion ---------- Fix expression language in the container when using the "container" variable | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#11995 | License | MIT | Doc PR | n/a See symfony#11995 for the description of the problem. Commits ------- 2b2f0df Fix expression language in the container when using the "container" variable
2 parents efb1237 + 2b2f0df commit da4e85e

File tree

12 files changed

+33
-11
lines changed

12 files changed

+33
-11
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ private function dumpValue($value, $interpolate = true)
12121212

12131213
return $this->getServiceCall((string) $value, $value);
12141214
} elseif ($value instanceof Expression) {
1215-
return $this->getExpressionLanguage()->compile((string) $value, array('container'));
1215+
return $this->getExpressionLanguage()->compile((string) $value, array('this' => 'container'));
12161216
} elseif ($value instanceof Parameter) {
12171217
return $this->dumpParameter($value);
12181218
} elseif (true === $interpolate && is_string($value)) {

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
addMethodCall('setBar', array(new Reference('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)))->
5353
addMethodCall('setBar', array(new Reference('foo3', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->
5454
addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->
55-
addMethodCall('setBar', array(new Expression('service("foo").foo() ~ parameter("foo")')))
55+
addMethodCall('setBar', array(new Expression('service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")')))
5656
;
5757
$container->
5858
register('factory_service', 'Bar')->

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ protected function getMethodCall1Service()
197197
if ($this->has('foobaz')) {
198198
$instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE));
199199
}
200-
$instance->setBar(($this->get("foo")->foo() . $this->getParameter("foo")));
200+
$instance->setBar(($this->get("foo")->foo() . (($this->hasparameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
201201

202202
return $instance;
203203
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected function getMethodCall1Service()
202202

203203
$instance->setBar($this->get('foo'));
204204
$instance->setBar(NULL);
205-
$instance->setBar(($this->get("foo")->foo() . $this->getParameter("foo")));
205+
$instance->setBar(($this->get("foo")->foo() . (($this->hasparameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
206206

207207
return $instance;
208208
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services6.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<service id="method_call1" class="FooClass">
3434
<call method="setBar" />
3535
<call method="setBar">
36-
<argument type="expression">service("foo").foo() ~ parameter("foo")</argument>
36+
<argument type="expression">service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")</argument>
3737
</call>
3838
</service>
3939
<service id="method_call2" class="FooClass">

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<argument type="service" id="foobaz" on-invalid="ignore"/>
5555
</call>
5656
<call method="setBar">
57-
<argument type="expression">service("foo").foo() ~ parameter("foo")</argument>
57+
<argument type="expression">service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")</argument>
5858
</call>
5959
</service>
6060
<service id="factory_service" class="Bar" factory-method="getInstance" factory-service="foo.baz"/>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
calls:
1616
- [ setBar, [] ]
1717
- [ setBar ]
18-
- [ setBar, ['@=service("foo").foo() ~ parameter("foo")'] ]
18+
- [ setBar, ['@=service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")'] ]
1919
method_call2:
2020
class: FooClass
2121
calls:

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ services:
3838
- [setBar, ['@?foo2']]
3939
- [setBar, ['@?foo3']]
4040
- [setBar, ['@?foobaz']]
41-
- [setBar, ['@=service("foo").foo() ~ parameter("foo")']]
41+
- [setBar, ['@=service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")']]
4242

4343
factory_service:
4444
class: Bar

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testLoadServices()
210210
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
211211
$this->assertEquals(array(new Reference('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
212212
$this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
213-
$this->assertEquals(array(array('setBar', array()), array('setBar', array(new Expression('service("foo").foo() ~ parameter("foo")')))), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
213+
$this->assertEquals(array(array('setBar', array()), array('setBar', array(new Expression('service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")')))), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
214214
$this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
215215
$this->assertNull($services['factory_service']->getClass());
216216
$this->assertEquals('getInstance', $services['factory_service']->getFactoryMethod());

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function testLoadServices()
138138
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
139139
$this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
140140
$this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
141-
$this->assertEquals(array(array('setBar', array()), array('setBar', array()), array('setBar', array(new Expression('service("foo").foo() ~ parameter("foo")')))), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
141+
$this->assertEquals(array(array('setBar', array()), array('setBar', array()), array('setBar', array(new Expression('service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")')))), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
142142
$this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
143143
$this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
144144

src/Symfony/Component/ExpressionLanguage/Parser.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public function __construct(array $functions)
7575
/**
7676
* Converts a token stream to a node tree.
7777
*
78+
* The valid names is an array where the values
79+
* are the names that the user can use in an expression.
80+
*
81+
* If the variable name in the compiled PHP code must be
82+
* different, define it as the key.
83+
*
84+
* For instance, ['this' => 'container'] means that the
85+
* variable 'container' can be used in the expression
86+
* but the compiled code will use 'this'.
87+
*
7888
* @param TokenStream $stream A token stream instance
7989
* @param array $names An array of valid names
8090
*
@@ -194,7 +204,13 @@ public function parsePrimaryExpression()
194204
throw new SyntaxError(sprintf('Variable "%s" is not valid', $token->value), $token->cursor);
195205
}
196206

197-
$node = new Node\NameNode($token->value);
207+
// is the name used in the compiled code different
208+
// from the name used in the expression?
209+
if (is_int($name = array_search($token->value, $this->names))) {
210+
$name = $token->value;
211+
}
212+
213+
$node = new Node\NameNode($name);
198214
}
199215
}
200216
break;

src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ public function getParseData()
148148
'foo.bar().foo().baz[3]',
149149
array('foo'),
150150
),
151+
152+
array(
153+
new Node\NameNode('foo'),
154+
'bar',
155+
array('foo' => 'bar'),
156+
),
151157
);
152158
}
153159

0 commit comments

Comments
 (0)
0