Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a file into specific directory/node using vfsStream

The use case of vfsStream is as follows:

$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');

The output of vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure() is

Array
(
    [root] => Array
    (
        [path] => Array
        (
            [to] => Array
            (
                [some] => Array
                (
                    [dir] => Array
                    (
                    )
                )
            )
        )

        [file] => Some content here
    )
)

Is it possible to insert a file into specific directory, for example, under dir directory?

like image 633
sitilge Avatar asked Jul 20 '26 02:07

sitilge


1 Answers

Yes, apparently it is possible to add a child to a vfsStreamFirectory with the addChild() method:

However I've found no simple method in the API Docs that allow for easily traversing a structure to add content. Here is a horrible hacky of doing this for this particular case, it would fail if for instance there were more than one folder per path element.

Basically we have to recursively step through each level, verify if the name is the one to which we want to add the file, then add it when it's found.

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());
like image 82
Félix Adriyel Gagnon-Grenier Avatar answered Jul 22 '26 15:07

Félix Adriyel Gagnon-Grenier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!