Skip to content

Createcomplexstructures

Francesco Tassi edited this page Feb 21, 2014 · 6 revisions

Create complex directory structures

Since version 0.10.0 one can create complex directory structures quite easily: $root = vfsStream::create($structure); Assumed $structure contains an array like this:

    $structure = array(
            'Core' => array(
                'AbstractFactory' => array(
                    'test.php' => 'some text content',
                    'other.php' => 'Some more text content',
                    'Invalid.csv' => 'Something else',
                ),
            'AnEmptyFolder' => array(),
            'badlocation.php' => 'some bad content',
        )
    );

the resulting directory tree will look like this:

root
\- Core
 |- badlocation.php
 |- AbstractFactory
 | |- test.php
 | |- other.php
 | \- Invalid.csv
 \- AnEmptyFolder

Arrays will become directories with their key as directory name, and strings becomes files with their key as file name and their value as file content. Mind that defining a value for the files is mandatory or the file will not be created.

If you need to modify other file or directory properties (especially permissions) please resort to the default API, this method is meant to create large structures more easily.

Starting with release 0.11.0 the behaviour of vfsStream::create(); changed a bit. While in 0.10.0 it completely replaced the directory structure this is not the case any more. It now only adds the given structure to an existing structure, either to the directory given with the second parameter, or to the root directory without removing any other childs of the root directory:

vfsStream::create($structure, $dir); // adds $structure as child to $dir
vfsStream::create($structure); // adds $structure to the root directory

If you want to replace the whole directory tree you can use the vfsStream::setup() method which now has a third parameter:

vfsStream::setup('root', null, $structure); // adds $structure to the fresh root directory.
Clone this wiki locally