Fix expression language in the container when using the "container" variable
This commit is contained in:
parent
924d06adf6
commit
2b2f0df27e
@ -1212,7 +1212,7 @@ EOF;
|
|||||||
|
|
||||||
return $this->getServiceCall((string) $value, $value);
|
return $this->getServiceCall((string) $value, $value);
|
||||||
} elseif ($value instanceof Expression) {
|
} elseif ($value instanceof Expression) {
|
||||||
return $this->getExpressionLanguage()->compile((string) $value, array('container'));
|
return $this->getExpressionLanguage()->compile((string) $value, array('this' => 'container'));
|
||||||
} elseif ($value instanceof Parameter) {
|
} elseif ($value instanceof Parameter) {
|
||||||
return $this->dumpParameter($value);
|
return $this->dumpParameter($value);
|
||||||
} elseif (true === $interpolate && is_string($value)) {
|
} elseif (true === $interpolate && is_string($value)) {
|
||||||
|
@ -52,7 +52,7 @@ $container->
|
|||||||
addMethodCall('setBar', array(new Reference('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)))->
|
addMethodCall('setBar', array(new Reference('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)))->
|
||||||
addMethodCall('setBar', array(new Reference('foo3', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->
|
addMethodCall('setBar', array(new Reference('foo3', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->
|
||||||
addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->
|
addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->
|
||||||
addMethodCall('setBar', array(new Expression('service("foo").foo() ~ parameter("foo")')))
|
addMethodCall('setBar', array(new Expression('service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")')))
|
||||||
;
|
;
|
||||||
$container->
|
$container->
|
||||||
register('factory_service', 'Bar')->
|
register('factory_service', 'Bar')->
|
||||||
|
@ -197,7 +197,7 @@ class ProjectServiceContainer extends Container
|
|||||||
if ($this->has('foobaz')) {
|
if ($this->has('foobaz')) {
|
||||||
$instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE));
|
$instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE));
|
||||||
}
|
}
|
||||||
$instance->setBar(($this->get("foo")->foo() . $this->getParameter("foo")));
|
$instance->setBar(($this->get("foo")->foo() . (($this->hasparameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
|
||||||
|
|
||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,7 @@ class ProjectServiceContainer extends Container
|
|||||||
|
|
||||||
$instance->setBar($this->get('foo'));
|
$instance->setBar($this->get('foo'));
|
||||||
$instance->setBar(NULL);
|
$instance->setBar(NULL);
|
||||||
$instance->setBar(($this->get("foo")->foo() . $this->getParameter("foo")));
|
$instance->setBar(($this->get("foo")->foo() . (($this->hasparameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
|
||||||
|
|
||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
<service id="method_call1" class="FooClass">
|
<service id="method_call1" class="FooClass">
|
||||||
<call method="setBar" />
|
<call method="setBar" />
|
||||||
<call method="setBar">
|
<call method="setBar">
|
||||||
<argument type="expression">service("foo").foo() ~ parameter("foo")</argument>
|
<argument type="expression">service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")</argument>
|
||||||
</call>
|
</call>
|
||||||
</service>
|
</service>
|
||||||
<service id="method_call2" class="FooClass">
|
<service id="method_call2" class="FooClass">
|
||||||
|
@ -54,7 +54,7 @@
|
|||||||
<argument type="service" id="foobaz" on-invalid="ignore"/>
|
<argument type="service" id="foobaz" on-invalid="ignore"/>
|
||||||
</call>
|
</call>
|
||||||
<call method="setBar">
|
<call method="setBar">
|
||||||
<argument type="expression">service("foo").foo() ~ parameter("foo")</argument>
|
<argument type="expression">service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")</argument>
|
||||||
</call>
|
</call>
|
||||||
</service>
|
</service>
|
||||||
<service id="factory_service" class="Bar" factory-method="getInstance" factory-service="foo.baz"/>
|
<service id="factory_service" class="Bar" factory-method="getInstance" factory-service="foo.baz"/>
|
||||||
|
@ -15,7 +15,7 @@ services:
|
|||||||
calls:
|
calls:
|
||||||
- [ setBar, [] ]
|
- [ setBar, [] ]
|
||||||
- [ setBar ]
|
- [ setBar ]
|
||||||
- [ setBar, ['@=service("foo").foo() ~ parameter("foo")'] ]
|
- [ setBar, ['@=service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")'] ]
|
||||||
method_call2:
|
method_call2:
|
||||||
class: FooClass
|
class: FooClass
|
||||||
calls:
|
calls:
|
||||||
|
@ -38,7 +38,7 @@ services:
|
|||||||
- [setBar, ['@?foo2']]
|
- [setBar, ['@?foo2']]
|
||||||
- [setBar, ['@?foo3']]
|
- [setBar, ['@?foo3']]
|
||||||
- [setBar, ['@?foobaz']]
|
- [setBar, ['@?foobaz']]
|
||||||
- [setBar, ['@=service("foo").foo() ~ parameter("foo")']]
|
- [setBar, ['@=service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")']]
|
||||||
|
|
||||||
factory_service:
|
factory_service:
|
||||||
class: Bar
|
class: Bar
|
||||||
|
@ -210,7 +210,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
|
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
|
||||||
$this->assertEquals(array(new Reference('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
|
$this->assertEquals(array(new Reference('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
|
||||||
$this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
|
$this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
|
||||||
$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');
|
$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');
|
||||||
$this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
|
$this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
|
||||||
$this->assertNull($services['factory_service']->getClass());
|
$this->assertNull($services['factory_service']->getClass());
|
||||||
$this->assertEquals('getInstance', $services['factory_service']->getFactoryMethod());
|
$this->assertEquals('getInstance', $services['factory_service']->getFactoryMethod());
|
||||||
|
@ -138,7 +138,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
|
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
|
||||||
$this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
|
$this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
|
||||||
$this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
|
$this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
|
||||||
$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');
|
$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');
|
||||||
$this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
|
$this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
|
||||||
$this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
|
$this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
|
||||||
|
|
||||||
|
@ -75,6 +75,16 @@ class Parser
|
|||||||
/**
|
/**
|
||||||
* Converts a token stream to a node tree.
|
* Converts a token stream to a node tree.
|
||||||
*
|
*
|
||||||
|
* The valid names is an array where the values
|
||||||
|
* are the names that the user can use in an expression.
|
||||||
|
*
|
||||||
|
* If the variable name in the compiled PHP code must be
|
||||||
|
* different, define it as the key.
|
||||||
|
*
|
||||||
|
* For instance, ['this' => 'container'] means that the
|
||||||
|
* variable 'container' can be used in the expression
|
||||||
|
* but the compiled code will use 'this'.
|
||||||
|
*
|
||||||
* @param TokenStream $stream A token stream instance
|
* @param TokenStream $stream A token stream instance
|
||||||
* @param array $names An array of valid names
|
* @param array $names An array of valid names
|
||||||
*
|
*
|
||||||
@ -194,7 +204,13 @@ class Parser
|
|||||||
throw new SyntaxError(sprintf('Variable "%s" is not valid', $token->value), $token->cursor);
|
throw new SyntaxError(sprintf('Variable "%s" is not valid', $token->value), $token->cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
$node = new Node\NameNode($token->value);
|
// is the name used in the compiled code different
|
||||||
|
// from the name used in the expression?
|
||||||
|
if (is_int($name = array_search($token->value, $this->names))) {
|
||||||
|
$name = $token->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$node = new Node\NameNode($name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -148,6 +148,12 @@ class ParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo.bar().foo().baz[3]',
|
'foo.bar().foo().baz[3]',
|
||||||
array('foo'),
|
array('foo'),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
array(
|
||||||
|
new Node\NameNode('foo'),
|
||||||
|
'bar',
|
||||||
|
array('foo' => 'bar'),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user