Skip to content

Instantly share code, notes, and snippets.

@filips123
Last active March 18, 2019 12:17
Show Gist options
  • Save filips123/8d6135debb763374fac5078bcee28542 to your computer and use it in GitHub Desktop.
Save filips123/8d6135debb763374fac5078bcee28542 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')
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,
}
const module = PHP(options)
window.iodide.exportPHP = (variableName, variable) => {
const out = variable
window[variableName] = out;
}
window.iodide.addOutputHandler({
shouldHandle: val => true,
render: (val) => {
const elem = document.createElement('div')
elem.innerHTML = val
return elem
},
});
})
%% plugin
{
"languageId": "php",
"displayName": "PHP",
"codeMirrorMode": "php",
"keybinding": "php",
"url": "https://gist.githubusercontent.com/filips123/8d6135debb763374fac5078bcee28542/raw/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