Neuron-PHP

MemoryFileSystem
in package
implements IFileSystem

In-memory file system implementation for testing.

Provides a virtual file system that exists entirely in memory. Files and directories can be programmatically added for testing without touching the real file system. Perfect for unit testing file-dependent operations.

Table of Contents

Interfaces

IFileSystem
Interface for file system operations abstraction.

Properties

$cwd  : string
$directories  : array<string, bool>
$files  : array<string, string>

Methods

addDirectory()  : void
Add a directory to the virtual file system
addFile()  : void
Add a file to the virtual file system
clear()  : void
Clear all files and directories
fileExists()  : bool
Check if a file exists
getcwd()  : string|false
Get current working directory
getDirectories()  : array<string, bool>
Get all directories in the virtual file system
getFiles()  : array<string, string>
Get all files in the virtual file system
glob()  : array<string|int, mixed>|false
Find pathnames matching a pattern
isDir()  : bool
Check if path is a directory
mkdir()  : bool
Create a directory
readFile()  : string|false
Read entire file contents
realpath()  : string|false
Get absolute path (resolve symlinks, relative paths)
removeDirectory()  : void
Remove a directory from the virtual file system
removeFile()  : void
Remove a file from the virtual file system
rmdir()  : bool
Remove a directory
scandir()  : array<string|int, mixed>|false
List files and directories in a directory
setCwd()  : void
Set the current working directory
unlink()  : bool
Delete a file
writeFile()  : int|false
Write data to file
globToRegex()  : string
Convert glob pattern to regex pattern

Properties

$directories

private array<string, bool> $directories = []

In-memory directories (path => true)

$files

private array<string, string> $files = []

In-memory files (path => content)

Methods

addDirectory()

Add a directory to the virtual file system

public addDirectory(string $path) : void
Parameters
$path : string

Directory path

addFile()

Add a file to the virtual file system

public addFile(string $path, string $content) : void
Parameters
$path : string

File path

$content : string

File content

fileExists()

Check if a file exists

public fileExists(string $path) : bool
Parameters
$path : string

File path

Tags
inheritDoc
Return values
bool

True if file exists, false otherwise

getcwd()

Get current working directory

public getcwd() : string|false
Tags
inheritDoc
Return values
string|false

Current directory or false on failure

getDirectories()

Get all directories in the virtual file system

public getDirectories() : array<string, bool>
Return values
array<string, bool>

getFiles()

Get all files in the virtual file system

public getFiles() : array<string, string>
Return values
array<string, string>

glob()

Find pathnames matching a pattern

public glob(string $pattern) : array<string|int, mixed>|false
Parameters
$pattern : string

Pattern to match (e.g., "/path/.txt", "/path/**​/.php")

Tags
inheritDoc
Return values
array<string|int, mixed>|false

Array of matching paths or false on failure

isDir()

Check if path is a directory

public isDir(string $path) : bool
Parameters
$path : string

Directory path

Tags
inheritDoc
Return values
bool

True if directory exists, false otherwise

mkdir()

Create a directory

public mkdir(string $path[, int $permissions = 0755 ][, bool $recursive = true ]) : bool
Parameters
$path : string

Directory path

$permissions : int = 0755

Directory permissions (default 0755)

$recursive : bool = true

Create parent directories if needed (default true)

Tags
inheritDoc
Return values
bool

True on success, false on failure

readFile()

Read entire file contents

public readFile(string $path) : string|false
Parameters
$path : string

File path

Tags
inheritDoc
Return values
string|false

File contents or false on failure

realpath()

Get absolute path (resolve symlinks, relative paths)

public realpath(string $path) : string|false
Parameters
$path : string

Path to resolve

Tags
inheritDoc
Return values
string|false

Resolved absolute path or false on failure

removeDirectory()

Remove a directory from the virtual file system

public removeDirectory(string $path) : void
Parameters
$path : string

Directory path

removeFile()

Remove a file from the virtual file system

public removeFile(string $path) : void
Parameters
$path : string

File path

rmdir()

Remove a directory

public rmdir(string $path) : bool
Parameters
$path : string

Directory path

Tags
inheritDoc
Return values
bool

True on success, false on failure

scandir()

List files and directories in a directory

public scandir(string $path) : array<string|int, mixed>|false
Parameters
$path : string

Directory path

Tags
inheritDoc
Return values
array<string|int, mixed>|false

Array of filenames or false on failure

setCwd()

Set the current working directory

public setCwd(string $path) : void
Parameters
$path : string

Directory path

Delete a file

public unlink(string $path) : bool
Parameters
$path : string

File path

Tags
inheritDoc
Return values
bool

True on success, false on failure

writeFile()

Write data to file

public writeFile(string $path, string $data) : int|false
Parameters
$path : string

File path

$data : string

Data to write

Tags
inheritDoc
Return values
int|false

Number of bytes written or false on failure

globToRegex()

Convert glob pattern to regex pattern

private globToRegex(string $pattern) : string
Parameters
$pattern : string

Glob pattern (e.g., "/path/*.txt")

Return values
string

Regex pattern


        
On this page

Search results