wwsh / vfs
PHP应用的简单、可迭代、模拟文件系统迭代器
dev-master
2016-03-01 14:53 UTC
Requires (Dev)
- phpspec/phpspec: ~2.0
This package is not auto-updated.
Last update: 2024-09-14 18:26:15 UTC
README
A Virtual File System (VFS) directory iterator for PHP, compatible with the API of RecursiveDirectoryIterator and DirectoryIterator, able to mimic original class behaviour, while creating a virtual, non-existent file system from a PHP array.
输入
工厂接受两个参数
- 字符串或数组:可以是JSON文件的路径,也可以是数组本身。
- (可选)要迭代的初始路径,例如:"/music/oldies"
数组格式
每个键代表一个目录,每个值是文件名本身。
结构示例
'/music' => [
'1993 Haddaway - What Is Love (Remixes)' => [
"1 - Haddaway - What Is Love (7'' Mix).wav",
"2 - Haddaway - What Is Love (Eat This Mix).wav",
"3 - Haddaway - What Is Love (Tour De Trance Mix).wav"
],
"1985 Paul Hardcastle - 19 (The Final Story) [601 814]" => [
"01 - Paul Hardcastle - 19 (The Final Story).wav",
"02 - Paul Hardcastle - 19 (Destruction Mix).wav"
]
]
JSON格式
相同。请参阅 vfs-example.json 以获取示例。
输出
以下方法受到支持并完全模拟
public function getFilename();
public function getPath();
public function isDir();
public function isDot();
public function isFile();
public function current();
public function next();
public function valid();
public function getChildren();
public function hasChildren();
public function rewind();
请参阅 这里 了解原始API说明。
示例工作代码
$path = ... // define starting path
$vfsArray = []; // paste the VFS array here. See above.
$iterator = FileSystemFactory::create('vfs', [$path, $vfsArray]);
$iterator->rewind();
echo 'Browsing path ' . $iterator->getPath() . "...\n";
while ($iterator->valid()) {
$file = $iterator->current();
if ($file->isDot() || $file->isFile()) {
continue;
}
echo 'Processing directory ' . $file->getFilename() . "\n";
// process (string)$file.....
$results[] = $result;
$iterator->next();
}
警告
不建议在 foreach 循环中使用。
注意
通过MIT许可。请随意修改和贡献。祝您使用愉快!