Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@akadlec @miloshavlicek
115 lines (84 sloc) 3.26 KB

Quickstart

If you need to handle file storing and reading on various file systems, checkout Flysystem from The PHP League. This extension is implementing Flysystem into your application based on Nette Framework

Installation

The best way to install ipub/flysystem is using Composer:

$ composer require ipub/flysystem

After that you have to register extension in config.neon.

extensions:
	flysystem: IPub\Flysystem\DI\FlysystemExtension

Creating adapters

As a first step is to define your file adapters. Adapters are used for manipulation with files.

flysystem:
    adapters:
        my_local_adapter:
            type: local
            directory: %baseDir%/cache

        other_adapter:
            type: local
            directory: %baseDir%/other

There are a bunch of adapters for you to use:

Create and use your filesystems

Next step for using Flysystem is to create filesystem service, that will handle all your read, write, etc. commands, but at first you have to define adapter.

flysystem:
    adapters:
        my_local_adapter:
            type: local
            directory: %baseDir%/cache

        other_adapter:
            type: local
            directory: %baseDir%/other

    filesystems:
        my_filesystem:
            adapter: my_local_adapter

This definition will create new service for you to use:

$filesystem = $container->getService('flysystem.filesystem.my_filesystem');

The naming scheme follows a simple rule: flysystem.filesystem.%s whereas %s is the name 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.

Using mount manager

All filesystems are automatically mounted to the mount manager service for easy usage. Now, when your filesystem is created, you can just inject mount manager where you need to operate with files:

class FileUploadPresenter extends BasePresenter
{
    /**
     * @var \League\Flysystem\MountManager
     */
    private $mountManager;

    /**
     * @param \League\Flysystem\MountManager $mountManager
     */
    public function __construct(\League\Flysystem\MountManager $mountManager)
    {
        $this->mountManager = $mountManager;
    }
    
    public function handleUpload()
    {
        //...do some stuff
        
        $this->mountManager->write('my_filesystem://path/where/to/save/filename.ext', $fileContent);
    }
}

Details on the usage of the MountManager can be found in the Flysystem documentation.

What next?

You can’t perform that action at this time.