selene/filesystem

此包已被废弃且不再维护。未建议替代包。

selene filesystem

dev-development 2014-08-15 00:54 UTC

This package is not auto-updated.

Last update: 2016-02-07 09:58:51 UTC


README

Build Status Latest Stable Version Latest Unstable Version License HHVM Status

设置

<?php

use Selene\Components\Filesystem\Filesystem;

$filesystem = new Filesystem();

用法

删除文件

unlink()用于删除文件。

<?php

$filesystem->unlink('/path/source_file.jpg');
// or
$filesystem->remove('/path/source_file.jpg');

修改和访问文件的修改时间

<?php

// Sets the current time for `filemtime` and `fileatime`.
// If the file doesn't exists, it will be created.
$filesystem->touch('/path/target_file.jpg');

// Set modification and accesstime explicitly.
$filesystem->touch('/path/target_file.jpg', $time, $atime);

确保文件存在

ensureFile()基本上与touch相同,除非文件不存在。

<?php

$filesystem->ensureFile('/path/target_file.txt');

将内容写入文件

setContents()将内容写入文件。setContents()接受第二个参数$writeFlags,请参阅file_put_contents flags
如果文件不存在,它将被创建。

<?php

$filesystem->setContents('/path/target_file.txt', 'some content');

获取文件内容

getContents()将文件内容写入。它接受第二个参数$readFlags,请参阅file_get_contents flags

<?php

$filesystem->getContents('/path/target_file.txt'); // returns 'some content'

创建目录

mkdir()尝试创建目录。该方法接受第二个布尔参数,表示是否递归创建目录(默认为true)。

<?php

$filesystem->mkdir('/path/target_dir');

但是,如果目标已存在,则会抛出IOExecption异常。如果您想确保只有在目录不存在时才创建目录,可以使用Filesystem::ensureDirectory

<?php

$filesystem->ensureDirectory('/path/target_dir');

删除目录

<?php

$filesystem->rmdir('/path/source_dir');
// or
$filesystem->remove('/path/source_dir');

列出目录

<?php

$filesystem
    ->directory('/path/dir') // returns an instance of Selene\Components\Filesystem\Directory
    ->get();  // returns an instance of Selene\Components\FileCollection

复制文件或目录

copy()创建文件或目录及其所有内容的镜像。第二个$target参数是可选的。如果省略,copy()将在当前目录的上下文中创建一个新的唯一文件名。

<?php

$filesystem->copy('/target', '/source');
$filesystem->copy('/target'); // creates '/target copy 1'
$filesystem->copy('/target'); // creates '/target copy 2'

// define the copy prefix
$filesystem->setCopyPrefix('Kopie');
$filesystem->copy('/target'); // creates '/target Kopie 1'

处理文件权限

所有posix方法chmod()chwon()chgrp()都接受第三个布尔参数$recursive,表示是否更改所有子目录的权限设置。

更改文件或目录的读写权限

<?php

// for files:
$filesystem->chmod('target_file', 0644);

// for directories:
$filesystem->chmod('target_dir', 0777, true);

更改文件或目录的所有权

<?php

// for files:
$filesystem->chown('target_file', 'new_owner');
// or
$filesystem->chown('target_file', 500 /*the uid*/);

// for directories:
$filesystem->chown('target_dir', 'new_owner', true);

更改文件或目录的组

<?php

// for files:
$filesystem->chgrp('target_file', 'new_group');
// or
$filesystem->chgrp('target_file', 20 /*the gid*/);

// for directories:
$filesystem->chgrp('target_dir', 'new_group', true);
// or
$filesystem->chgrp('target_dir', 20 /*the gid*/, true);

使用umask设置适当的文件权限

<?php

// for files:
$filesystem->mask('target_file');
// or
$filesystem->mask('target_file', 0666);

// for directories:
$filesystem->mask('target_dir');
// or
$filesystem->mask('target_file', 0755);

TODO:处理文件和目录

目录对象Directory

目录对象提供了一些方便的、可链式调用的方法,用于与文件系统交互。它还允许列出其根路径的数组表示。目录对象和文件对象都实现了可数组和可JSON化的接口,提供了toArray()toJson()方法。

可链式调用的方法有:remove()mkdir()rmdir()chmod()chown()chgrp()in()notIn()filter()

文件对象 File

这些方法是可链式的:remove()mkdir()rmdir()chmod()chown()chgrp()

创建文件或目录对象

<?php

use Selene\Components\Filesystem\File;
use Selene\Components\Filesystem\Directory;
use Selene\Components\Filesystem\Filesystem;


// get an instance of `Selene\Components\Filesystem\Directory`
$dir = $filesystem->directory('/path/target_dir'); 
// or
$dir = new Directory(new Filesystem, '/path/target_dir');

// get an instance of `Selene\Components\Filesystem\File`
$file = $filesystem->file('/path/target_file'); 
// or
$file = new File(new Filesystem, '/path/target_file');

创建文件集合

<?php

// Get the directory contents as `FileCollection` instance:
$collection = $filesystem->directory('/path/target_dir')
    ->get(); 

// Get contents from a certain subdirectory or subdirectories:
$collection = $filesystem->directory('/path/target_dir')
    ->in(['sub_a', 'sub_b'])
    ->get(); 

// Exclude subdirectories:
$collection = $filesystem->directory('/path/target_dir')
    ->notIn(['sub_c', 'sub_d'])
    ->get(); 

// Filter for files:
// By now, `filter takes an arbitrary regeular expression. Glob matching is
// planed for the future. `
$collection = $filesystem->directory('/path/target_dir')
    ->in(['images'])
    ->filter('.*\.(jpe?g|png|gif)$')
    ->get(); 
<?php

foreach ($collection as $fileName => $fileInfo) {
    if ($fileInfo->isDir()) {
        // ...      
    }   
    if ($fileInfo->isFile()) {
        // ...      
    }   
}
<?php
// export collection to an array:
$collection->toArray();
// export collection to json
$collection->toJson();
 {
    ".": {
        "%directories%": {
            "source_tree": {
                "name": "source_tree",
                "path": "\/files\/source_tree",
                "lastmod": 1375118383,
                "type": "dir",
                "owner": 501,
                "group": 20,
                "%directories%": {
                    "nested_subtree": {
                        "name": "nested_subtree",
                        "path": "\/files\/source_tree\/nested_subtree",
                        "lastmod": 1375118383,
                        "type": "dir",
                        "owner": 501,
                        "group": 20
                    }
                },
                "%files%": {
                    "bar.txt": {
                        "name": "bar.txt",
                        "path": "\/files\/source_tree\/bar.txt",
                        "lastmod": 1375118383,
                        "type": "file",
                        "owner": 501,
                        "group": 20,
                        "extension": "txt",
                        "mimetype": "text\/plain"
                    }
                }
            }
        }
    }
}