2017 © Pedro Peláez
 

yii2-extension yii2-flysystem

The flysystem extension for the Yii framework

image

maxodrom/yii2-flysystem

The flysystem extension for the Yii framework

  • Tuesday, October 10, 2017
  • by mercium
  • Repository
  • 1 Watchers
  • 0 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 45 Forks
  • 0 Open issues
  • 10 Versions
  • 25 % Grown

The README.md

Flysystem Extension for Yii 2

Code Quality Packagist Version Total Downloads, (*1)

This extension provides Flysystem integration for the Yii framework. Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one., (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

$ composer require creocoder/yii2-flysystem

or add, (*5)

"creocoder/yii2-flysystem": "0.8.*"

to the require section of your composer.json file., (*6)

Configuring

Local filesystem

Configure application components as follows, (*7)

return [
    //...
    'components' => [
        //...
        'fs' => [
            'class' => 'creocoder\flysystem\LocalFilesystem',
            'path' => '@webroot/files',
            // 'writeFlags' => LOCK_EX,
            // 'linkHandling' => 0002,
            // 'permissions' => [],
        ],
    ],
];

FTP filesystem

Configure application components as follows, (*8)

return [
    //...
    'components' => [
        //...
        'ftpFs' => [
            'class' => 'creocoder\flysystem\FtpFilesystem',
            'host' => 'ftp.example.com',
            // 'port' => 21,
            // 'username' => 'your-username',
            // 'password' => 'your-password',
            // 'ssl' => true,
            // 'timeout' => 60,
            // 'root' => '/path/to/root',
            // 'permPrivate' => 0700,
            // 'permPublic' => 0744,
            // 'passive' => false,
            // 'transferMode' => FTP_TEXT,
        ],
    ],
];

NULL filesystem

Configure application components as follows, (*9)

return [
    //...
    'components' => [
        //...
        'nullFs' => [
            'class' => 'creocoder\flysystem\NullFilesystem',
        ],
    ],
];

AWS S3 filesystem

Either run, (*10)

$ composer require league/flysystem-aws-s3-v3

or add, (*11)

"league/flysystem-aws-s3-v3": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*12)

return [
    //...
    'components' => [
        //...
        'awss3Fs' => [
            'class' => 'creocoder\flysystem\AwsS3Filesystem',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'bucket' => 'your-bucket',
            'region' => 'your-region',
            // 'version' => 'latest',
            // 'baseUrl' => 'your-base-url',
            // 'prefix' => 'your-prefix',
            // 'options' => [],
            // 'endpoint' => 'http://my-custom-url'
        ],
    ],
];

Azure filesystem

Add the following to the repositories section of your composer.json, (*13)

{
    "type": "pear",
    "url": "http://pear.php.net"
}

Either run, (*14)

$ composer require league/flysystem-azure

or add, (*15)

"league/flysystem-azure": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*16)

return [
    //...
    'components' => [
        //...
        'azureFs' => [
            'class' => 'creocoder\flysystem\AzureFilesystem',
            'accountName' => 'your-account-name',
            'accountKey' => 'your-account-key',
            'container' => 'your-container',
        ],
    ],
];

Copy filesystem

Either run, (*17)

$ composer require league/flysystem-copy

or add, (*18)

"league/flysystem-copy": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*19)

