asmblah / uniter

🎉 PHP in the browser and Node.js => Docs: https://phptojs.com/
https://asmblah.github.io/uniter/
Other
446 stars 42 forks source link

Enhancement to the include transport #26

Closed IngwiePhoenix closed 8 years ago

IngwiePhoenix commented 8 years ago

So originally, the function that returns/resolves the result of an include or require statement was supposed to be either a wrapper function for the new compiled PHP code or the already compiled code.

Since we are urging the compiler itself out of the runtime, we will only be able to return the transpield PHP source as returned by phpToJS. However, what if we want to actually return a value itself? Say I want to tweak the require keyword to return node modules, I'd like to do something such as

include: function(path, promise) {
    // ... snip ...
    var module = require(...);
    promise.resolve( ValueFactory.createValue(module) );
}

I believe this should be possible.

asmblah commented 8 years ago

Hi @IngwiePhoenix,

Nice suggestion! I've now added this to PHPCore v3.5.0/Uniter v2.6.0. I'm now also passing the ValueFactory instance through as the fourth argument to the include transport, so you can use it like so:

phpEngine.configure({
    include: function (path, promise, callerPath, valueFactory) {
        promise.resolve(valueFactory.coerce(21)); // Or .createInteger(...)/.createFloat(...), etc.
    }
});

Cheers, Dan

IngwiePhoenix commented 8 years ago

Amazing!

And with that, I should be all up and ready to port my backend code over, completely. I'll introduce a proper transport to uniter-node and see how it goes. When that one works as expected, ill send a PR to introduce a register script to Uniter, which behaves like babelo's registrar or CoffeeScript - but for PHP instead :).

Thanks for implementing that stuff! (: