phuxtil / splfileinfo
SplFileInfo 兼容实现,允许操作虚拟(不存在)的文件
3.0.0
2021-05-18 16:17 UTC
Requires
- php: ^8
Requires (Dev)
- phpunit/phpunit: ^9
- symfony/var-dumper: ^4|^5
This package is auto-updated.
Last update: 2024-09-18 23:26:38 UTC
README
VirtualSplFileInfo
允许使用不存在的(虚拟)路径,同时仍然能够执行所有文件操作,如 getSize()
、isFile()
、getOwner()
等,并获得预定义的结果。
它支持设置器,并提供辅助方法,如 isVirtual()
、toArray()
、fromArray()
、fromSplFileInfo()
。
安装
composer require phuxtil/splfileinfo
注意:使用 v1.x 以兼容 PHP v7.0.x。 注意:使用 v2.x 以兼容 PHP v7.2+
使用
创建虚拟文件信息。
$path = '/tmp/not-yet/existing-path'; $virtualInfo = new VirtualSplFileInfo($path);
此时只设置了 PathInfo 数据。
$virtualInfo->getPathname(); # /tmp/not-yet/existing-path $virtualInfo->getPath(); # /tmp/not-yet ...
其余数据可以通过设置器进行更新。
$virtualInfo->setSize(120); $virtualInfo->setATime(time()); $virtualInfo->setPerms(0775); ...
注意:所有除了 PathInfo 的属性默认设置为 -1。
检查资源是否为虚拟的。
$virtualInfo->getType(); # virtual $virtualInfo->isVirtual(); # true
使用真实资源数据更新虚拟文件信息
@mkdir($path); $virtualInfo->fromSplFileInfo(new SplFileInfo($path)); $virtualInfo->isVirtual(); # false
VirtualFileInfo
与 \SplFileInfo
。
$splInfo = SplFileInfo {
path: "/tmp/not-yet"
filename: "existing-path"
basename: "existing-path"
pathname: "/tmp/not-yet/existing-path"
extension: ""
realPath: "/tmp/not-yet/existing-path"
aTime: 2019-06-15 22:07:47
mTime: 2019-06-15 22:07:47
cTime: 2019-06-15 22:07:47
inode: 10248205
size: 64
perms: 040755
owner: 0
group: 0
type: "dir"
writable: true
readable: true
executable: true
file: false
dir: true
link: false
}
$virtualInfo = Phuxtil\SplFileInfo\VirtualSplFileInfo {
path: "/tmp/not-yet"
filename: "existing-path"
basename: "existing-path"
pathname: "/tmp/not-yet/existing-path"
extension: ""
realPath: "/tmp/not-yet/existing-path"
aTime: 2019-06-15 22:07:47
mTime: 2019-06-15 22:07:47
cTime: 2019-06-15 22:07:47
inode: 10248205
size: 64
perms: 040755
owner: 0
group: 0
type: "dir"
writable: true
readable: true
executable: true
file: false
dir: true
link: false
}
额外方法
isVirtual(): bool
如果资源实际上不存在,则返回 true。
注意:isReadable()、isFile()... 等,即使资源不存在也可以返回 true。
fromSplFileInfo(\SplFileInfo $info)
$path = '/tmp/not-yet/existing-file.txt'; $virtualInfo = new VirtualSplFileInfo($path); // ... do stuff // create resource later file_put_contents($path, 'Lorem Ipsum'); // update virtual file info $virtualInfo->fromSplFileInfo(new SplFileInfo($path));
toArray(): array
$info = new VirtualSplFileInfo('/tmp/not-yet/existing-path'); $data = $info->toArray();
fromArray(array $data)
$info = new VirtualSplFileInfo('/tmp/not-yet/existing-path'); $info->fromArray( [ 'aTime' => 123, 'mTime' => 456, 'cTime' => 789, 'inode' => 222, 'size' => 333, 'perms' => 0755, 'owner' => 1, 'group' => 2, 'type' => 'dir', 'writable' => true, 'readable' => true, 'executable' => true, 'file' => false, 'dir' => true, 'link' => false, ] );
设置器支持
您可以使用设置器为所有除了 PathInfo
以外的属性设置值,在 \SplFileInfo
中默认解决。这些方法不需要资源已存在即可工作。
具有设置器的属性
realPath
aTime
mTime
cTime
inode
size
perms
owner
group
type
writable
readable
executable
file
dir
link
linkTarget
默认值
除了 PathInfo
以外的所有值默认设置为 -1
。
测试驱动开发(TDD)
查看更多示例,请参阅 tests。