Tarsana 文件系统库

2.0.1 2018-02-10 17:22 UTC

This package is not auto-updated.

Last update: 2024-09-26 20:29:58 UTC


README

Build Status Coverage Status Donate Software License

简单的类,用于处理文件系统操作。

安装

使用composer安装它

composer require tarsana/filesystem

处理文件和目录

Filesystem类旨在易于使用,并支持调用链,这使得代码更易读。

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

检查路径

可能你需要检查一个特定的路径是文件

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

还是目录

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

或者你只是想知道它是否存在,无论它是文件还是目录

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

如果你需要一次性检查多个路径呢?

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

但是,如果你想在不需要做多次检查的情况下知道路径的类型怎么办?

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

你可以使用通配符模式作为此函数的参数,结果将是

  • 'file':如果单个文件符合模式。

  • 'dir':如果单个目录符合模式。

  • 'collection':如果有多个文件和/或目录符合模式。

  • 'nothing':如果没有符合模式。

查找文件和目录

现在如果你想要获取与模式匹配的所有文件和目录怎么办?

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

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

你也可以操作这个集合

$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

处理文件

好,要处理一个文件,你应该首先获取它

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

注意,如果文件找不到,这将抛出一个异常。如果你想创建它(如果不存在),在第二个参数中指定true

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

你也可以一次性获取或创建多个文件

$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
}

现在你已经有了文件,你可以玩弄它了

$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

注意,所有的设置方法都返回相同的实例,以启用调用链。

处理目录

就像文件一样,你可以这样获取一个目录

$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

有了目录,你就可以玩弄它了

$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

注意,所有的设置方法都返回相同的实例,以启用调用链。

读取和写入资源

写入器

Tarsana\Filesystem\Resource\Writer允许将内容写入任何资源。

// 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();

读取器

Tarsana\Filesystem\Resources\Reader允许从任何资源读取内容。构造函数与Writer相同,但默认资源是STDIN

$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 !

缓冲区

Tarsana\Filesystem\Resource\Buffer既是读取器又是写入器。如果没有提供资源,它将使用php://memory来存储内容。