Updated on May 9, 2017
I’m happy to announce the immediate availability of FileFetcher 4.0.0.
FileFecther is a small PHP library that provides an OO way to retrieve the contents of files.
What’s OO about such an interface? You can inject an implementation of it into a class, thus avoiding that the class knows about the details of the implementation, and being able to choose which implementation you provide. Calling
file_get_contents does not allow changing implementation as it is a procedural/static call making use of global state.
Library number 8234803417 that does this exact thing? Probably not. The philosophy behind this library is to provide a very basic interface (
FileFetcher) that while insufficient for plenty of use cases, is ideal for a great many, in particular replacing procedural
file_get_contents calls. The provided implementations are to facilitate testing and common generic tasks around the actual file fetching. You are encouraged to create your own core file fetching implementation in your codebase, presumably an adapter to a library that focuses on this task such as Guzzle.
So what is in it then? The library provides two trivial implementations of the
FileFetcher interface at its heart:
SimpleFileFetcher: Adapter around
InMemoryFileFetcher: Adapter around an array provided to its constructor (construct with  for a “throwing fetcher”)
It also provides a number of generic decorators:
ErrorLoggingFileFetcher: Logs errors via the PSR-3 LoggerInterface
CachingFileFetcher: Adds caching capabilities using the SimpleCache library
SpyingFileFetcher: A spy (test double)
Version 4.0.0 brings PHP7 features (scalar type hints \o/) and adds a few extra handy implementations. You can add the library to your composer.json (
jeroen/file-fetcher) or look at the documentation on GitHub. You can also read about its inception in 2013.