After successfully configured the adapters, create a filesystem and inject an adapter of your choice.
oneup_flysystem:
adapter: ~
filesystems:
acme:
adapter: my_adapter
alias: ~
mount: ~
visibility: ~
directory_visibility: ~
This will expose a new service for you to use.
$filesystem = $container->get('oneup_flysystem.acme_filesystem');
The naming scheme follows a simple rule: oneup_flysystem.%s_filesystem
whereas %s
is the name (config key) of your filesystem.
The $filesystem
variable is of the type \League\Flysystem\Filesystem
.
Please refer to the General Usage section in the official documentation for details.
You can alias your filesystem by providing an alias
key.
oneup_flysystem:
adapter: ~
filesystems:
acme:
adapter: my_adapter
alias: acme_filesystem
Afterwards, the filesystem service is aliased with the provided value and can be retrieved like this:
$filesystem = $container->get('acme_filesystem');
If you use Dependency Injection instead of the container as service locator, you can inject your filesystems in your services:
services:
app.my_service:
class: App\MyService
arguments:
- '@oneup_flysystem.acme_filesystem' # Inject the resolved service name, or the alias (see previous section)
Dependency Injection is considered a good practice since you do not need the container, and you can then refer to it in your class like this:
use League\Flysystem\FilesystemOperator;
class MyService
{
public function __construct(FilesystemOperator $acmeFilesystem)
{
$this->filesystem = $acmeFilesystem;
}
}
💡 Pro tip: when using Symfony 4.2, you can completely omit the service arguments definition in your config files,
and instead you can automatically inject your filesystems by providing the exact same type-hint as above, and
replace acme
with the name of your filesystem. Thanks to the ContainerBuilder::registerAliasForArgument()
method!
If you provided a mount prefix, you can also access the filesystem through the MountManager.
oneup_flysystem:
adapters:
myadapter:
local:
location: "%kernel.root_dir%/cache"
filesystems:
myfilesystem:
adapter: myadapter
mount: prefix
$filesystem = $container->get('oneup_flysystem.mount_manager')->getFilesystem('prefix');
Details on the usage of the MountManager can be found in the Flysystem documentation.
In version 1.x of Flysystem you could provide a cache per each adapter. The cached adapter was not ported to V2 of Flysystem.
If you want to use cached adapters, give a try to lustmored/flysystem-v2-simple-cache-adapter.