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,
25 String commandLine = renderCommandLine(configuration);
26 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
28 setEnvironmentVariables(configuration);
29 String[] env = configuration.getEnvironment();
30 Process nativePHPProcess = null;
32 nativePHPProcess = configuration.getInterpreter().exec(commandLine,
33 workingDirectory, env);
34 } catch (IOException e) {
35 throw new RuntimeException("Unable to execute interpreter: "
36 + commandLine + workingDirectory);
39 IProcess process = DebugPlugin.newProcess(launch, nativePHPProcess,
40 renderLabel(configuration));
42 .setAttribute(PHPLaunchingPlugin.PLUGIN_ID
43 + ".launcher.cmdline", commandLine);
44 process.setAttribute(IProcess.ATTR_PROCESS_TYPE,
45 PHPLaunchConfigurationAttribute.PHP_LAUNCH_PROCESS_TYPE);
50 protected String renderLabel(InterpreterRunnerConfiguration configuration) {
51 StringBuffer buffer = new StringBuffer();
53 PHPInterpreter interpreter = configuration.getInterpreter();
54 buffer.append("PHP ");
55 buffer.append(interpreter.getCommand());
57 buffer.append(configuration.getFileName());
59 return buffer.toString();
62 protected String renderCommandLine(
63 InterpreterRunnerConfiguration configuration) {
64 PHPInterpreter interpreter = configuration.getInterpreter();
66 StringBuffer buffer = new StringBuffer();
67 buffer.append(this.getDebugCommandLineArgument());
68 // buffer.append(renderLoadPath(configuration));
69 buffer.append(" " + configuration.getInterpreterArguments());
70 // buffer.append(interpreter.endOfOptionsDelimeter);
72 + osDependentPath(configuration.getAbsoluteFileName()));
73 buffer.append(" " + configuration.getProgramArguments());
75 return buffer.toString();
78 protected void setEnvironmentVariables(
79 InterpreterRunnerConfiguration configuration) {
80 IPath FilePath = new Path(configuration.getAbsoluteFileName());
81 String OSFilePath = FilePath.toOSString();
82 configuration.addEnvironmentValue("REDIRECT_URL", OSFilePath, true);
83 configuration.addEnvironmentValue("REQUEST_URI", OSFilePath, true);
84 configuration.addEnvironmentValue("PATH_INFO", OSFilePath, true);
85 configuration.addEnvironmentValue("PATH_TRANSLATED", OSFilePath, true);
86 configuration.addEnvironmentValue("SCRIPT_FILENAME", configuration
87 .getInterpreter().getCommand(), true);
89 .addEnvironmentValue("SERVER_PROTOCOL", "HTTP / 1.1", true);
91 configuration.addEnvironmentValue("REDIRECT_QUERY_STRING", "", true);
92 configuration.addEnvironmentValue("REDIRECT_STATUS", "200", true);
93 configuration.addEnvironmentValue("SERVER_SOFTWARE", "DBG / 2.1", true);
94 configuration.addEnvironmentValue("SERVER_NAME", "localhost", true);
95 configuration.addEnvironmentValue("SERVER_ADDR", "127.0.0.1", true);
96 configuration.addEnvironmentValue("SERVER_PORT", "80", true);
97 configuration.addEnvironmentValue("REMOTE_ADDR", "127.0.0.1", true);
99 configuration.addEnvironmentValue("GATEWAY_INTERFACE", "CGI / 1.1",
101 configuration.addEnvironmentValue("REQUEST_METHOD", "GET", true);
103 Map stringVars = DebugPlugin.getDefault().getLaunchManager()
104 .getNativeEnvironment();
105 if (stringVars.containsKey("SYSTEMROOT"))
106 configuration.addEnvironmentValue("SYSTEMROOT", (String) stringVars
107 .get("SYSTEMROOT"), true);
111 protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
112 StringBuffer loadPath = new StringBuffer();
114 JavaProject project = configuration.getProject();
115 addToLoadPath(loadPath, project.getProject());
117 Iterator referencedProjects = project.getReferencedProjects()
119 while (referencedProjects.hasNext())
120 addToLoadPath(loadPath, (IProject) referencedProjects.next());
122 return loadPath.toString();
125 protected void addToLoadPath(StringBuffer loadPath, IProject project) {
126 loadPath.append(" -I "
127 + osDependentPath(project.getFullPath().toOSString()));
130 protected String osDependentPath(String aPath) {
131 if (Platform.getOS().equals(Platform.OS_WIN32))
132 aPath = "\"" + aPath + "\"";
137 protected String getDebugCommandLineArgument() {