aphp/files

The Filesystem component provides basic utilities for the filesystem

1.1.2 2020-07-27 19:31 UTC

This package is auto-updated.

Last update: 2020-07-27 19:33:12 UTC


README

PHP Support PHP Support License Travis

Introduction

Files provides basic utilities for the filesystem.

Installation

composer require aphp/files ~1.1.0

Syntax

Init

$path = new FilePath('C:\aphp_git\projectGenerator\hello.jpg');

Path Interface

interface PathInterface
{
	function replacePath($path, $rightIndex = 1);

	// string
	function getPath($index = -1);

	function add($path); // $this FilePath
	function pop($count = 1); // $this FilePath
}

getPath

$e[] = $path->getPath(0);
$e[] = $path->getPath(-1);
$e[] = $path->getPath(-2);
print_r($e);
/*
[0] => C:/aphp_git/projectGenerator/hello.jpg
[1] => C:/aphp_git/projectGenerator
[2] => C:/aphp_git
*/

add pop

$e[] = $path->getPath(0);

$path->pop();
$e[] = $path->getPath(0);

$path->add('image01.jpg');
$e[] = $path->getPath(0);

$path->pop()->add('image02.jpg');
$e[] = $path->getPath(0);

print_r($e);
/*
[0] => C:/aphp_git/projectGenerator/hello.jpg
[1] => C:/aphp_git/projectGenerator
[2] => C:/aphp_git/projectGenerator/image01.jpg
[3] => C:/aphp_git/projectGenerator/image02.jpg
*/

replacePath

$orig = $path->getPath();

$path->replacePath('X:/tests/folder', 0);
$e[] = $path->getPath(0);
$path->replacePath($orig, 0);

$path->replacePath('X:/tests/folder', 1);
$e[] = $path->getPath(0);
$path->replacePath($orig, 0);

$path->replacePath('X:/tests/folder', 2);
$e[] = $path->getPath(0);
echo $orig . PHP_EOL;
print_r($e);
/*
C:/aphp_git/projectGenerator/otherName.exe
Array
(
    [0] => X:/tests/folder
    [1] => X:/tests/folder/otherName.exe
    [2] => X:/tests/folder/projectGenerator/otherName.exe
)*/

File Interface

interface FileInterface
{
	// string
	function fileName($extension = true);
	function extension();
	function replaceFileName($filename, $extension = true);
	function replaceExtension($extension);
}

replaceFileName, replaceExtension

$e[] = $path->getPath();

$path->replaceFileName('newName.txt');
$e[] = $path->getPath();

$path->replaceFileName('otherName', false);
$e[] = $path->getPath();

$path->replaceExtension('exe');
$e[] = $path->getPath();
print_r($e);
/*
[0] => C:/aphp_git/projectGenerator/image02.jpg
[1] => C:/aphp_git/projectGenerator/newName.txt
[2] => C:/aphp_git/projectGenerator/otherName.txt
[3] => C:/aphp_git/projectGenerator/otherName.exe
*/

File System Interface

interface FileSystemInterface
{
	// r = recursive
	// f = files
	// d = dir
	// o = return objects
	// s = sort by name (natural)
	// n = native return SplFileInfo
	// h = hidden files (ignoreVCS)
	function getPathListOpt($flags = 'rfdosh', /* function(Finder $finder){ } */ $finderConfigFn = null);
	function isRoot($_win = null);
	function isExists();

	function mkdir();
	function remove($recursive = true, $rmdir = false);
	function move(/* SplFileInfo | string */ $destination, $overwriteNewerFiles = true);
	function copy(/* SplFileInfo | string */ $destination, $overwriteNewerFiles = true);
	function touch($time = 0);
}

FileObj

class FileObj extends FilePath {
	public $_mime = null;

	public function reset() { $this->_mime = null; }

	// string
	public function mime();
	public function mimeExtension($default = 'bin');
	 // bool
	public function isText();
	public function isImage();
	public function isBinary();
}

Test running

  • install phpunit, composer, php if not installed
  • run composer install at package dir
  • run composer run-script test

On linux use *.sh files like *.bat files

🔵 Useful links

More features

For more features:

  • Read source code and examples
  • Practice with Files in real code