Updated: Comment #0
Problem/Motivation
Lots of procedural code in file.inc including drupal_chmod can't be injected into services/ OO code.
- Increasing test coverage
- and it could unblock #2304461: KernelTestBaseTNG™ which would make tests run fast, and allow us to write phpunit integration tests (phptests that are not unit tests)
- exposes and fixes inconsistencies and brittleness in several simpletests as well as the installer
Beta phase evaluation
Issue category | Task because it exposes and fixes bugs that could be created, but are not yet existing. |
---|---|
Issue priority | Major because not a functional bug, but important by community consensous to prevent future bugs. |
Disruption | None. BC layer left in and slated for removal in Drupal 9. |
Could be allowed since major and impact is greater than disruption.
Proposed resolution
Create Drupal\Core\File\FileSystem class, make it the 'file_system' service, and move much of file.inc into methods on it. Leave file.inc methods as wrappers to the class and mark as deprecated.
Remaining tasks
- (done) Write the patch
- (done) address feedback
- Reviews
User interface changes
None
API changes
FILE_CHMOD_DIRECTORY and FILE_CHMOD_FILE have been deprecated in favour of \Drupal\Core\File\FileSystem::CHMOD_DIRECTORY and \Drupal\Core\File\FileSystem::CHMOD_FILE
Related Issues
Comment | File | Size | Author |
---|---|---|---|
#65 | 2050759-file-65.patch | 64.68 KB | tim.plunkett |
#65 | interdiff.txt | 5.14 KB | tim.plunkett |
#60 | 2050759-file-60.patch | 64.3 KB | tim.plunkett |
#60 | interdiff.txt | 624 bytes | tim.plunkett |
#58 | interdiff.txt | 38.21 KB | tim.plunkett |
Comments
Comment #1
tim.plunkett.
Comment #2
tim.plunkettThis would be a lot better if #2028109: Convert hook_stream_wrappers() to tagged services. was in. Here's a first pass.
Comment #3
larowlan*blink* and you miss it
Comment #4
larowlanha ha
I assume we can't register this as a service and inject because we want to keep it static? Should we be using \Drupal::config() instead?
Can we add a todo for https://drupal.org/node/2028109
I'm assuming these are moved in the plugin conversion?
Comment #5
tim.plunkettI'll fix the @todo :)
Drupal::config() is our best bet.
Yes, the rest of that will be plugins once the other issue goes in.
Comment #6
tim.plunkettNot all of the uri/stream functions are going to be straight plugins, they themselves will also need to be wrappers.
I'd like to just postpone this on #2028109: Convert hook_stream_wrappers() to tagged services. for right now.
Comment #7
tim.plunkettJust tracking some work, this is what it will look like once the other issue is in.
Comment #8
sunI'm very concerned about \Utility turning into a dumping-ground of arbitrary things.
In my mind, \Utility should not contain any classes that depend on other (injected-or-not) services.
So e.g. the utility/helper classes for arrays, array-sorting, string escaping, etc are all fine, because they are self-contained (having only PHP core as a dependency).
But the procedural functions being converted here clearly do not follow that pattern — since they rely and depend on configuration, plugins, and other things, they are part of an own "service", which needs proper dependency injection.
Can we move this code from \Utility into \File, register it as a service and inject the dependencies?
In the end (and hopefully before release), we should ideally consolidate all the file related things into that namespace:
Drupal\Core\File\StreamWrapper\...
Drupal\Core\File\Transfer\...
Drupal\Core\File\FileHandler (or similar; the stuff here)
Comment #9
sunComment #10
tim.plunkettI don't think we should bother waiting around for #2028109: Convert hook_stream_wrappers() to tagged services.
Comment #11
sunComment #12
larowlanWondering if it makes sense to rewrite file.inc on top of https://github.com/thephpleague/flysystem?
Save reinventing the wheel.
Comment #13
mikeryanAnyone working on this? file_unmanaged_copy() right now is unsuitable for use in migration (doesn't work for remote files - see #2244555: Use copy() directly instead of file_unmanaged_copy()), not sure if we should refactor it on its own or as part of this issue.
Comment #14
tim.plunkettI'm working on this.
Comment #15
larowlanThank you
Comment #16
tim.plunkettHere's a fresh start.
Comment #17
fietserwinad 1)
ad 2)
Comment #18
tim.plunkett1) I left the stream wrapper functions that actual need the info/instances of stream wrappers. The rest was moved.
2) I will probably go over this for violations caused by the patch, like 80 chars, but not fixing the rest of it here.
Comment #19
ParisLiakos CreditAttribution: ParisLiakos commentedeither this or #2043773: Replace php function wrappers in file.inc with a Drupal\Core\File\FileSystem class should be closed..those two issues are very similar
i really think Drupal/Core/File is a better namespace for this
Comment #20
tim.plunkettMarked the other issue as a dupe.
Per #8, made this a service. Was able to add tests for chmod and unlink, thanks to vfsStream.
Comment #22
tim.plunkettForgot to test locally before posting. Missed a service.
Comment #25
fietserwinFWIW: adding
to function install_begin_request() does make the error go away.
So it is an order problem during installation: the file_system service is not yet there, but already used. Makes me think, can I define a logger that logs to private:\\mylogfile.log and would that give problems here or in #2028109: Convert hook_stream_wrappers() to tagged services.?
Comment #26
tim.plunkettServices used during the installer need to be registered, so your code is correct.
Comment #28
tim.plunkettWe can't set up the config directories (using the file system) until the container is built.
Comment #30
tim.plunkettSeems like TestBase is trying to clean up sites/simpletest/* after the container is reset?
Reroll.
Comment #32
tim.plunkettThis is much easier to debug with #2363341: Throw exception in Drupal::service() and friends, if container not initialized yet. applied.
Comment #36
fietserwinReroll. interdiff is ot a real interdiff, but a diff of the changes that failed to apply (and that I applied manually).
Comment #37
fietserwinComment #39
fietserwinThe logger (factory) has a lot of (new) dependencies, but with this change I could reinstall my local test env (via the UI). Let's see if the tests pass.
Comment #41
fietserwinIt is a hack, but just to see if all failures are a result of the same problem.
Problem: file_uri_scheme() get's called during a web based test setup. Stack trace (not complete):
- WebTestBase::setUp()
- WebTestBase::installParameters()
- drupal_get_database_types()
- file_scan_directory()
- file_stream_wrapper_uri_normalize()
- file_uri_scheme
- \Drupal::service('file_system')
- static::$container->get($id);
And this only to find out if there is more than 1 database driver. I would think that even if there's 1, it won't hurt to select it on the install form (and if it is not there, that web test form fill in handling takes care of fields that are not found on the form).
Comment #42
fietserwinComment #44
fietserwinI give up. It looks like the remaining errors are Drupal test setup specific and I do not have enough knowledge about Drupal testing to solve these remaining errors in an easy way. Someone else who is willing to give it a try?
Remaining failures:
Failure in core\modules\system\src\Tests\Bootstrap\GetFilenameUnitTest.php:
Failure in core\modules\system\src\Tests\Routing\RouteProviderTest.php:
- TestBase::restoreEnvironment()
- file_unmanaged_delete_recursive()
- drupal_rmdir()
TestBase::prepareEnvironment() unsets the container:
No idea why, but this probably provokes the errors.
Failure in core\modules\system\src\Tests\DrupalKernel\DrupalKernelTest.php:
- DrupalKernelTest::setUp()
- KernelTestBase::prepareConfigDirectories()
- install_ensure_config_directory()
- file_prepare_directory()
- file_uri_scheme()
I guess that adapting or overriding containerBuild can solve the problems here.
7 Failures in core\modules\simpletest\src\Tests\BrokenSetUpTest.php:
Not sure why, but I guess a similar cause as the first 2.
Comment #45
tim.plunkettIf you do not call parent::setUp(), your test will fail.
There are two problems with that:
BrokenSetUpTest does not call setUp on purpose to show that things are broken.
DrupalKernelTest has this comment:
Comment #47
tim.plunkettJust to see what happens :D
Comment #49
tim.plunkettComment #51
jibrannice.
Comment #52
tim.plunkettWow, BrokenSetUpTest was hard to debug.
Hopefully moving the restoration of the container up a few lines won't break anything.
Comment #53
tim.plunkettOkay, so as to not block this on #2363341: Throw exception in Drupal::service() and friends, if container not initialized yet., here's a version of this with those changes stripped out.
Comment #54
jibranThank for removing 2363341 from this patch. We need BE in IS other then that it is RTBC.
Comment #55
dawehnerFile vs. FileSystem ... did you considered adding an interface ... so would you ever want to replace it?
meh typehinting.
All of those are deprecated
Why do we want to wrap it in the first place?
Comment #56
YesCT CreditAttribution: YesCT commentedadding beta evaluation
Comment #57
YesCT CreditAttribution: YesCT commentedhtml
Comment #58
tim.plunkettAddressed the feedback in #55, and updated the issue summary
Comment #60
tim.plunkettExposing more issues, this time in the installer: stream_wrapper_manager is registered as needing the module_handler, which doesn't exist in the early installer, but it actually isn't needed at all.
Comment #61
dawehnerAwesome!
Comment #62
alexpottLets' deprecate the constants rather than remove them. Let's say they will be removed in D9.
We should only be saying that new deprecations will be removed in D9. All the deprecations need to be changed.
Comment #63
tim.plunkettIf we're not going to make any API changes or deprecate anything before D9, why the need for the CR?
I'll fix the other stuff tomorrow, and probably just write the CR as well...
Comment #64
alexpottI CRs are valid when we deprecate a load of functions that have been around forever - so people know to use the new functions.
Comment #65
tim.plunkettOpened https://www.drupal.org/node/2418133
Comment #66
YesCT CreditAttribution: YesCT commentedlooks like you got them all.
no unintended changes either.
change record made.
all feedback from @alexpott addressed.
super.
Comment #67
alexpottCommitted fc63f5e and pushed to 8.0.x. Thanks!
I've updated the beta evaluation and issue summary to reflect the latest changes.