Skip to content
File system abstraction
PHP
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Initial commit Mar 12, 2020
config Add filesystem implementation (#1) Mar 12, 2020
src Fix class check Mar 12, 2020
tests Initial commit Mar 12, 2020
.editorconfig Initial commit Mar 12, 2020
.gitattributes Initial commit Mar 12, 2020
.gitignore Initial commit Mar 12, 2020
.scrutinizer.yml
.styleci.yml Initial commit Mar 12, 2020
.travis.yml Initial commit Mar 12, 2020
CHANGELOG.md
LICENSE.md Initial commit Mar 12, 2020
README.md Add Flysystem to description in README (#3) Mar 13, 2020
composer.json Add filesystem implementation (#1) Mar 12, 2020
phpunit.xml.dist Adjust package name Mar 12, 2020

README.md

Yii filesystem


An abstract filesystem to manage files and directories. Based on Flysystem.

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage

Requirements

The package requires PHP 7.4 and is meant to be used with Yii 3.

Installation

composer require yiisoft/yii-filesystem

After installation, the Yiisoft\Yii\Filesystem\FilesystemInterface will be automatically registered in the main application container. This interface provides a local filesystem with root, as defined in the @root alias of the aliases parameter.

Getting started

The service could be obtained via DI container autowiring:

public function view(\Yiisoft\Yii\Filesystem\FilesystemInterface $filesystem)
{
    $someFileContent = $filesystem->read('/runtime/somefile.txt');
    //...
}

Also you can use aliases

$someFileContent = $filesystem->write('@views/site/testfile.txt', 'Test content');

Configuration

Additional filesystems could be configured in config/params.php as described below:

'file.storage' => [
    'runtimeStorage' => [
        'adapter' => [
            '__class' => \League\Flysystem\Local\LocalFilesystemAdapter::class,
            '__construct()' => [
                dirname(__DIR__) . '/runtime',
                \League\Flysystem\UnixVisibility\PortableVisibilityConverter::fromArray([
                    'file' => [
                        'public' => 0644,
                        'private' => 0600,
                    ],
                    'dir' => [
                        'public' => 0755,
                        'private' => 0700,
                    ],
                ]),
                LOCK_EX,
                \League\Flysystem\Local\LocalFilesystemAdapter::DISALLOW_LINKS
            ]
        ],
        'aliases' => [
            '@cache' => '@root/cache',
        ]
    ],
    'documentStorage' => [
        'adapter' => [
            '__class' => \League\Flysystem\Local\LocalFilesystemAdapter::class,
            '__construct()' => [
                dirname(__DIR__) . '/docs',
                \League\Flysystem\UnixVisibility\PortableVisibilityConverter::fromArray([
                    'file' => [
                        'public' => 0644,
                        'private' => 0600,
                        ],
                    'dir' => [
                        'public' => 0755,
                        'private' => 0700,
                        ],
                    ]),
                    LOCK_EX,
                    \League\Flysystem\Local\LocalFilesystemAdapter::DISALLOW_LINKS
                ]
        ],
        'aliases' => [
             '@invoices' => '@root/export/invoices',
             '@orders' => '@root/export/orders',
        ],
    ],
],

Aliases runtimeStorage and documentStorage will be automatically registered in the main application container. So, you can get it from the container:

public function index(ContainerInterface $container) 
{
    $documentStorage = $container->get('documentStorage');
}

If you prefer to use autowiring, you can create own interface for your filesystem.

interface ImageStorageInterface extends \Yiisoft\Yii\Filesystem\FilesystemInterface
{
}

And then register it in the params:

'file.storage' => [
    ImageStorageInterface::class => [
        'adapter' => [
            '__class' => \League\Flysystem\Local\LocalFilesystemAdapter::class,
            '__construct()' => [
                dirname(__DIR__) . '/storage/images',
                \League\Flysystem\UnixVisibility\PortableVisibilityConverter::fromArray([
                    'file' => [
                        'public' => 0644,
                        'private' => 0600,
                    ],
                    'dir' => [
                        'public' => 0755,
                        'private' => 0700,
                    ],
                ]),
                LOCK_EX,
                \League\Flysystem\Local\LocalFilesystemAdapter::DISALLOW_LINKS
            ]
        ],
    ],
]

Now you can use it like this:

//controller action
public function addImage(ImageStorageInterface $imageStorage)
{
    //get image stream...

    $imageStorage->writeStream('/path/to/image/myimage.jpeg', $myImageStream);
}

You can find documentation on FilesystemInterface methods in the Flysystem Docs.

You can’t perform that action at this time.