replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / InterpreterRunner.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.Map;
7
8 import net.sourceforge.phpdt.internal.core.JavaProject;
9
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;
17
18 public class InterpreterRunner {
19
20         public InterpreterRunner() {
21         }
22
23         public IProcess run(InterpreterRunnerConfiguration configuration, ILaunch launch) {
24                 String commandLine = renderCommandLine(configuration);
25                 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
26
27                 setEnvironmentVariables(configuration);
28                 String[] env = configuration.getEnvironment();
29                 Process nativePHPProcess = null;
30                 try {
31                         nativePHPProcess = configuration.getInterpreter().exec(commandLine, workingDirectory, env);
32                 } catch (IOException e) {
33                         throw new RuntimeException("Unable to execute interpreter: " + commandLine + workingDirectory);
34                 }
35
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);
39
40                 return process ;
41         }
42
43         protected String renderLabel(InterpreterRunnerConfiguration configuration) {
44                 StringBuffer buffer = new StringBuffer();
45
46                 PHPInterpreter interpreter = configuration.getInterpreter();
47                 buffer.append("PHP ");
48                 buffer.append(interpreter.getCommand());
49                 buffer.append(" : ");
50                 buffer.append(configuration.getFileName());
51
52                 return buffer.toString();
53         }
54
55         protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
56                 PHPInterpreter interpreter = configuration.getInterpreter();
57
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());
65
66                 return buffer.toString();
67         }
68
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);
78
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);
86
87                 configuration.addEnvironmentValue("GATEWAY_INTERFACE","CGI / 1.1",true);
88                 configuration.addEnvironmentValue("REQUEST_METHOD","GET",true);
89
90                 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
91                 if (stringVars.containsKey("SYSTEMROOT"))
92                         configuration.addEnvironmentValue("SYSTEMROOT",(String) stringVars.get("SYSTEMROOT"),true);
93
94         }
95
96         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
97                 StringBuffer loadPath = new StringBuffer();
98
99                 JavaProject project = configuration.getProject();
100                 addToLoadPath(loadPath, project.getProject());
101
102                 Iterator referencedProjects = project.getReferencedProjects().iterator();
103                 while (referencedProjects.hasNext())
104                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
105
106                 return loadPath.toString();
107         }
108
109         protected void addToLoadPath(StringBuffer loadPath, IProject project) {
110                 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
111         }
112
113         protected String osDependentPath(String aPath) {
114                 if (Platform.getOS().equals(Platform.OS_WIN32))
115                         aPath = "\"" + aPath + "\"";
116
117                 return aPath;
118         }
119
120         protected String getDebugCommandLineArgument() {
121                 return "" ;
122         }
123 }