-
Notifications
You must be signed in to change notification settings - Fork 826
Adapter Internals
Creating an adapter for Flysystem is pretty straightforward. An adapter must implement the League\Flysystem\AdapterInterface
which expands upon the League\Flysystem\ReadInterface
. For some easy fallbacks with streams and visibility defaults you can extend the League\Flysystem\Adapter\AbstractAdapter
.
<?php
namespace Vendor\Namespace;
use League\Flysystem\Adapter\AbstractAdapter;
class MyAdapter extends AbstractAdapter
{
// Implement all the method
}
When an Adapter returns any kind of meta data it should ALWAYS return in the format of an array. By doing so, it's possible to get the most out of every API call. Adapters like S3 will return pretty much everything in a single request. With caching enabled, holding back this information will lead to multiple API calls and by doing so impact performance.
key | description |
---|---|
type |
file or dir
|
path | path to the file or dir |
contents | file contents (string) |
stream | stream (resource) |
visibility |
public or private
|
timestamp | modified time |
When an adapter can not provide the metadata with the key that's required to satisfy the call, false
should be returned.
The Filesystem
class orchestrates all the actions fired on an adapter. This centralises some of the routines and prevents adapters from dealing with certain situations. Because the Filesystem class caches the results, either in memory (per request / instance) or persistent, a great speed improvement is achieved. The following assumptions can be made:
- A file will be absent when a
write
orwriteStream
method is called. - A file will be present when a
update
,updateStream
,delete
method is called.
In many new-age filesystems directories are non-existing or second class citizens. Therefor adapters should implicitly create directories when a write
or writeStream
method is called. This ensures the behaviour is consistent across the different adapters.
The result of the listContents
must always contain the full path of the root specified in the adapter. When adapters don't support directories (linear filesystem / key => value stores) the directory structure can be emulated using the League\Flysystem\Util::emulateDirectories
helper. This will ensure the existence of all parent directories in the result set.
Exceptions from underlying packages should NOT be caught in case of actual unexpected behaviour. Some adapters will throw a message when a file is not found, which is always possible. This exception will be caught as it is expected behaviour.