使用 vfsStream 将文件插入特定 directory/node
Insert a file into specific directory/node using vfsStream
vfsStream的用例如下:
$directories = explode('/', 'path/to/some/dir');
$structure = [];
$reference =& $structure;
foreach ($directories as $directory) {
$reference[$directory] = [];
$reference =& $reference[$directory];
}
vfsStream::setup();
$root = vfsStream::create($structure);
$file = vfsStream::newFile('file')
->at($root) //should changes be introduced here?
->setContent($content = 'Some content here');
vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure()
的输出是
Array
(
[root] => Array
(
[path] => Array
(
[to] => Array
(
[some] => Array
(
[dir] => Array
(
)
)
)
)
[file] => Some content here
)
)
是否可以将文件插入特定目录,例如dir
目录下?
是的,显然可以使用 addChild()
方法将 child 添加到 vfsStreamFirectory
:
然而,我在 API Docs 中没有找到允许轻松遍历结构以添加内容的简单方法。 对于这种特殊情况,这是一个可怕的技巧,例如,如果每个路径元素有多个文件夹,它就会失败。
基本上我们必须递归遍历每个级别,验证名称是否是我们要添加文件的名称,然后在找到后添加它。
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
$directories = explode('/', 'path/to/some/dir');
$structure = [];
$reference =& $structure;
foreach ($directories as $directory) {
$reference[$directory] = [];
$reference =& $reference[$directory];
}
vfsStream::setup();
$root = vfsStream::create($structure);
$file = vfsStream::newFile('file')
->setContent($content = 'Some content here');
$elem = $root;
while ($elem instanceof vfsStreamDirectory)
{
if ($elem->getName() === 'dir')
{
$elem->addChild($file);
}
$children = $elem = $elem->getChildren();
if (!isset($children[0]))
{
break;
}
$elem = $children[0];
}
print_r(vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure());
给出了答案on github;因此而不是
->at($root)
应该使用
->at($root->getChild('path/to/some/dir')).
vfsStream的用例如下:
$directories = explode('/', 'path/to/some/dir');
$structure = [];
$reference =& $structure;
foreach ($directories as $directory) {
$reference[$directory] = [];
$reference =& $reference[$directory];
}
vfsStream::setup();
$root = vfsStream::create($structure);
$file = vfsStream::newFile('file')
->at($root) //should changes be introduced here?
->setContent($content = 'Some content here');
vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure()
的输出是
Array
(
[root] => Array
(
[path] => Array
(
[to] => Array
(
[some] => Array
(
[dir] => Array
(
)
)
)
)
[file] => Some content here
)
)
是否可以将文件插入特定目录,例如dir
目录下?
是的,显然可以使用 addChild()
方法将 child 添加到 vfsStreamFirectory
:
然而,我在 API Docs 中没有找到允许轻松遍历结构以添加内容的简单方法。 对于这种特殊情况,这是一个可怕的技巧,例如,如果每个路径元素有多个文件夹,它就会失败。
基本上我们必须递归遍历每个级别,验证名称是否是我们要添加文件的名称,然后在找到后添加它。
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
$directories = explode('/', 'path/to/some/dir');
$structure = [];
$reference =& $structure;
foreach ($directories as $directory) {
$reference[$directory] = [];
$reference =& $reference[$directory];
}
vfsStream::setup();
$root = vfsStream::create($structure);
$file = vfsStream::newFile('file')
->setContent($content = 'Some content here');
$elem = $root;
while ($elem instanceof vfsStreamDirectory)
{
if ($elem->getName() === 'dir')
{
$elem->addChild($file);
}
$children = $elem = $elem->getChildren();
if (!isset($children[0]))
{
break;
}
$elem = $children[0];
}
print_r(vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure());
给出了答案on github;因此而不是
->at($root)
应该使用
->at($root->getChild('path/to/some/dir')).