What frameworks/tools are there to help run javascript from PHP? Is there anything like the harmony project for PHP? I am hoping to perform JS unit tests (or 开发者_如何学Pythoneven better, BDD) directly from PHP, inspired by this post (for Ruby). Am I asking for too much?
There is in fact the Spidermonkey PECL extension, which embeds THE mozilla Javascript interpreter in PHP. It will however not provide the document.whatever
object tree that browsers have. So I'm not sure which kind of JS unit tests you could possibly accomplish with this.
Maybe you can utilize env.js and co like that Ruby project does. But I'm unware if a pre-made setup or framework for such purposes exists (most likely not). So much for the unconclusive answer.
If you just want to probe the user interface with jQuery-like features, then phpQuery might be an option.
If you need to run some Javascript code from PHP, on the server, the spidermonkey extension might be what you are looking for (quoting) :
This extension allow you to embed Mozilla's Javascript engine Spidermonkey in PHP.
I've used it -- for fun -- a couple of times, and it was working not too bad ; but note I have never used it in a production environment (and know no-one who do).
You should give mozilla's Rhino a try, if you want server-side execution of javascript. It is a sister project to spidermonkey, written in Java. It was designed to be used in cases where you want syntactically valid client code to run on the server (and, fyi, provides the foundation for google's closure compiler). It's not an instant solution for javascript-in-php, but as demonstrated here http://ejohn.org/blog/bringing-the-browser-to-the-server/, it can be used for server side testing of client code.
精彩评论