PHP code example of wshafer / psr11-flysystem

1. Go to this page and download the library: Download wshafer/psr11-flysystem library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wshafer / psr11-flysystem example snippets




// Get the FlySystem FileSystem
$fileSystem = $container->get('myFileSystemService');

// Write to file
$fileSystem->put('test.txt', 'this is test');

// Create Container
$container = new \Xtreamwayz\Pimple\Container([
    // FlySystem using the default keys.
    'fileSystem' => new \WShafer\PSR11FlySystem\FlySystemFactory(),
    
    // FlySystem using a different filesystem configuration
    'other' => function($c) {
        return \WShafer\PSR11FlySystem\FlySystemFactory::other($c);
    },
    
    // Config
    'config' => [
        'flysystem' => [
            'adaptors' => [
                // At the bare minimum you must        ],
            ],
            
            'fileSystems' => [
                'other' => [
                    'adaptor' => 'someOtherAdaptor'
                ],
            ],
        ],
    ]
]);

/** @var \League\Flysystem\FilesystemInterface $fileSystem */
$fileSystem = $container->get('other');
$fileSystem->put('test1.txt', 'this is a test');
print $fileSystem->get('test1.txt')->read();

// Create the container and define the services you'd like to use
$container = new \Zend\ServiceManager\ServiceManager([
    'factories' => [
        // FlySystem using the default keys.
        'fileSystem' => \WShafer\PSR11FlySystem\FlySystemFactory::class,
        
        // FlySystem using a different filesystem configuration
        'other' => [\WShafer\PSR11FlySystem\FlySystemFactory::class, 'other'],
    ],
]);

// Config
$container->setService('config', [
    'flysystem' => [
        'adaptors' => [
            // At the bare minimum you must          ],
        ],
    ],
]);

/** @var \League\Flysystem\FilesystemInterface $fileSystem */
$fileSystem = $container->get('other');
$fileSystem->put('test1.txt', 'this is a test');
print $fileSystem->get('test1.txt')->read();


return [
    'dependencies' => [
        'factories' => [
            // FlySystem using the default keys.
            'fileSystem' => \WShafer\PSR11FlySystem\FlySystemFactory::class,
            
            // FlySystem using a different filesystem configuration
            'other' => [\WShafer\PSR11FlySystem\FlySystemFactory::class, 'other'],
        ],
    ],
    
    'flysystem' => [
        'adaptors' => [
            // At the bare minimum you must leSystems' => [
            'other' => [
                'adaptor' => 'someOtherAdaptor'
            ],
        ],
    ],
];


return [
    'service_manager' => [
        'factories' => [
            // FlySystem using the default keys.
            'fileSystem' => \WShafer\PSR11FlySystem\FlySystemFactory::class,
            
            // FlySystem using a different filesystem configuration
            'other' => [\WShafer\PSR11FlySystem\FlySystemFactory::class, 'other'],
        ],
    ],
    
    'flysystem' => [
        'adaptors' => [
            // At the bare minimum you must ystems' => [
            'other' => [
                'adaptor' => 'someOtherAdaptor'
            ],
        ],
    ],
];



return [
    // ... Previously registered modules here
    'WShafer\\PSR11FlySystem',
];



return [
    'modules' => [
        // ... Previously registered modules here
        'WShafer\\PSR11FlySystem',
    ]
];



namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $fileSystem = $this->container->get('fileSystem');
        $fileSystem->write('default.txt', 'Hi there');
        
        $fileSystem = $this->container->get('other');
        $fileSystem->write('other.txt', 'Hi there');
    }
}


use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

       // At the bare minimum you must                 ],
                ],

                // Some other Adaptor.  Keys are the names for each adaptor
                'someOtherAdaptor' => [
                    'type' => 'local',
                    'options' => [
                        'root' => '/tmp/slim'
                    ],
                ],
            ],

            'fileSystems' => [
                'other' => [
                    'adaptor' => 'someOtherAdaptor'
                ],
            ],
        ],
    ],
];

