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": "*"
}
}
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
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');
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
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.