return [
    //...
    'components' => [
        //...
        'copyFs' => [
            'class' => 'creocoder\flysystem\CopyFilesystem',
            'consumerKey' => 'your-consumer-key',
            'consumerSecret' => 'your-consumer-secret',
            'accessToken' => 'your-access-token',
            'tokenSecret' => 'your-token-secret',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

Dropbox filesystem

Either run, (*20)

$ composer require league/flysystem-dropbox

or add, (*21)

"league/flysystem-dropbox": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*22)

return [
    //...
    'components' => [
        //...
        'dropboxFs' => [
            'class' => 'creocoder\flysystem\DropboxFilesystem',
            'token' => 'your-token',
            'app' => 'your-app',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

Google Cloud filesystem

Run, (*23)

$ composer require "superbalist/flysystem-google-storage": "^5.0"

and configure application components as follows, (*24)

return [
    //...
    'components' => [
        //...
        'googleCloudFs' => [
            'class' => 'creocoder\flysystem\GoogleCloudFilesystem',
            'projectId' => 'GOOGLE_PROJECT_ID',
            'bucket' => 'GOOGLE_BUCKET',
            'keyFilePath' => 'GOOGLE_KEY_FILE_PATH',
        ],
    ],
];

Note: Credential configuration is read from the keyFile., (*25)

GridFS filesystem

Either run, (*26)

$ composer require league/flysystem-gridfs

or add, (*27)

"league/flysystem-gridfs": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*28)

return [
    //...
    'components' => [
        //...
        'gridFs' => [
            'class' => 'creocoder\flysystem\GridFSFilesystem',
            'server' => 'mongodb://localhost:27017',
            'database' => 'your-database',
        ],
    ],
];

Rackspace filesystem

Either run, (*29)

$ composer require league/flysystem-rackspace

or add, (*30)

"league/flysystem-rackspace": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*31)

return [
    //...
    'components' => [
        //...
        'rackspaceFs' => [
            'class' => 'creocoder\flysystem\RackspaceFilesystem',
            'endpoint' => 'your-endpoint',
            'region' => 'your-region',
            'username' => 'your-username',
            'apiKey' => 'your-api-key',
            'container' => 'your-container',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

SFTP filesystem

Either run, (*32)

$ composer require league/flysystem-sftp

or add, (*33)

"league/flysystem-sftp": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*34)

return [
    //...
    'components' => [
        //...
        'sftpFs' => [
            'class' => 'creocoder\flysystem\SftpFilesystem',
            'host' => 'sftp.example.com',
            // 'port' => 22,
            'username' => 'your-username',
            'password' => 'your-password',
            'privateKey' => '/path/to/or/contents/of/privatekey',
            // 'timeout' => 60,
            // 'root' => '/path/to/root',
            // 'permPrivate' => 0700,
            // 'permPublic' => 0744,
        ],
    ],
];

WebDAV filesystem

Either run, (*35)

$ composer require league/flysystem-webdav

or add, (*36)

"league/flysystem-webdav": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*37)

return [
    //...
    'components' => [
        //...
        'webdavFs' => [
            'class' => 'creocoder\flysystem\WebDAVFilesystem',
            'baseUri' => 'your-base-uri',
            // 'userName' => 'your-user-name',
            // 'password' => 'your-password',
            // 'proxy' => 'your-proxy',
            // 'authType' => \Sabre\DAV\Client::AUTH_BASIC,
            // 'encoding' => \Sabre\DAV\Client::ENCODING_IDENTITY,
            // 'prefix' => 'your-prefix',
        ],
    ],
];

ZipArchive filesystem

Either run, (*38)

$ composer require league/flysystem-ziparchive

or add, (*39)

"league/flysystem-ziparchive": "~1.0"

to the require section of your composer.json file and configure application components as follows, (*40)

return [
    //...
    'components' => [
        //...
        'ziparchiveFs' => [
            'class' => 'creocoder\flysystem\ZipArchiveFilesystem',
            'path' => '@webroot/files/archive.zip',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

Caching feature

Either run, (*41)

$ composer require league/flysystem-cached-adapter

or add, (*42)

"league/flysystem-cached-adapter": "~1.0"

to the require section of your composer.json file and configure fsID application component as follows, (*43)

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'cache' => 'cacheID',
            // 'cacheKey' => 'flysystem',
            // 'cacheDuration' => 3600,
        ],
    ],
];

Replication feature

Either run, (*44)

$ composer require league/flysystem-replicate-adapter

or add, (*45)

"league/flysystem-replicate-adapter": "~1.0"

to the require section of your composer.json file and configure fsID application component as follows, (*46)

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'replica' => 'anotherFsID',
        ],
    ],
];

Global visibility settings

Configure fsID application component as follows, (*47)

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'config' => [
                'visibility' => \League\Flysystem\AdapterInterface::VISIBILITY_PRIVATE,
            ],
        ],
    ],
];

