Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Fix a limitation of the PhpDumper #18167

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
wants to merge 5 commits into from

Conversation

GuilhemN
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #17801
License MIT
Doc PR

The PhpDumper cannot currently dump several services' id containing characters unsupported by php.
This PR tries to solve this issue by removing the unsupported characters and by sufixing their camelized association to avoid conflicts.

@javiereguiluz
Copy link
Member

I'm afraid that @xabbuh is already trying to fix this issue in #18028. Maybe you can join forces?

@GuilhemN
Copy link
Contributor Author

@javiereguiluz yeah I know but as @stof said #18028 (comment), @xabbuh can't use a black list without this :-/

$name = Container::camelize($id);
$finalName = $name = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', $name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename $finalName to $uniqueName

@GuilhemN
Copy link
Contributor Author

Thank you @javiereguiluz, I fixed your comments

@@ -58,6 +58,7 @@ class PhpDumper extends Dumper
private $targetDirRegex;
private $targetDirMaxMatches;
private $docStar;
private $existingNames = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for bothering you again about this ... but after looking at the code with more calm, I think the "right" name for this variable would be $serviceNames instead of $existingNames. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem ;-)
Would you prefer $serviceIdToMethodNameMap as proposed by @xabbuh ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to describe the purpose of the variable with absolute precision, so it's probably a good idea to make that change ... and, after that change, it's probably a good idea to change $uniqueName to $methodName or $serviceMethodName too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, changed

@xabbuh
Copy link
Member

xabbuh commented Mar 15, 2016

Thanks for taking care of this @Ener-Getick! I like the approach in general and just left some suggestions to improve it a bit.

@@ -1349,13 +1351,22 @@ private function getServiceCall($id, Reference $reference = null)
*/
private function camelize($id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this private method should be renamed though, as it is not about camelizing anymore, but about generating the method name

@stof
Copy link
Member

stof commented Mar 16, 2016

there is a another nice side-effect of this PR: Container::camelize can generate the same output for several service ids (due to method name being case insensitive), so this could allow fixing this case too

}
$this->serviceIdToMethodNameMap[$id] = $methodName;
$this->usedMethodNames[$methodName] = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method names must be considered as case insensitive in this array, as PHP treats them this way. Otherwise you will still generated conflicting names

@GuilhemN
Copy link
Contributor Author

Status: Needs Review

@GuilhemN
Copy link
Contributor Author

I updated the ProxyDumper as the method name was hardcoded.

@@ -63,7 +63,7 @@ public function isProxyCandidate(Definition $definition)
/**
* {@inheritdoc}
*/
public function getProxyFactoryCode(Definition $definition, $id)
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an argument here is a BC break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if it is optional ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was allowed... Do you have an idea how to make it work otherwise ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As child classes are allowed to add additional arguments (see https://3v4l.org/CmOt2) you could use func_num_args() and func_get_arg() to create a forward compatible layer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well... you could do play with func_get_args and func_num_args : https://github.com/symfony/validator/blob/2.8/Validator/RecursiveValidator.php#L118 it isn't pretty though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a cleaner solution would be to add a new method getMethodFor($id) in the compiled container. WDYT ?

Edit: after all, that's much more complicated so I did it with your method thank you @xabbuh @Taluu

@fabpot
Copy link
Member

fabpot commented Mar 31, 2016

👍

@@ -71,7 +71,12 @@ public function getProxyFactoryCode(Definition $definition, $id)
$instantiation .= " \$this->services['$id'] =";
}

$methodName = 'get'.Container::camelize($id).'Service';
if (func_num_args() >= 3) {
$methodName = func_get_arg(2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be written by adding $methodName = null in the signature of the method, and checking for null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, actually no. It would break BC for people extending the class. so forget it.

@stof
Copy link
Member

stof commented Mar 31, 2016

👍

Status: reviewed

@xabbuh
Copy link
Member

xabbuh commented Mar 31, 2016

👍

@GuilhemN
Copy link
Contributor Author

@trousers this issues aren't related to this PR as there were already present.

@fabpot
Copy link
Member

fabpot commented Apr 1, 2016

Thank you @Ener-Getick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants