Skip to content

Instantly share code, notes, and snippets.

@mdboom
Forked from filips123/evaluate-php.js
Last active March 18, 2019 12:36
Show Gist options
  • Save mdboom/213e71d37a4b26c01f80e8db41b68dfe to your computer and use it in GitHub Desktop.
Save mdboom/213e71d37a4b26c01f80e8db41b68dfe to your computer and use it in GitHub Desktop.
Iodide with PHP
const PHP_MODULE = 'https://oraoto.github.io/pib/php.js'
const loadResource = url => new Promise((resolve) => {
const head = document.getElementsByTagName('head')[0]
const theScript = document.createElement('script')
console.log("window.PHP " + window.PHP)
theScript.src = url;
theScript.onload = () => {
resolve()
};
head.appendChild(theScript)
})
loadResource(PHP_MODULE)
.then(() => {
window.php = {}
window.php.evaluate = code => {
code = code.replace(/^\s*<\?php/, '')
code = code + '\necho PHP_EOL;'
let ret = module.ccall('pib_eval', 'number', ['string'], [code])
}
window.php.export = (...arguments) => {
if (arguments.length > 1) {
text = Array.prototype.slice.call(arguments).join(' ')
} else {
text = arguments
}
const elem = window.iodide.output.element('div')
elem.innerHTML = text
}
const options = {
print: window.php.export,
printErr: window.php.export,
locateFile: (path) => 'https://oraoto.github.io/pib/' + path
}
const module = PHP(options)
window.iodide.exportPHP = (variableName, variable) => {
const out = variable
window[variableName] = out;
}
window.iodide.addOutputHandler({
shouldHandle: val => true,
render: (val) => {
return val.toString()
},
});
})
%% plugin
{
"languageId": "php",
"displayName": "PHP",
"codeMirrorMode": "php",
"keybinding": "php",
"url": "https://gist.githubusercontent.com/mdboom/213e71d37a4b26c01f80e8db41b68dfe/raw/8d348f670d670b9bdcd8e486f192cffda65ff0c6/evaluate-php.js",
"module": "php",
"evaluator": "evaluate",
"pluginType": "language"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment