Create custom plugins for Flysystem

Code
<?php

use League\Flysystem\Filesystem;
use 
League\Flysystem\Adapter\Local;
use 
League\Flysystem\Plugin\AbstractPlugin;

// Create your custom Flysystem plugin
class BackupPlugin extends AbstractPlugin
{
    public function 
getMethod()
    {
        return 
'backupFiles';
    }
    
    public function 
handle($dir)
    {
        
$fs $this->filesystem;
        
$backupPath $dir '/backup';
        
        
$fs->assertPresent($dir);
        
        if (!
$fs->has($backupPath)) {
            
$fs->createDir($backupPath);
        }
        
        foreach (
$fs->listContents($dir) as $file) {
            if (
'file' === $file['type']) {
                
// do some safer backup than this in your code!
                
$backupFile $backupPath '/' $file['basename'];
                if (
$fs->has($backupFile)) {
                    
$fs->delete($backupFile);
                }
                
$fs->copy($file['path'], $backupFile);
            }
        }
    }
}

// Create Flysystem filesystem object
$dir __DIR__;
$adapter = new Local($dir);

$fs = new Filesystem($adapter);

echo 
'<b>All files in "files" directory</b>' PHP_EOL;
foreach (
$fs->listContents('files') as $file) {
    echo 
$file['path'] . PHP_EOL;
}

// Add and use custom BackupPlugin
$fs->addPlugin(new BackupPlugin());
$fs->backupFiles('files');

$fs->assertPresent('files/backup');

echo 
'<b>All files in new "files/backup" directory</b>' PHP_EOL;
foreach (
$fs->listContents('files/backup') as $file) {
    echo 
$file['path'] . PHP_EOL;
}
Result
All files in "files" directory
files/file1.txt
files/file2.txt
files/file3.txt
All files in new "files/backup" directory
files/backup/file1.txt
files/backup/file2.txt
files/backup/file3.txt
Used Versions
PHP 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6