2017 © Pedro Peláez
 

library io

Tarsana Filesystem library

image

tarsana/io

Tarsana Filesystem library

  • Sunday, February 11, 2018
  • by webNeat
  • Repository
  • 2 Watchers
  • 1 Stars
  • 324 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 27 % Grown

The README.md

Tarsana Filesystem Package

Build Status Coverage Status Donate Software License, (*1)

Simple classes to handle Filesystem operations., (*2)

Installation

Install it using composer, (*3)

composer require tarsana/filesystem

Handeling Files and Directories

The Filesystem class was designed to be used easily and support call chaining which makes the code more readable., (*4)

// Create a Filesystem instance given a root path
$fs = new Tarsana\Filesystem('path/to/fs/root/directory');

Checking Paths

Maybe you need to check if a specific path is a file, (*5)

if ($fs->isFile('path'))

or a directory, (*6)

if ($fs->isDir('path'))

or you just want to know it exists, no matter it's a file or directory, (*7)

if ($fs->isAny('path'))

What if you need to check multiple paths at once ?, (*8)

if ($fs->areFiles(['path1', 'path2', 'path3']))
if ($fs->areDirs(['path1', 'path2', 'path3']))
if ($fs->areAny(['path1', 'path2', 'path3']))

But what if you want to know the type of a path without having to do multiple checks ?, (*9)

$fs->whatIs('path-pattern')

You can use wildcard pattern as argument to this function and the result will be:, (*10)

  • 'file': if a single file corresponds to the pattern., (*11)

  • 'dir': if a single directory corresponds to the pattern., (*12)

  • 'collection': if multiple files and/or directories correspond to the pattern., (*13)

  • 'nothing': if nothing corresponds to the pattern., (*14)

Finding Files and Directories

Now what if you want to get all files and directories corresponding to a pattern ?, (*15)

$collection = $fs->find('pattern'); // a Collection instance

foreach ($collection->asArray() as $fileOrDir) {
    // Handle the file or directory
}

You can also manipulate the collection, (*16)

$collection->count(); // number of elements
$collection->add($fs->file('path/to/file')); // add new element to the collection
$collection->contains('path'); // checks if the collection contains an element with that path
$collection->remove('path'); // remove the element having the path from the collection

$collection->files(); // a new collection containing only files
$collection->dirs(); // a new collection containing only directories

$collection->first(); // the first element
$collection->last(); // the last element

$collection->paths(); // array of paths of the files and directories
$collection->names(); // array of names of the files and directories

Handling Files

Well, to handle a file, you should get it first, (*17)

$file = $fs->file('path/to/file');

Notice that this will throw an exception if the file is not found. If you want to create it when missing; specify true in the second argument, (*18)

$file = $fs->file('path/to/file', true);

You can also get or create multiple files at once, (*19)

$files = $fs->files([
    'path/to/file1',
    'path/to/file2',
    'path/to/file3'
]); // specify the second argument as true if you want missing files to be created

foreach ($files->asArray() as $file) {
    // Handle the file
}

Now that you have the file, you can play with it, (*20)

$file->name(); // get the name
$file->name('new-name.txt'); // renaming the file

$file->path(); // get the absolute path
$file->path('new/absolute/path'); // moving the file

$file->content(); // reading the content
$file->content('new content'); // writing to the file
$file->append('additional content'); // add content to the file

$file->hash(); // get the md5 hash of the content
$file->extension(); // get the extension (like "txt" or "php")

$file->perms(); // get the file permissions as string (like "0755")
$file->isWritable(); // check if the file is writable
$file->isExecutable(); // check if the file is executable

$copy = $file->copyAs('absolute/path/to/file-copy'); // Copy the file
$file->remove(); // Remove the file

Notice that all setters return the same instance to enable call chaining., (*21)

Handling Directories

Just like the file, you can get a directory like that, (*22)

