/php-veval

Virtual metaprogramming

Primary LanguagePHPMIT LicenseMIT

Veval (Virtual eval)

Virtual Eval

Master branch build status Published version PHP ~5.4 MIT Licensed

Veval is an implementation of eval that uses a virtual file system to store code for evaluation and then require it back out. It should work on PHP installations that don't support or have disabled native eval.

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

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

Documentation

The Veval API is exposed as a collection of Veval\ namespaced functions, though you may prefer to use Veval:: to take advantage of autoloading and namespace aliasing (PHP < 5.6).

<?php

// Evaluate some code
Veval\execute(<<<'EOF'
class Foo {
    public $name;
    public function __construct($name) {$this->name = $name;}
}
EOF
);

// Use your newly evaulated code
$foo = new Foo('bar');
$foo->name; // bar

Debugging

Storing all of the "files" in memory is all fine if your evaluated code is workng as you expect, but sometimes it's useful to read the generated code to debug any problems. There are a few different debugging functions available to suit your needs.

<?php

// Debug all evaluated strings
Veval\debug(function ($name, $content) {
    // Debug some things here
});

// Iterate over all evaulated strings
foreach (Veval\iterator() as $name => $content) {
    // Debug some things here
}

// Dump all to path
Veval\dump(sys_get_temp_dir(), 'veval-%s.php');

Warning

Eval Warning

Using **Veval**, just like eval, is considered dangerous to use if you're evaluating user input. Always be careful not to do this as it can open up quite a large hole in the security of your system.

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.