Usage

Writing files

To write file, (*48)

Yii::$app->fs->write('filename.ext', 'contents');

To write file using stream contents, (*49)

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);

Updating files

To update file, (*50)

Yii::$app->fs->update('filename.ext', 'contents');

To update file using stream contents, (*51)

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->updateStream('filename.ext', $stream);

Writing or updating files

To write or update file, (*52)

Yii::$app->fs->put('filename.ext', 'contents');

To write or update file using stream contents, (*53)

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->putStream('filename.ext', $stream);

Reading files

To read file, (*54)

$contents = Yii::$app->fs->read('filename.ext');

To retrieve a read-stream, (*55)

$stream = Yii::$app->fs->readStream('filename.ext');
$contents = stream_get_contents($stream);
fclose($stream);

Checking if a file exists

To check if a file exists, (*56)

$exists = Yii::$app->fs->has('filename.ext');

Deleting files

To delete file, (*57)

Yii::$app->fs->delete('filename.ext');

Reading and deleting files

To read and delete file, (*58)

$contents = Yii::$app->fs->readAndDelete('filename.ext');

Renaming files

To rename file, (*59)

Yii::$app->fs->rename('filename.ext', 'newname.ext');

Getting files mimetype

To get file mimetype, (*60)

$mimetype = Yii::$app->fs->getMimetype('filename.ext');

Getting files timestamp

To get file timestamp, (*61)

$timestamp = Yii::$app->fs->getTimestamp('filename.ext');

Getting files size

To get file size, (*62)

$timestamp = Yii::$app->fs->getSize('filename.ext');

Creating directories

To create directory, (*63)

Yii::$app->fs->createDir('path/to/directory');

Directories are also made implicitly when writing to a deeper path, (*64)

Yii::$app->fs->write('path/to/filename.ext');

Deleting directories

To delete directory, (*65)

Yii::$app->fs->deleteDir('path/to/filename.ext');

Managing visibility

Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private., (*66)

use League\Flysystem\AdapterInterface;

Yii::$app->fs->write('filename.ext', 'contents', [
    'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);

You can also change and check visibility of existing files, (*67)

use League\Flysystem\AdapterInterface;

if (Yii::$app->fs->getVisibility('filename.ext') === AdapterInterface::VISIBILITY_PRIVATE) {
    Yii::$app->fs->setVisibility('filename.ext', AdapterInterface::VISIBILITY_PUBLIC);
}

Listing contents

To list contents, (*68)

$contents = Yii::$app->fs->listContents();

foreach ($contents as $object) {
    echo $object['basename']
        . ' is located at' . $object['path']
        . ' and is a ' . $object['type'];
}

By default Flysystem lists the top directory non-recursively. You can supply a directory name and recursive boolean to get more precise results, (*69)

$contents = Yii::$app->fs->listContents('path/to/directory', true);

Listing paths

To list paths, (*70)

$paths = Yii::$app->fs->listPaths();

foreach ($paths as $path) {
    echo $path;
}

Listing with ensured presence of specific metadata

To list with ensured presence of specific metadata, (*71)

$listing = Yii::$app->fs->listWith(
    ['mimetype', 'size', 'timestamp'],
    'optional/path/to/directory',
    true
);

foreach ($listing as $object) {
    echo $object['path'] . ' has mimetype: ' . $object['mimetype'];
}

Getting file info with explicit metadata

To get file info with explicit metadata, (*72)

$info = Yii::$app->fs->getWithMetadata('path/to/filename.ext', ['timestamp', 'mimetype']);
echo $info['mimetype'];
echo $info['timestamp'];

Donating

Support this project and others by creocoder via gratipay., (*73)

Support via Gratipay, (*74)

The Versions

09/02 2015

0.1.0

0.1.0.0

The flysystem extension for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexander Kochetov

yii2 flysystem