$dir = $fs->dir('path/to/dir'); // throws exception if the directory not found
$dir = $fs->dir('path/to/dir', true); // creates the directory if not found
$dirs = $fs->dirs([
    'path/to/file1',
    'path/to/file2',
    'path/to/file3'
]); // a collection containing directories

Having a directory, you can play with it, (*23)

$dir->name(); // get the name
$dir->name('new-name'); // renaming the directory

$dir->path(); // get the absolute path
$dir->path('new/absolute/path'); // moving the directory

$dir->perms(); // get the directory permissions as string (like "0755")

$copy = $dir->copyAs('absolute/path/to/dir-copy'); // Copy the directory
$dir->remove(); // Remove the directory

$dir->fs(); // get a Filesystem instance having this directory as root

Notice that all setters return the same instance to enable call chaining., (*24)

Reading & Writing to Resources

Writer

Tarsana\Filesystem\Resource\Writer gives the possibility to write content to any resource., (*25)

// Default constructor uses STDOUT by default
$stdout = new Writer;
// Any writable resource can be used
$res = fopen('temp.txt', 'w');
$out = Writer($res);
// Or just give the path
$out = Writer('php://memory');

// Writing content
$out->write('Hello ')->write('World !');
// Writes "Hello World !" to the resource
$out->writeLine('Hi');
// Writes "Hi".PHP_EOL to the resource

// The resource is closed when the $out object is destructed
// But you can still close it before
$out->close();

Reader

Tarsana\Filesystem\Resources\Reader gives the possibility to read content from any resource. Constructors are the same as Writer but the default resource is STDIN., (*26)

$stdin = new Reader; // when no parameter is given, it uses STDIN by default

$stdin->read(); // reads the whole content of STDIN
$stdin->read(100); // reads 100 bytes from STDIN
$stdin->readUntil(' '); // reads until the first ' ' (space) or EOF
$stdin->readLine(); // reads until PHP_EOL or EOF
// If the STDIN is empty and we do
$stdin->blocking(false)->read();
// This will return immediately an empty string; no blocking !

Buffer

Tarsana\Filesystem\Resource\Buffer is a Reader and Writer at the same time. If no resource is given, it uses php://memory to store content., (*27)

The Versions

11/02 2018

dev-master

9999999-dev

Tarsana Filesystem library

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Amine Ben hammou

10/02 2018

2.0.1

2.0.1.0

Tarsana IO library

  Sources   Download

MIT

The Development Requires

by Amine Ben hammou

10/02 2018

dev-absolute-paths-detection

dev-absolute-paths-detection

Tarsana IO library

  Sources   Download

MIT

The Development Requires

by Amine Ben hammou

02/09 2017

2.0.0

2.0.0.0

Tarsana IO library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amine Ben hammou

02/09 2017

2.0.0-alpha.1

2.0.0.0-alpha1

Tarsana IO library

  Sources   Download

MIT

The Development Requires

by Amine Ben hammou

25/08 2017

4.0.0-alpha

4.0.0.0-alpha

Tarsana IO library

  Sources   Download

MIT

The Development Requires

by Amine Ben hammou

25/08 2017

2.0.0-alpha

2.0.0.0-alpha

Tarsana IO library

  Sources   Download

MIT

The Development Requires

by Amine Ben hammou

18/05 2017

3.1.0

3.1.0.0

Tarsana IO library

  Sources   Download

MIT

The Development Requires

by Amine Ben hammou

18/05 2017

dev-console

dev-console

Tarsana IO library

  Sources   Download

MIT

The Development Requires

by Amine Ben hammou

15/09 2016

3.0.0

3.0.0.0

Simple IO helper classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amine Ben hammou

04/07 2016

1.2.0

1.2.0.0

Simple IO and filesystem helper classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amine Ben hammou

03/01 2016

1.1.0

1.1.0.0

Simple IO and filesystem helper classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amine Ben hammou

30/12 2015

1.0.1

1.0.1.0

Simple IO and filesystem helper classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amine Ben hammou

16/12 2015

1.0.0

1.0.0.0

Simple IO and filesystem helper classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amine Ben hammou