1 package net.sourceforge.phpdt.internal.launching;
4 import java.io.IOException;
5 import java.util.Iterator;
8 import net.sourceforge.phpdt.internal.core.JavaProject;
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.Path;
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.debug.core.DebugPlugin;
15 import org.eclipse.debug.core.ILaunch;
16 import org.eclipse.debug.core.model.IProcess;
18 public class InterpreterRunner {
20 public InterpreterRunner() {
23 public IProcess run(InterpreterRunnerConfiguration configuration, ILaunch launch) {
24 String commandLine = renderCommandLine(configuration);
25 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
27 setEnvironmentVariables(configuration);
28 String[] env = configuration.getEnvironment();
29 Process nativePHPProcess = null;
31 nativePHPProcess = configuration.getInterpreter().exec(commandLine, workingDirectory, env);
32 } catch (IOException e) {
33 throw new RuntimeException("Unable to execute interpreter: " + commandLine + workingDirectory);
36 IProcess process = DebugPlugin.newProcess(launch, nativePHPProcess, renderLabel(configuration));
37 process.setAttribute(PHPLaunchingPlugin.PLUGIN_ID + ".launcher.cmdline", commandLine);
42 protected String renderLabel(InterpreterRunnerConfiguration configuration) {
43 StringBuffer buffer = new StringBuffer();
45 PHPInterpreter interpreter = configuration.getInterpreter();
46 buffer.append("PHP ");
47 buffer.append(interpreter.getCommand());
49 buffer.append(configuration.getFileName());
51 return buffer.toString();
54 protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
55 PHPInterpreter interpreter = configuration.getInterpreter();
57 StringBuffer buffer = new StringBuffer();
58 buffer.append(this.getDebugCommandLineArgument());
59 // buffer.append(renderLoadPath(configuration));
60 buffer.append(" " + configuration.getInterpreterArguments());
61 // buffer.append(interpreter.endOfOptionsDelimeter);
62 buffer.append(" " + osDependentPath(configuration.getAbsoluteFileName()));
63 buffer.append(" " + configuration.getProgramArguments());
65 return buffer.toString();
68 protected void setEnvironmentVariables(InterpreterRunnerConfiguration configuration) {
69 IPath FilePath= new Path(configuration.getAbsoluteFileName());
70 String OSFilePath= FilePath.toOSString();
71 configuration.addEnvironmentValue("REDIRECT_URL",OSFilePath,true);
72 configuration.addEnvironmentValue("REQUEST_URI",OSFilePath,true);
73 configuration.addEnvironmentValue("PATH_INFO",OSFilePath,true);
74 configuration.addEnvironmentValue("PATH_TRANSLATED",OSFilePath,true);
75 configuration.addEnvironmentValue("SCRIPT_FILENAME",configuration.getInterpreter().getCommand(),true);
76 configuration.addEnvironmentValue("SERVER_PROTOCOL","HTTP / 1.1",true);
78 configuration.addEnvironmentValue("REDIRECT_QUERY_STRING","",true);
79 configuration.addEnvironmentValue("REDIRECT_STATUS","200",true);
80 configuration.addEnvironmentValue("SERVER_SOFTWARE","DBG / 2.1",true);
81 configuration.addEnvironmentValue("SERVER_NAME","localhost",true);
82 configuration.addEnvironmentValue("SERVER_ADDR","127.0.0.1",true);
83 configuration.addEnvironmentValue("SERVER_PORT","80",true);
84 configuration.addEnvironmentValue("REMOTE_ADDR","127.0.0.1",true);
86 configuration.addEnvironmentValue("GATEWAY_INTERFACE","CGI / 1.1",true);
87 configuration.addEnvironmentValue("REQUEST_METHOD","GET",true);
89 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
90 if (stringVars.containsKey("SYSTEMROOT"))
91 configuration.addEnvironmentValue("SYSTEMROOT",(String) stringVars.get("SYSTEMROOT"),true);
95 protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
96 StringBuffer loadPath = new StringBuffer();
98 JavaProject project = configuration.getProject();
99 addToLoadPath(loadPath, project.getProject());
101 Iterator referencedProjects = project.getReferencedProjects().iterator();
102 while (referencedProjects.hasNext())
103 addToLoadPath(loadPath, (IProject) referencedProjects.next());
105 return loadPath.toString();
108 protected void addToLoadPath(StringBuffer loadPath, IProject project) {
109 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
112 protected String osDependentPath(String aPath) {
113 if (Platform.getOS().equals(Platform.OS_WIN32))
114 aPath = "\"" + aPath + "\"";
119 protected String getDebugCommandLineArgument() {