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

Skip to content

[Filesystem] Add the readFile() method #54173

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

Merged
merged 1 commit into from
Mar 17, 2024
Merged

Conversation

derrabus
Copy link
Member

@derrabus derrabus commented Mar 6, 2024

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #47215
License MIT

Our Filesystem class is a tool for leveraging PHP's native filesystem functions is a sane way.

For example, it provides a method dumpFile() which attempts to write the contents of a string to a file and throws if that attempt fails. However, it lacks a method to read the content of a file. The native function for this task, file_get_contents() comes with a couple of traps that I don't want to think about or step into when working with a file system:

  • If reading a file fails, file_get_contents() returns false and triggers an E_WARNING. I need to check the return value against false and if I want feedback on the failure, I need to work with custom error handlers. Yuck.
  • Passing the path to a directory to file_get_contents() is almost certainly a mistake that I would want to get an exception for. However, file_get_contents() will return an empty string in that case. Fun times.

With this PR, I'm proposing to add a readFile() method that either returns the content of a given file or throws an IOException. Furthermore, I'm dogfooding this method in components that already depend on the Filesystem component.

@carsonbot carsonbot added this to the 7.1 milestone Mar 6, 2024
@derrabus derrabus force-pushed the feature/read-file branch 3 times, most recently from 5d32ad6 to 6396dd1 Compare March 6, 2024 11:52
@derrabus derrabus force-pushed the feature/read-file branch from 0019010 to 1a13e3e Compare March 8, 2024 07:06
@derrabus derrabus force-pushed the feature/read-file branch from 1a13e3e to 632ff62 Compare March 15, 2024 13:33
@fabpot
Copy link
Member

fabpot commented Mar 17, 2024

Thank you @derrabus.

@fabpot fabpot merged commit d4cab3a into symfony:7.1 Mar 17, 2024
@derrabus derrabus deleted the feature/read-file branch March 17, 2024 09:54
@fabpot fabpot mentioned this pull request May 2, 2024
fabpot added a commit that referenced this pull request Aug 19, 2024
This PR was squashed before being merged into the 7.2 branch.

Discussion
----------

[AssetMapper] Leverage Filesystem

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #...
| License       | MIT

Continue the work started in #54173 & #54724 to replace php file functions  (`mkdir`, `file_get_contents` , `file_put_contents`, `unlink`) by Filesystem methods

Commits
-------

a1bb5a2 [AssetMapper] Leverage Filesystem
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.

RFC: FileSystem::getContent
6 participants