• Stars
    star
    299
  • Rank 139,269 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Virtual File System

VFS (Virtual File System)

Virtual File System

Master branch build status Published version PHP ~5.4 MIT Licensed

VFS is a virtual file system for PHP built using the stream wrapper API. Streams are exposed just as typical file:// or http:// streams are to PHP's built-in functions and keywords like fopen and require. This implementation attempts to stay true to the typical streams, including triggering warnings and handling edge cases appropriately.

It can be installed in whichever way you prefer, but I recommend Composer.

{
    "require": {
        "adlawson/vfs": "*"
    }
}

Documentation

After creating and mounting the file system, you have the option of manipulating the virtual file system either via PHP's built-in functions, the VFS interfaces, or interfaces provided by another file system library.

<?php
use Vfs\FileSystem;
use Vfs\Node\Directory;
use Vfs\Node\File;

// Create and mount the file system
$fs = FileSystem::factory('vfs://');
$fs->mount();

// Add `/foo` and `/foo/bar.txt`
$foo = new Directory(['bar.txt' => new File('Hello, World!')]);
$fs->get('/')->add('foo', $foo);

// Get contents of `/foo/bar.txt`
$fs->get('/foo/bar.txt')->getContent(); // Hello, World!
file_get_contents('vfs://foo/bar.txt'); // Hello, World!

// Add `/foo/bar` and `/foo/bar/baz.php`
mkdir('vfs://foo/bar');
file_put_contents('vfs://foo/bar.php', '<?php echo "Hello, World!";');

// Require `/foo/bar.php`
require 'vfs://foo/baz.php';

// Works with any other file system library too
$symfony = new Symfony\Component\Filesystem\Filesystem();
$symfony->mkdir('vfs://foo/bar/baz');
$laravel = new Illuminate\Filesystem();
$laravel->isDirectory('vfs://foo/bar/baz'); //true

// Triggers PHP warnings on error just like typical streams
rename('vfs://path/to/nowhere', 'vfs://path/to/somewhere');
// PHP Warning: rename(vfs://path/to/nowhere,vfs://path/to/somewhere): No such file or directory in /srv/index.php on line 1; triggered in /srv/src/Logger/PhpErrorLogger.php on line 32

Example use cases

If you need to ask what you'd use a virtual file system for, you probably don't need one, but just in case, I've compiled a small list of examples:

  • Testing file system libraries without writing to disc
  • Runtime evaluation without eval (via write and require)
  • ...we need some more!

Todo

Current tasks are listed on the github issues page, but some are listed here for reference:

  • Symlinks
  • File locks
  • Permissions/ACL

Contributing

Contributions are accepted via Pull Request, but passing unit tests must be included before it will be considered for merge.

$ curl -O https://raw.githubusercontent.com/adlawson/vagrantfiles/master/php/Vagrantfile
$ vagrant up
$ vagrant ssh
...

$ cd /srv
$ composer install
$ vendor/bin/phpunit

License

The content of this library is released under the MIT License by Andrew Lawson.
You can find a copy of this license in LICENSE or at http://opensource.org/licenses/mit.

More Repositories

1

vagrantfiles

A Virtual Machine for every language
Ruby
230
star
2

nodejs-langs

ISO 639-1/2/3 Language codes with English and local names
JavaScript
114
star
3

search-algorithms

Search algorithms implemented in different languages
Elixir
71
star
4

mixcloud-tracklist

[Unofficial] Display tracklists on Mixcloud
JavaScript
48
star
5

sweetjs-pipeline

Naturally expressive functional composition
JavaScript
26
star
6

nodejs-wesley

🚨 [UNMAINTAINED] Protocol compliant web socket server with some awesome extras.
JavaScript
23
star
7

nodejs-urine

Sample a stream
JavaScript
9
star
8

docker-dat

Dat server in Docker
Shell
9
star
9

nodejs-throb

🚨 [UNMAINTAINED] Lightweight heartbeat and ping server
JavaScript
7
star
10

php-veval

Virtual metaprogramming
PHP
6
star
11

scala-cats

Cats extended
Scala
5
star
12

js-defur

🚨 [UNMAINTAINED] Defer construction or execution of a service until it's needed
JavaScript
4
star
13

php-mustache

🚨 [UNMAINTAINED] PHP5.3+ Mustache implementation. Still currently in development.
PHP
4
star
14

coffee-key

🚨 [UNMAINTAINED] A tiny little keycode library
CoffeeScript
4
star
15

php-timezone

UTC default timezone
PHP
3
star
16

elixir-pnum

Concurrent list enumeration
Elixir
2
star
17

php-restack

🚨 [UNMAINTAINED] PHP 5.3+ datastructure library
PHP
2
star
18

adlawson.github.io

Website
HTML
2
star
19

dotfiles

~
Emacs Lisp
2
star
20

dockerfiles

🐳
Dockerfile
2
star
21

scala-json4s

Serializers, formatters and other useful extensions for JSON4S in Scala
Scala
2
star
22

behat-parallel

Parallel Behat processes
Shell
2
star
23

nodejs-conway

Rendering agnostic implementation of Conway's Game of Life in JavaScript
JavaScript
2
star
24

php-reform

🚨 [UNMAINTAINED] PHP 5.3+ form library
PHP
1
star
25

js-zcombinator

Z Combinator for self-referencing anonymous functions
JavaScript
1
star
26

howlonguntilourholiday.com

🚨 [UNMAINTAINED] How long until our holiday?
CoffeeScript
1
star