php.java.script.servlet
Class InvocablePhpServletScriptEngine
java.lang.Object
javax.script.AbstractScriptEngine
php.java.script.SimplePhpScriptEngine
php.java.script.InvocablePhpScriptEngine
php.java.script.servlet.InvocablePhpServletLocalHttpServerScriptEngine
php.java.script.servlet.InvocablePhpServletScriptEngine
- All Implemented Interfaces:
- java.io.Closeable, Invocable, ScriptEngine
public final class InvocablePhpServletScriptEngine
- extends InvocablePhpServletLocalHttpServerScriptEngine
A PHP script engine which implements the Invocable interface for Servlets. See ContextLoaderListener for details.
PHP scripts are evaluated as follows:
- "http://127.0.0.1:CURRENT_PORT/CURRENT_WEBAPP/java/JavaProxy.php" is requested from Java
- Your script is evaluated
- <?php java_context()->call(java_closure());?> is called in order to make the script invocable
In order to evaluate PHP methods follow these steps:
- Create a factory which creates a PHP script file from a reader using the methods from
EngineFactory:
private static File script;
private static final File getScriptF() {
if (script!=null) return script;
String webCacheDir = ctx.getRealPath(req.getServletPath());
Reader reader = new StringReader ("<?php function f($v) {return "passed:".$v;} ?>");
return EngineFactory.getPhpScript(webCacheDir, reader);
}
- Acquire a PHP invocable script engine from the
EngineFactory:
ScriptEngine scriptEngine = EngineFactory.getInvocablePhpScriptEngine(this, ctx, req, res);
- Create a FileReader for the created script file:
Reader readerF = EngineFactory.createPhpScriptFileReader(getScriptF());
- Evaluate the engine:
scriptEngine.eval(readerF);
- Close the reader obtained from the
EngineFactory:
readerF.close();
- Cast the engine to Invocable:
Invocable invocableEngine = (Invocable)scriptEngine;
- Call PHP functions or methods:
System.out.println("result from PHP:" + invocableEngine.invoceFunction(f, new Object[]{"arg1"}));
- Release the invocable:
((Closeable)scriptEngine).close();
Alternatively one may use the following "quick and dirty" code which creates a new PHP script for
each eval and removes it when the invocable is released:
ScriptEngine e = EngineFactory.getInvocablePhpScriptEngine(this, ctx, req, res);
e.eval("<?php function f($v) {return "passed:".$v;} ?>");
((Invocable)e).invoceFunction("f", new Object[]{"arg1"};
((Closeable)e).close();
|
Constructor Summary |
InvocablePhpServletScriptEngine(javax.servlet.Servlet servlet,
javax.servlet.ServletContext ctx,
javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res,
java.lang.String protocol,
int port)
|
|
Method Summary |
void |
release()
Release the continuation. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
InvocablePhpServletScriptEngine
public InvocablePhpServletScriptEngine(javax.servlet.Servlet servlet,
javax.servlet.ServletContext ctx,
javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res,
java.lang.String protocol,
int port)
throws java.net.MalformedURLException,
java.net.URISyntaxException
- Throws:
java.net.MalformedURLException
java.net.URISyntaxException
release
public void release()
- Release the continuation. Must be called explicitly or
via the
RequestListener
- Overrides:
release in class InvocablePhpScriptEngine