Problem/Motivation
http://api.drupal.org/api/drupal/includes%21file.inc/function/drupal_rea...
Returns the absolute local filesystem path of a stream URI.
This function was originally written to ease the conversion of 6.x code to use 7.x stream wrappers. However, it assumes that every URI may be resolved to an absolute local filesystem path, and this assumption fails when stream wrappers are used to support remote file storage. Remote stream wrappers may implement the realpath method by always returning FALSE. The use of drupal_realpath() is discouraged, and is slowly being removed from core functions where possible.
Only use this function if you know that the stream wrapper in the URI uses the local file system, and you need to pass an absolute path to a function that is incompatible with stream URIs.
@todo This function is deprecated, and should be removed wherever possible.
Proposed resolution
Replace calls to drupal_realpath() with \Drupal\Core\File\FileSystem::realpath()
Remaining tasks
Find all calls of drupal_realpath() in core
Replace calls with \Drupal\Core\File\FileSystem::realpath()
User interface changes
None
API changes
None
Already deprecated in Change Record:https://www.drupal.org/node/2418133
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #64 | interdiff-1472946-60.txt | 646 bytes | andypost |
| #62 | 1472946_realpath-file_system-62.patch | 70.52 KB | andypost |
| #60 | interdiff-1472946-59.txt | 16.96 KB | andypost |
Comments
Comment #1
gnugetOk, This is my first try.
Comment #2
gnugetI left a few drupal_realpath in some places because i was unsure how access to the service and I wanted to check the majority first.
So I will continue working on this :-)
Comment #3
gnugetNew patch added and I think this is the good one.
David.
Comment #4
gnugetI forgot one file in my last patch, New patch added.
Comment #5
lokapujyaComment #6
lokapujyaComment #7
gnugetVersion updated
Comment #8
gnugetComment #10
gnugetReroll of #8.
Comment #11
avpadernoInstead of using
\Drupal::service('file_system'), would not it be better to use a temporary variable?Comment #12
gnugetHi! Kiamlaluno.
Thanks for your review.
I think is the same, check:
Drupal\Component\DependencyInjection\Container::get()there you will find:Which basically check if the service has been already instanced and if it is the case then the container just re-use the service instance, so even if the patch is using multiple times
\Drupal::service('file_system')it is actually just using the same instance.Comment #13
avpadernoActually, Drupal core uses the following code, in
BaseFieldDefinition::create().If it were calling
\Drupal::service('plugin.manager.field.field_type')twice, it would be calling\Drupal\Component\DependencyInjection\Container::get()twice, to get a value it already has.Comment #14
gnugetTo me using or not the temporary variable is the same, maybe if the service is used more than two times it would make more sense to create a variable to improve the readability of the code.
But if there is other place in the core where the same scenario happened and in that case a temporary variable was used we should keep the same style and do it in the same way.
I'm not near of my computer so I will add the novice tag in case to someone else want to help to change this.
Thanks for your review.
Comment #16
valthebaldTagging for the Friday mentored sprint
Comment #17
mmrares commentedI will try to work on this.
Comment #18
mmrares commentedReroll of #10 applied on 8.3-dev branch.
Comment #19
mmrares commentedThere were some new usages that needed to be replaced. This is based on #18 patch.
Comment #20
mmrares commentedAdded interdiff between #18 and #19 patches.
Comment #21
andread commentedWOrking on the issue.
Comment #22
andread commentedI have reviewed the code, before the patch have been applied, the function appeared 50+ times.
After the patch applied there are none.
The code is reviewed.
Comment #23
valthebaldHere I'd suggest to store \Drupal::service('file_system') result in a variable, and use that variable on the next line.
Same pattern should be applied in all places where we call \Drupal::service() twice
Comment #24
sidharthapThank you @valthebald.
Modified patch as per comment #23
Comment #25
gnugetHi @sidharthap
Can you please provide an interdiff?
Comment #26
sidharthapThank you @gnuget. Here is the diff file.
Comment #27
gnugetThis looks ready to me.
Thanks!
Comment #28
valthebald+1 to RTBC
Comment #30
dineshkumares commentedRerolling patch in #24
Comment #31
amit.drupal commentedI am not able to apply #30 patch .
Please advice.
Comment #32
himanshu5050 commentedPatch in #24 is fine and ready, resubmitting it.
Comment #34
gnugetThe patch at #30 looks great.
I'm changing the status back to Needs Review.
Thanks!
Comment #35
gnugetComment #36
amit.drupal commenteddiff in patch #30 and #32
Comment #37
gnugetIs #32 working? because it seems to it doesn't apply.
I reviewed #30 as the patch ready to be committed.
Comment #38
catchPlease upload the patch to be committed as the last one to this issue. It makes it easier for backports (not relevant here), but also re-tests rely on that.
Comment #39
sidharthap#30 patch works. Resubmitting the patch for review.
Comment #40
valthebaldPatch applies, but with offsets. Can it be rerolled against the latest 8.3.x please?
Comment #41
ada hernandez commented@valthebald I've applied the patch with git apply --check 1472946-39.patch and it don't need reroll.
Comment #42
avpadernoLet's see what the test runner thinks.
Comment #44
gnugetIt seems to #42 has a missing new line:
Uploading a new patch. (I rerolled it just to be sure and it hasn't conflicts as expected)
Thanks for keeping this issue alive :-)
Comment #45
valthebaldYes, it probably was a new line
Comment #46
xjm@catch, @alexpott, and I discussed this issue and had concerns with the use of
$this->containerbeing added in tests. Also see #2066993: Use magic methods to sync container property to \Drupal::getContainer in functional tests.We would be best off to start by using
\Drupalfor the initial patch, to remove the dependency on the global function and present a single scope for the issue. See https://www.drupal.org/core/scope. Then, we can have a followup meta issue to inject the service properly where appropriate. The correct fix in tests can be discussed there as needed. See #2729597: [meta] Replace \Drupal with injected services where appropriate in core.Comment #47
gnugetThanks for your review xjm.
Patch attached removing the inyection and using \Drupal.
Also removing the tag novice.
Comment #49
gnugeteh.. ignore my last patch will upload a new version. :-) I made a mistake while I was doing it.
Comment #50
gnugetok round 2, let's see what the bot says.
Comment #51
valthebald@xjm I thought #2066993: Use magic methods to sync container property to \Drupal::getContainer in functional tests suggests the opposite: to replace `\Drupal::` with `$this->container`? Or the issue summary is not updated?
Comment #52
gnugetIn anycase if we prefer use the fixture container we can use #44 and if we prefer to use the static method can use #50. :-)
In is a matter to decide which is the good one.
Thanks!
Comment #53
20th commentedComment #55
gnugetDid we decide already if we are going to use the fixture container or the static method?
Comment #56
lokapujyaThe way I read it: Use
\Drupalfor now. Let the meta issue replace it after.Comment #57
gnugetStright reroll of #50. (this patch use the static \Drupal as expected).
(Big thanks to Lendude for https://www.dx-experts.nl/converting-patches-use-correct-array-notation, the script did 156 replacements!)
Comment #59
andypostreroll
Comment #60
andypostFix the rest
Comment #62
andypostFix failed test
Comment #64
andypostproper interdiff
Comment #65
gnugetThanks for keeping this alive, to me this is RTBC!
Comment #66
borisson_This does replace all the drupal_realpath calls, after applying the path, the only leftover I can find is the function declaration. There's a couple of places where this could be injected instead of using \Drupal::service(). However that might be too much to do in this issue.
Comment #67
catchIn #1472946-46: Remove usages of deprecated function drupal_realpath() throughout core functions we decided to keep the scope limited to a find and replace here and handle injection in follow-ups, so happy with the patch as is.
Committed/pushed to 8.5.x, thanks!
Comment #68
gnugetI can't see the commit, To which branch was committed?
Thanks!
Comment #70
gnugetI can see it now hehe, thanks!
Comment #71
andypostFiled follow-up #2923511: Use proper DI for FileSystem service