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
Change editor mapping to support multiple changes to the same substring #176
Conversation
@@ -20,7 +20,7 @@ class Helpers | |||
*/ | |||
public static function editorLink($file, $line = NULL) | |||
{ | |||
$file = strtr($origFile = $file, Debugger::$editorMapping); | |||
$file = self::editorMapping($origFile = $file); |
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.
What about
$file = str_replace(array_keys(Debugger::$editorMapping), array_values(Debugger::$editorMapping), $file));
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.
Yeah - that's what I was originally planning on doing, but I thought I should follow the approach that @dg took yesterday when he implemented substr_replace.
I am certainly happy to go with the array_keys / array_values approach though - it's certainly less code.
Would you like me to modify the PR?
Whats wrong with strtr()? |
strtr won't replace the same substring again once it has been modified, so subsequent rules in the array are ignored. My fault for not thinking through the possibility of wanting to make multiple adjustments to the same parts of a path. |
I fear that multiple substitutions in one string can cause problems elsewhere. Is it really necessary? |
Well in my use case it is definitely necessary. As I mentioned I absolutely have to modify the path from the compiled version to the source version. The editor link is no use at all without this. Then in addition I want to be able to modify the links from the production server path to the local dev path so that if an error shows up on a production site, there is an easy link to the same file on your local dev machine (or to a mapped path to the production server if you like to work that way). What problems do you foresee ? |
7f3fd63
to
bbc898e
Compare
I need to know exactly what do you want, more specifically I need to have a test, because otherwise I can not guarantee that in the future it will behave as you need. |
@dg - it's pretty simple - I just need the ability to make more than one replacement on the path. This PR takes care of that and is working fine here. @JanTvrdik's suggestion of using array_keys() and array_values() is cleaner in my mind, but I wanted to match your coding style from the previous commit you made, which is why I went with the loop approach. I can try to put a test together for this, but I don't really have any time this weekend and am on vacation for the next two weeks. |
The test would be great. |
Ok, I haven't tested this test, but based on my reading about Assert, this should do the trick:
This is based on the new
is changed to:
|
PS - maybe it's confusing having the Debugger::$editorMapping variable and the Helpers::$editorMapping() method having the same name? |
Hi @dg - any thoughts on this change and the test? Thanks! |
I still don't know what you exactly want. What are your source paths, destination paths and your mapping. Create (failng) test file and it becomes clear. |
@dg - sorry I don't know what else to tell you - in the test above (#176 (comment)) you can see the exact mapping and you can compare I don't think I really know how to create a failing test for this - sorry I am new to Nette and am only familiar with Tracy, not the rest of the framework. If the test I wrote works, then I think everything is fine. I am using the code from this PR and it's working perfectly. As I mentioned, the problem with my initially proposed strtr() won't allow multiple changes to the same substring. This new approach of simple str_replace - either via foreach, or array_keys / array_values allows the user to define as many replacements as they need. Surely if they fail to match, then it's their fault, not the fault of this mapping method? Sorry if I am still not understanding what you need. |
Perhaps we do not understand :-) I'm not asking you to learn Tester, I am just trying to understand your use case. This URL
can be changed to this
with this mapping:
I simply can't understand why you want to split it into more replacements. |
Because it think it should be flexibly enough to handle any scenario a user may want to throw at it. Also, in the case of my example, I don't know if the |
So what about this?
IMHO it is perfectly readable, understandable and expectable. I can improve |
The problem with that version is that it doesn't work on your local dev setup - it only works to make links from remote production server map to local. It doesn't handle mapping of local dev compiled directory to the local dev src file. |
And this?
|
I expect that |
Multiple replacements may lead to unwanted results. Yes, it is unlikely that it will happen, if you have long path names (i.e long keys in $editorMapping), but these things may happen and there is no way how to solve it (ie. how to say „make only one replacement and finish“). Maybe I am afraid unnecessarily, but still I find it better to avoid it by design and use more explicit mappings. |
Ok, fair enough - better to be safe than sorry I guess. By automatically building up the array of replacements modified from your suggestion here (#176 (comment)), I have something that now works with Thanks for spending the time to work through this. |
Sorry, I didn’t think about the scenario of wanting make multiple changes to a substring. Of course strtr fails at this. This version allows these sorts of changes. In my scenario I can now change from the compiled version to the source version, as well as map production server URLs to the local dev source.
Hopefully you are happy with these changes.