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);
38 process.setAttribute(IProcess.ATTR_PROCESS_TYPE, PHPLaunchConfigurationAttribute.PHP_LAUNCH_PROCESS_TYPE);
43 protected String renderLabel(InterpreterRunnerConfiguration configuration) {
44 StringBuffer buffer = new StringBuffer();
46 PHPInterpreter interpreter = configuration.getInterpreter();
47 buffer.append("PHP ");
48 buffer.append(interpreter.getCommand());
50 buffer.append(configuration.getFileName());
52 return buffer.toString();
55 protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
56 PHPInterpreter interpreter = configuration.getInterpreter();
58 StringBuffer buffer = new StringBuffer();
59 buffer.append(this.getDebugCommandLineArgument());
60 // buffer.append(renderLoadPath(configuration));
61 buffer.append(" " + configuration.getInterpreterArguments());
62 // buffer.append(interpreter.endOfOptionsDelimeter);
63 buffer.append(" " + osDependentPath(configuration.getAbsoluteFileName()));
64 buffer.append(" " + configuration.getProgramArguments());
66 return buffer.toString();
69 protected void setEnvironmentVariables(InterpreterRunnerConfiguration configuration) {
70 IPath FilePath= new Path(configuration.getAbsoluteFileName());
71 String OSFilePath= FilePath.toOSString();
72 configuration.addEnvironmentValue("REDIRECT_URL",OSFilePath,true);
73 configuration.addEnvironmentValue("REQUEST_URI",OSFilePath,true);
74 configuration.addEnvironmentValue("PATH_INFO",OSFilePath,true);
75 configuration.addEnvironmentValue("PATH_TRANSLATED",OSFilePath,true);
76 configuration.addEnvironmentValue("SCRIPT_FILENAME",configuration.getInterpreter().getCommand(),true);
77 configuration.addEnvironmentValue("SERVER_PROTOCOL","HTTP / 1.1",true);
79 configuration.addEnvironmentValue("REDIRECT_QUERY_STRING","",true);
80 configuration.addEnvironmentValue("REDIRECT_STATUS","200",true);
81 configuration.addEnvironmentValue("SERVER_SOFTWARE","DBG / 2.1",true);
82 configuration.addEnvironmentValue("SERVER_NAME","localhost",true);
83 configuration.addEnvironmentValue("SERVER_ADDR","127.0.0.1",true);
84 configuration.addEnvironmentValue("SERVER_PORT","80",true);
85 configuration.addEnvironmentValue("REMOTE_ADDR","127.0.0.1",true);
87 configuration.addEnvironmentValue("GATEWAY_INTERFACE","CGI / 1.1",true);
88 configuration.addEnvironmentValue("REQUEST_METHOD","GET",true);
90 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
91 if (stringVars.containsKey("SYSTEMROOT"))
92 configuration.addEnvironmentValue("SYSTEMROOT",(String) stringVars.get("SYSTEMROOT"),true);
96 protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
97 StringBuffer loadPath = new StringBuffer();
99 JavaProject project = configuration.getProject();
100 addToLoadPath(loadPath, project.getProject());
102 Iterator referencedProjects = project.getReferencedProjects().iterator();
103 while (referencedProjects.hasNext())
104 addToLoadPath(loadPath, (IProject) referencedProjects.next());
106 return loadPath.toString();
109 protected void addToLoadPath(StringBuffer loadPath, IProject project) {
110 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
113 protected String osDependentPath(String aPath) {
114 if (Platform.getOS().equals(Platform.OS_WIN32))
115 aPath = "\"" + aPath + "\"";
120 protected String getDebugCommandLineArgument() {