$app = new \Slim\App($config);

// Wire up the factory
$container = $app->getContainer();

// FlySystem using the default keys.
$container['fileSystem'] = new \WShafer\PSR11FlySystem\FlySystemFactory();

// FlySystem using a different filesystem configuration
$container['other'] = function ($c) {
    return \WShafer\PSR11FlySystem\FlySystemFactory::other($c);
};


// Example usage
$app->get('/example', function (Request $request, Response $response) {
    
    /** @var \League\Flysystem\FilesystemInterface $fileSystem */
    $fileSystem = $this->get('fileSystem');
    $fileSystem->put('default.txt', 'Hi there');

    /** @var \League\Flysystem\FilesystemInterface $fileSystem */
    $fileSystem = $this->get('other');
    $fileSystem->put('other.txt', 'Hi there');
});

$app->run();



return [
    'dependencies' => [
        'factories' => [
            // FlySystem using the default keys.
            'MyServiceName' => \WShafer\PSR11FlySystem\FlySystemFactory::class,
        ],
    ],
    
    'flysystem' => [
        'adaptors' => [
            // Array Keys are the names used for the adaptor
            'default' => [
                'type' => 'local', # Adaptor name or pre-configured service from the container
                
                // Adaptor specific options.  See adaptors below
                'options' => [
                    'root' => '/path/to/root', // Path on local filesystem
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            // Array Keys are the names used for the adaptor.  Default entry   
                // Adaptor specific options.  See adaptors below
                'options' => [
                    'root' => '/path/to/root', // Path on local filesystem
                ],
            ],
            
            'adaptorTwo' => [
                'type' => 'null', // Adaptor name or pre-configured service from the container
                'options' => [],  // Adaptor specific options.  See adaptors below
            ],
        ],
        
        'caches' => [
            // Array Keys are the names used for the cache
            //
            // Note: You can specify "default" here to overwrite the default settings for the
            // default cache.  Memory is used if not specified
            'default' => [
                'type' => 'psr6',
                // Cache specific options.  See caches below
                'options' => [
                    'service' => 'my_psr6_service_from_container',
                    'key' => 'my_key_',
                    'ttl' => 3000
                ], 
            ],
            
            'cacheTwo' => [
                'type' => 'psr6',
                // Cache specific options.  See caches below
                'options' => [
                    'service' => 'my_psr6_service_from_container',
                    'key' => 'my_key_',
                    'ttl' => 3000
                ], 
            ],
            
        ],
        
        'fileSystems' => [
            // Array Keys are the file systems identifiers.
            //
            // Note: You can specify "default" here to overwrite the default settings for the
            // default file system
            'default' => [
                'adaptor' => 'default', // Adaptor name from adaptor configuration
                'cache' => 'default',   // Cache name from adaptor configuration
                'plugins' => []         // User defined plugins to be injected into the file system
            ],
            
            // Mount Manager Config
            'manager' => [
                'adaptor' => 'manager',
                'fileSystems' => [
                    'local' => [
                        'adaptor' => 'default', // Adaptor name from adaptor configuration
                        'cache' => 'default',   // PSR-6 pre-configured service
                        'plugins' => []         // User defined plugins to be injected into the file system
                    ],
                    
                    'anotherFileSystem' => [
                        'adaptor' => 'adaptorTwo', // Adaptor name from adaptor configuration
                        'cache' => 'cacheTwo',     // PSR-6 pre-configured service
                        'plugins' => []            // User defined plugins to be injected into the file system
                    ],
                ],
            ],
        ],
    ],
];




return [
    'flysystem' => [
        'fileSystems' => [
            // Array Keys are the file systems identifiers
            'myFileSystemName' => [
                'adaptor' => 'default', // Required : Adaptor name from adaptor configuration
                'cache' => 'default',   // Optional : Cache name from adaptor configuration
                'plugins' => []         // Optional : User defined plugins to be injected into the file system
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'myAdaptorName' => [
                'type' => 'null',
                'options' => [], #No options available
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 'local',
                'options' => [
                    'root' => '/path/to/root', // Required : Path on local filesystem
                    'writeFlags' => LOCK_EX,   // Optional : PHP flags.  See: file_get_contents for more info
                    'linkBehavior' => League\Flysystem\Adapter\Local::DISALLOW_LINKS, // Optional : Link behavior
                    
                    // Optional:  Optional set of permissions to set for files
                    'permissions' => [
                        'file' => [
                            'public' => 0644,
                            'private' => 0600,
                        ],
                        'dir' => [
                            'public' => 0755,
                            'private' => 0700,
                        ]    
                    ]
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 'ftp',
                'options' => [
                    'host' => 'ftp.example.com', // Required : Host
                    'username' => 'username',    // Required : Username
                    'password' => 'password',    // Required : Password
                
                    // optional config settings
                    'port' => 21,
                    'root' => '/path/to/root',
                    'passive' => true,
                    'ssl' => true,
                    'timeout' => 30,
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 'sftp',
                'options' => [
                    'host' => 'example.com',                              // Required : Host
                    'port' => 21,                                         // Optional : Port
                    'username' => 'username',                             // Optional : Username
                    'password' => 'password',                             // Optional : Password
                    'privateKey' => 'path/to/or/contents/of/privatekey',  // Optional : Host
                    'root' => '/path/to/root',                            // Required : Root Path
                    'timeout' => 10,                                      // Optional : Timeout
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 'memory',
                'options' => [],  // No options available
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 'zip',
                'options' => [
                    'path' => '/some/path/to/file.zip' // Required : File name and path to use for zip file
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 'azure',
                'options' => [
                    'accountName' => 'account-name',  // Required : Account Name
                    'apiKey' => 'api-key',            // Required : API Key
                    'container' => 'container-name',  // Required : Container name
                    'prefix' => 'prefix_',            // Optional
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 's3',
                'options' => [
                    'key' => 'aws-key',         // Required : Key
                    'secret'  => 'aws-secret',  // Required : Secret
                    'region'  => 'us-east-1',   // Required : Region
                    'bucket'  => 'bucket-name', // Required : Bucket Name
                    'prefix'  => 'some/prefix', // Optional : Prefix
                    'version' => 'latest'       // Optional : Api Version.  Default: 'latest'
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'adaptors' => [
            'default' => [
                'type' => 'dropbox',
                'options' => [
                    'token'   => 'my-token', // Required : API Token
                    'prefix'  => 'prefix',   // Optional : Prefix
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'caches' => [
            'default' => [
                'type' => 'memory',
                'options' => [], // No options available
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'caches' => [
            'default' => [
                'type' => 'adaptor',
                'options' => [
                    'adaptor' => 'MyAdaptorName',  // Required : Adaptor name found in the adaptor config
                    'fileName' => 'my_cache_file', // Required : File name for cache file
                    'ttl' => 300                   // Optional : Time to live
                ], 
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'caches' => [
            'default' => [
                'type' => 'psr6',
                'options' => [
                    'service' => 'my_psr6_service_from_container', // Required : Service to be used from the container
                    'key' => 'my_key_',                            // Required : Cache Key
                    'ttl' => 3000                                  // Optional : Time to live
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'caches' => [
            'default' => [
                'type' => 'predis',
                'options' => [
                    'service' => 'my_predis_client_from_container', // Required : Configured Predis Client Service to pull from container
                    'key' => 'my_key_',                             // Required : Cache Key
                    'ttl' => 3000                                   // Optional : Time to live
                ],
            ],
        ],
    ],
];



return [
    'flysystem' => [
        'caches' => [
            'default' => [
                'type' => 'memcached',
                'options' => [
                    'service' => 'my_memcached_client_from_container', // Required : Configured Memcached Client Service to pull from container
                    'key' => 'my_key_',                                // Required : Cache Key
                    'ttl' => 3000                                      // Optional : Time to live
                ],
            ],
        ],
    ],
];