-
Notifications
You must be signed in to change notification settings - Fork 106
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
Resolve issue where Symfony2 collection parameters fail to be injected #119
Conversation
@@ -60,10 +60,6 @@ public function resolveArguments(ReflectionClass $classReflection, array $argume | |||
*/ | |||
private function resolveArgument($argument) | |||
{ | |||
if (is_array($argument)) { | |||
return array_map(array($this, 'resolveArgument'), $argument); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not resolve argument recursively for arrays, which look wrong to me, as it does not allow services inside an array anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very strange, I wasn't aware that was in the diff before I committed. I've re-instated that chunk, re-tested my scenarios locally and it's running. Unit tests aren't running on my machine right now so I'm relying on Travis.
and CI complains about your change |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test case as well please?
return preg_replace_callback('/(?<!%)%([^%]+)%(?!%)/', function($matches) use ($container) { | ||
return $container->getParameter($matches[1]); | ||
}, $argument); | ||
$cleanKey = preg_replace('/(?<!%)%([^%]+)%(?!%)/', '$1', $key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could probably use preg_match
instead then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, as explained in my comment below, preg_replace
worked for some cases, except I needed to check if there was a match before I proceeded. I have updated to use preg_match
.
*/ | ||
private function replaceParameters(ContainerInterface $container, $argument) | ||
private function replaceParameters(ContainerInterface $container, $key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you change the behaviour of this method, can you also change its name? Also, why this name change (key
) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason for changing the key, this PR was not in a ready state, still undergoing work as I was waiting for the test results from the CI process.
Has the behaviour of the method really changed? I don't think it has. I don't see the benefit of changing the name here, as it does still replace parameters, nothing new other than it will handle parameter collections.
|
||
return $this->escape($withParameters); | ||
if (!is_string($resolvedParam)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the ternary operator in the return instead of a if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can use the ternary operator, but could you tell me the benefit in this case please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fair, I won't argue about it, just my personal preference by reading the code. You can leave it this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preferences are fine, I have no real preference either way; it was just the smallest issue on this PR at the time. My preference is that it's more readable to have a conditional block rather than a ternary in this case, equally I won't argue the point as I don't have any strong concerns either way.
Now the PR is complete, however, I don't mind changing it if that's the style for this repo.
@stof / @sroze, could not get unit tests running yesterday so I left it to Travis to run the tests for me. They failed as the I've also written and tested a Behat scenario for this, where we have a collection-type parameter registered in Symfony's container. Added a couple of new methods to the |
Done a bit of thinking about how to tackle this nicely, still don't think it's quite refined enough. The unit tests are now passing, and I've added in new ones to cover cases where container parameters are collections. I made the decision to first check if the given argument is one entire substitution, and in that case, go directly to the container and request the parameter. That handles the case where an argument wants a collection parameter. If that is not the case, then similar logic as before occurs, exception and The builds, except HHVM ( |
@@ -23,12 +23,25 @@ function it_resolves_parameters_starting_and_ending_with_percentage_sign_if_they | |||
ContainerInterface $container | |||
) { | |||
$container->getParameter('parameter')->willReturn('param_value'); | |||
$container->hasParameter('parameter')->wilLReturn(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice spot. Resolved.
That's a good one, I'm happy with this. Regarding HHVM, as it's not supported anymore in Symfony 4, you can remove it from the |
@sroze thanks for confirming; I'll remove it in a separate PR as it's not a direct concern for this PR. |
@stof are you happy with these changes as well? |
…s (array arguments). Includes tests.
Rebased, just to make the commit log cleaner. @stof do you mind reviewing please? |
Travis is blocked on PHP 5.3; will remove the support anyway. Thank you @glennunipro! |
This allows collection parameters defined in Symfony 2 to be injected. Fixes #117.
Currently, they fail to do so as there is a constraint that
preg_replace_callback
must return a string, but when resolving parameters from Symfony 2's container, you may either get back astring
,constant
, orcollection
(see here).I've swapped to using
preg_match
which will return a string, and I have changed the method signature ofreplaceParameters
so that it returns the container value of the parameter. Then, if it is not a string, we return it to the caller (to avoid another issue passing it to@escape
), otherwise theescape
method is still called on it, and then returned.