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.ILaunchManager;
17 import org.eclipse.debug.core.Launch;
18 import org.eclipse.debug.core.model.IProcess;
20 public class InterpreterRunner {
22 public InterpreterRunner() {
25 public IProcess run(InterpreterRunnerConfiguration configuration, ILaunch launch) {
26 String commandLine = renderCommandLine(configuration);
27 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
29 setEnvironmentVariables(configuration);
30 String[] env = configuration.getEnvironment();
31 Process nativePHPProcess = null;
33 nativePHPProcess = configuration.getInterpreter().exec(commandLine, workingDirectory, env);
34 } catch (IOException e) {
35 throw new RuntimeException("Unable to execute interpreter: " + commandLine + workingDirectory);
38 IProcess process = DebugPlugin.newProcess(launch, nativePHPProcess, renderLabel(configuration));
39 process.setAttribute(PHPLaunchingPlugin.PLUGIN_ID + ".launcher.cmdline", commandLine);
40 process.setAttribute(IProcess.ATTR_PROCESS_TYPE, PHPLaunchConfigurationAttribute.PHP_LAUNCH_PROCESS_TYPE);
45 protected String renderLabel(InterpreterRunnerConfiguration configuration) {
46 StringBuffer buffer = new StringBuffer();
48 PHPInterpreter interpreter = configuration.getInterpreter();
49 buffer.append("PHP ");
50 buffer.append(interpreter.getCommand());
52 buffer.append(configuration.getFileName());
54 return buffer.toString();
57 protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
58 PHPInterpreter interpreter = configuration.getInterpreter();
60 StringBuffer buffer = new StringBuffer();
61 buffer.append(this.getDebugCommandLineArgument());
62 // buffer.append(renderLoadPath(configuration));
63 buffer.append(" " + configuration.getInterpreterArguments());
64 // buffer.append(interpreter.endOfOptionsDelimeter);
65 buffer.append(" " + osDependentPath(configuration.getAbsoluteFileName()));
66 buffer.append(" " + configuration.getProgramArguments());
68 return buffer.toString();
71 protected void setEnvironmentVariables(InterpreterRunnerConfiguration configuration) {
72 IPath FilePath= new Path(configuration.getAbsoluteFileName());
73 String OSFilePath= FilePath.toOSString();
74 configuration.addEnvironmentValue("REDIRECT_URL",OSFilePath,true);
75 configuration.addEnvironmentValue("REQUEST_URI",OSFilePath,true);
76 configuration.addEnvironmentValue("PATH_INFO",OSFilePath,true);
77 configuration.addEnvironmentValue("PATH_TRANSLATED",OSFilePath,true);
78 configuration.addEnvironmentValue("SCRIPT_FILENAME",configuration.getInterpreter().getCommand(),true);
79 configuration.addEnvironmentValue("SERVER_PROTOCOL","HTTP / 1.1",true);
81 configuration.addEnvironmentValue("REDIRECT_QUERY_STRING","",true);
82 configuration.addEnvironmentValue("REDIRECT_STATUS","200",true);
83 configuration.addEnvironmentValue("SERVER_SOFTWARE","DBG / 2.1",true);
84 configuration.addEnvironmentValue("SERVER_NAME","localhost",true);
85 configuration.addEnvironmentValue("SERVER_ADDR","127.0.0.1",true);
86 configuration.addEnvironmentValue("SERVER_PORT","80",true);
87 configuration.addEnvironmentValue("REMOTE_ADDR","127.0.0.1",true);
89 configuration.addEnvironmentValue("GATEWAY_INTERFACE","CGI / 1.1",true);
90 configuration.addEnvironmentValue("REQUEST_METHOD","GET",true);
92 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
93 if (stringVars.containsKey("SYSTEMROOT"))
94 configuration.addEnvironmentValue("SYSTEMROOT",(String) stringVars.get("SYSTEMROOT"),true);
98 protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
99 StringBuffer loadPath = new StringBuffer();
101 JavaProject project = configuration.getProject();
102 addToLoadPath(loadPath, project.getProject());
104 Iterator referencedProjects = project.getReferencedProjects().iterator();
105 while (referencedProjects.hasNext())
106 addToLoadPath(loadPath, (IProject) referencedProjects.next());
108 return loadPath.toString();
111 protected void addToLoadPath(StringBuffer loadPath, IProject project) {
112 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
115 protected String osDependentPath(String aPath) {
116 if (Platform.getOS().equals(Platform.OS_WIN32))
117 aPath = "\"" + aPath + "\"";
122 protected String getDebugCommandLineArgument() {