Updated on July 31, 2018
I’m happy to announce the immediate availability of FileFetcher 4.0.0.
FileFetcher 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 several trivial implementations of the
FileFetcher interface at its heart:
SimpleFileFetcher: Adapter around
InMemoryFileFetcher: Adapter around an array provided to its constructor
ThrowingFileFetcher: Throws a
FileFetchingExceptionfor all calls (added after 4.0)
NullFileFetcher: Returns an empty string for all calls (added after 4.0)
StubFileFetcher: Returns a stub value for all calls (added after 4.0)
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 (
<a href="https://packagist.org/packages/jeroen/file-fetcher">jeroen/file-fetcher</a>) or look at the documentation on GitHub. You can also read about its inception in 2013.