2017 © Pedro Peláez
 

library storage

A storage front for local or remote storage system

image

phossa2/storage

A storage front for local or remote storage system

  • Saturday, October 1, 2016
  • by phossa2
  • Repository
  • 1 Watchers
  • 2 Stars
  • 109 Installations
  • PHP
  • 2 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

phossa2/storage

Build Status Code Quality Code Climate PHP 7 ready HHVM Latest Stable Version License, (*1)

phossa2/storage is a PHP storage library with support for local or cloud storage., (*2)

It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with PSR-1, PSR-2, PSR-3, PSR-4, and the proposed PSR-5., (*3)

Installation

Install via the composer utility., (*4)

composer require "phossa2/storage"

or add the following lines to your composer.json, (*5)

{
    "require": {
       "phossa2/storage": "2.*"
    }
}

Introduction

Usage

Create the storage instance,, (*11)

use Phossa2\Storage\Storage;
use Phossa2\Storage\Filesystem;
use Phossa2\Storage\Driver\LocalDriver;

// mount local dir '/www/storage' to '/local'
$storage = new Storage(
    '/local',
    new Filesystem('/www/storage')
);

// add a file
$filename = '/local/newfile.txt';
$storage->put($filename, 'this is the content');

// check existens
if ($storage->has($filename)) {
    // read file content
    $str = $storage->get($filename);

    // delete the file
    $storage->del($filename);
}

// mount another filesystem
$storage->mount('/aws', new Filesystem(new AwsDriver()));

Features

  • Restful APIs, (*12)

    Support for simple and instinctive APIs like get(), put(), has() and del() etc., (*13)

    Others APIs like, (*14)

    • meta()

    Get the meta data of the file, (*15)

    // get the meta data
    if ($storage->has($file)) {
        $meta = $storage->meta($file);
    }
    
    // update meta data
    $new = ['mtime' => time()];
    $storage->put($file, null, $new);
    
    • copy() and move()

    Copy or move files in or between filesystems, (*16)

    // move to another name
    $storage->move('/local/README.txt', '/local/README.bak.txt');
    
    // copy into another filesystem's directory
    $storage->copy('/local/README.txt', '/aws/www/');
    
  • Unified path syntax, (*17)

    Uses unified path syntax like `/local/dir/file.txt' for all systems including windows. The underlying driver is responsible for translating path transparently., (*18)

    $storage = new Storage(
    '/disk/d',
    new Filesystem(new LocalDriver('D:\\\\'))
    );
    
    $storage->put('/disk/d/temp/newfile.txt', 'this is content');
    
  • Mounting and umounting filesystems, (*19)

    filesytem is a wrapper of different drivers with permissions. User may mount a read only filesystem as follows,, (*20)

    // mount as readonly, default is Filesystem::PERM_ALL
    $storage->mount(
      '/readonly',
      new Filesystem(
        '/home/www/public',
        Filesystem::PERM_READ
      )
    );
    
    // will fail
    $storage->put('/readonly/newfile.txt', 'this is the content');
    

    Different filesystem may use same driver,, (*21)

    $driver = new LocalDriver('/home/www/public');
    
    // writable
    $storage->mount('/public', new Filesystem($driver));
    
    // readonly
    $storage->mount('/readonly', new Filesystem($driver, Filesystem::PERM_READ));
    

    Filesystems may overlapping on top of others,, (*22)

    // mount root
    $storage->mount('/', new Filesystem(...));
    
    // mount var
    $storage->mount('/var', new Filesystem(...));
    
    // mount cache
    $storage->mount('/var/cache', new Filesystem(...));
    
  • Drivers, (*23)

    Support for different drivers inlucing local or cloud storage., (*24)

  • Streaming, (*25)

    Write and read streams as follows,, (*26)

    // read stream
    $stream = $storage->get('/local/thefile.txt', true);
    
    // write with stream
    $storage->put('/local/anotherfile.txt', $stream);
    
    // close it
    if (is_resource($stream)) {
      fclose($stream);
    }
    

APIs

  • \Phossa2\Storage\Storage, (*27)

    • bool has(string $path)

    check $path existens., (*28)

    • null|string|array|resource get(string $path, bool $getAsStream)

    Get content of the $path., (*29)

    • If not found or failure, returns NULL., (*30)

    • If $path is a directory, returns an array of the full paths of the files under this $path., (*31)

    • If $getAsStream is true, returns a stream handler., (*32)

    • bool put(string $path, string|resource|null $content, array $meta = []), (*33)

    Set the content or meta data of the $path., (*34)

    • bool del(string $path)

    Remove the $path. If $path is a directory, will remove all files under this path and the path itself (unless it is a mount point)., (*35)

    • bool copy(string $from, string $to) and bool copy(string $from, string $to)

    Copy or move $from to $to., (*36)

    • array meta(string $path)

    Get the meta data of $path. If not found or error, returns []., (*37)

    • Phossa2\Storage\Path path(string $path)

    Returns a Phossa2\Storage\Path object, (*38)

  • Error related, (*39)

    • bool hasError()

    Has error ?, (*40)

    • string getError()

    Get previous error message. If no error, returns ''., (*41)

    if (!$storage->copy('/local/from.txt', '/local/to.txt')) {
        $err = $storage->getError();
    }
    
    • string getErrorCode()

    Get a numeric string of the error code., (*42)

Change log

Please see CHANGELOG from more information., (*43)

Contributing

Please see CONTRIBUTE for more information., (*44)

Dependencies

  • PHP >= 5.4.0, (*45)

  • phossa2/shared >= 2.0.23, (*46)

License

MIT License, (*47)

The Versions

01/10 2016

dev-master

9999999-dev https://github.com/phossa2/storage

A storage front for local or remote storage system

  Sources   Download

MIT

The Requires

 

The Development Requires

filesystem storage phossa phossa2

23/08 2016

2.0.1

2.0.1.0 https://github.com/phossa2/storage

A storage front for local or remote storage system

  Sources   Download

MIT

The Requires

 

The Development Requires

storage phossa

11/08 2016

2.0.0

2.0.0.0 https://github.com/phossa2/storage

A storage front for local or remote storage system

  Sources   Download

MIT

The Requires

 

The Development Requires

storage phossa