PHP code example of pluggit / storage

1. Go to this page and download the library: Download pluggit/storage 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/ */

    

pluggit / storage example snippets



//one adapter (save data to S3)
$s3Adapter = new \Cmp\Storage\Adapter\S3AWSAdapter();
$s3Adapter->put('/tmp/test.txt',"this is a test");


//two adapters with a fallback strategy and decorated with a logger
$s3Adapter = new \Cmp\Storage\Adapter\S3AWSAdapter();
$fallBackAdapter = (new StorageBuilder())->addAdapter($s3Adapter)
    ->addAdapter($s3Adapter) //the order matters with FallBackChainStrategy
    ->addAdapter($fileSystemAdapter)
    ->setLogger(new Logger())
    ->build(new \Cmp\Storage\Strategy\FallBackChainStrategy());

//it saves data to S3 and if fails save the data to FS
$fallBackAdapter->put('/tmp/test.txt',"this is a test");


//one step more fs adapter bind to one folder and strategy to another folder
$vfs = new \Cmp\Storage\MountableVirtualStorage($fileSystemStorage); //bind to any path that non match with mountpoint folders
$localMountPoint = new \Cmp\Storage\MountPoint('/tmp', $fileSystemAdapter);
$publicMountPoint = new \Cmp\Storage\MountPoint('/var/www/app/public', $fallBackAdapter);
$vfs->registerMountPoint($localMountPoint);
$vfs->registerMountPoint($publicMountPoint);

/*
//move file from /tmp (FS) to /var/www/app/public (S3) and if fails try to move from /tmp (FS) to /var/www/app/public (FS)
*/
$vfs->move('/tmp/testfile.jpg','/var/www/app/public/avatar.jpg' );

 $s3Adapter = new \Cmp\Storage\Adapter\S3AWSAdapter();
 $fileSystemAdapter = new \Cmp\Storage\Adapter\FileSystemAdapter();

 $localMountPoint = new \Cmp\Storage\MountPoint('/tmp', $fileSystemAdapter);
 $publicMountPoint = new \Cmp\Storage\MountPoint('/var/www/app/public', $s3Adapter);

 $vfs = new \Cmp\Storage\MountableVirtualStorage($fileSystemStorage); //bind to /
 $vfs->registerMountPoint($localMountPoint);
 $vfs->registerMountPoint($publicMountPoint);

 $vfs->delete('/tmp/testfile'); //running over filesystem adapter
 $vfs->put('/var/www/app/public/testfile', '..some content..')); //running over AWS S3 adapter