I'm trying to run code through Node.js from PHP without touching the file system (the disk). Is this possible?
What I'm doing
<?php
function renderJs($script) {
$path = __DIR__.'/'.md5($script).'.js';
file_put_contents($path, $script);
$rendered = `node $path`;
unlink($path);
return $rendered;
}
$val = renderJs('console.log(1+1)'); // 2
What I'd like to avoid
- Having to save the file to disk
- Installing additional PHP extensions (such as V8js)
Ideally, it'd be something like
$magicBlob = php_magic($script);
$rendered = `node $magicBlob`;
I can't just eval it as in
$rendered = `node --eval "$script"`;
because the script might have quotes that would break my outer quoting.