PHP code example of patterns / filesystem

1. Go to this page and download the library: Download patterns/filesystem library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

patterns / filesystem example snippets


namespace Patterns;

interface IFilesystem
{
    public function exists(string $path): bool;
    public function readFile(string $path): string;
    public function writeFile(string $path, string $data): void;
    public function deleteFile(string $path): void;
    public function readDir(string $path): array;      // list<string>, names not paths
    public function ensureDir(string $path): void;
    public function deleteDir(string $path): void;
}

namespace Patterns;

interface IStatable
{
    public function stat(string $path): FileStats;
}

use Patterns\IStatable;

if ($backend instanceof IStatable) {
    $size = $backend->stat('reports/2026.json')->size();
}

$stats = FileStats::create([
    'path'     => 'reports/2026.json',
    'size'     => 18432,
    'modified' => '2026-10-06T08:00:00+00:00',
    'isDir'    => false,
]);

$stats->path();               // 'reports/2026.json'
$stats->size();               // 18432
$stats->modified();           // DateTimeImmutable
$stats->isDir();              // false
$stats->toArray();            // back to the array above

namespace App\Storage;

use Patterns\IFilesystem;
use RuntimeException;

final class MemoryFilesystem implements IFilesystem
{
    /** @var array<string, string> */
    private array $files = [];

    public function exists(string $path): bool
    {
        return array_key_exists($path, $this->files);
    }

    public function readFile(string $path): string
    {
        return $this->files[$path] ?? throw new RuntimeException('no such file: ' . $path);
    }

    public function writeFile(string $path, string $data): void
    {
        $this->files[$path] = $data;
    }

    public function deleteFile(string $path): void
    {
        unset($this->files[$path]);
    }

    public function readDir(string $path): array
    {
        $prefix = $path === '' || str_ends_with($path, '/') ? $path : $path . '/';
        $entries = [];

        foreach (array_keys($this->files) as $key) {
            if (!str_starts_with($key, $prefix)) {
                continue;
            }

            $entries[] = explode('/', substr($key, strlen($prefix)))[0];
        }

        $entries = array_values(array_unique($entries));
        sort($entries);

        return $entries;
    }

    public function ensureDir(string $path): void
    {
        // Nothing to do: a key's hierarchy exists because the key does.
    }

    public function deleteDir(string $path): void
    {
        $prefix = $path === '' || str_ends_with($path, '/') ? $path : $path . '/';

        foreach (array_keys($this->files) as $key) {
            if (str_starts_with($key, $prefix)) {
                unset($this->files[$key]);
            }
        }
    }
}