Skip to content

Using expressions with container throws exception #11995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Stelian opened this issue Sep 23, 2014 · 8 comments
Closed

Using expressions with container throws exception #11995

Stelian opened this issue Sep 23, 2014 · 8 comments

Comments

@Stelian
Copy link
Contributor

Stelian commented Sep 23, 2014

arguments: ["@=container.hasParameter('param') ? parameter('param') : 'null'"]

gives an Undefined variable: container in the appDevDebugProjectContainer.php. The generated code for that error is

 return $this->services['service_name'] = new \Name\Space\Class((($container->hasParameter("param")) ? ($this->getParameter("param")) : ("null")));
@Stelian
Copy link
Contributor Author

Stelian commented Sep 23, 2014

As a workaround, I went without the check and just parameter('param'), doing the check somewhere else.

@hacfi
Copy link
Contributor

hacfi commented Sep 23, 2014

@Stelian You could do this in the Bundle (Dependency Injection) Extension of your bundle.

@Stelian
Copy link
Contributor Author

Stelian commented Sep 23, 2014

@hacfi, totally true, can even be solved in the a compiler pass, but it's pretty much a 1 to 1 use case as described in the docs here: http://symfony.com/doc/current/book/service_container.html#using-the-expression-language.

This is pretty much the reason for opening the issue.

@hacfi
Copy link
Contributor

hacfi commented Sep 23, 2014

@Stelian Sorry..wasn’t aware of that as I haven’t spent much time using the Expression Language.

@jakzal
Copy link
Contributor

jakzal commented Sep 24, 2014

Actually, I'm not sure if this ever worked. Looks like it has been broken before the ExpressionLanguage component was merged (when this was replaced with container in 984bd38).

@linaori
Copy link
Contributor

linaori commented Sep 25, 2014

What you can do in a PR for: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/ExpressionLanguage.php

// add this code in the registerFunctions()
$this->register('has_parameter', function ($arg) {
    return sprintf('$this->hasParameter(%s)', $arg);
}, function (array $variables, $value) {
    return $variables['container']->hasParameter($value);
});

I'm not sure if this would be desired, but it's an option. If you make a PR with this code, you should update the docs too.

This would allow:

has_parameter('param') ? parameter('param') : 'null'

@fabpot
Copy link
Member

fabpot commented Sep 25, 2014

#12030 should fix the problem. Can you confirm?

@Stelian
Copy link
Contributor Author

Stelian commented Sep 25, 2014

@fabpot, yes it works with your commit, thank you.

fabpot added a commit that referenced this issue Sep 25, 2014
…ontainer" variable (fabpot)

This PR was squashed before being merged into the 2.4 branch (closes #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 | #11995
| License       | MIT
| Doc PR        | n/a

See #11995 for the description of the problem.

Commits
-------

2b2f0df Fix expression language in the container when using the "container" variable
@fabpot fabpot closed this as completed Sep 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants