Fix hover functionality.
[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,
24                         ILaunch launch) {
25                 String commandLine = renderCommandLine(configuration);
26                 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
27
28                 setEnvironmentVariables(configuration);
29                 String[] env = configuration.getEnvironment();
30                 Process nativePHPProcess = null;
31                 try {
32                         nativePHPProcess = configuration.getInterpreter().exec(commandLine,
33                                         workingDirectory, env);
34                 } catch (IOException e) {
35                         throw new RuntimeException("Unable to execute interpreter: "
36                                         + commandLine + workingDirectory);
37                 }
38
39                 IProcess process = DebugPlugin.newProcess(launch, nativePHPProcess,
40                                 renderLabel(configuration));
41                 process
42                                 .setAttribute(PHPLaunchingPlugin.PLUGIN_ID
43                                                 + ".launcher.cmdline", commandLine);
44                 process.setAttribute(IProcess.ATTR_PROCESS_TYPE,
45                                 PHPLaunchConfigurationAttribute.PHP_LAUNCH_PROCESS_TYPE);
46
47                 return process;
48         }
49
50         protected String renderLabel(InterpreterRunnerConfiguration configuration) {
51                 StringBuffer buffer = new StringBuffer();
52
53                 PHPInterpreter interpreter = configuration.getInterpreter();
54                 buffer.append("PHP ");
55                 buffer.append(interpreter.getCommand());
56                 buffer.append(" : ");
57                 buffer.append(configuration.getFileName());
58
59                 return buffer.toString();
60         }
61
62         protected String renderCommandLine(
63                         InterpreterRunnerConfiguration configuration) {
64                 //PHPInterpreter interpreter = configuration.getInterpreter();
65
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);
71                 buffer.append(" "
72                                 + osDependentPath(configuration.getAbsoluteFileName()));
73                 buffer.append(" " + configuration.getProgramArguments());
74
75                 return buffer.toString();
76         }
77
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);
88                 configuration
89                                 .addEnvironmentValue("SERVER_PROTOCOL", "HTTP / 1.1", true);
90
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);
98
99                 configuration.addEnvironmentValue("GATEWAY_INTERFACE", "CGI / 1.1",
100                                 true);
101                 configuration.addEnvironmentValue("REQUEST_METHOD", "GET", true);
102
103                 Map stringVars = DebugPlugin.getDefault().getLaunchManager()
104                                 .getNativeEnvironment();
105                 if (stringVars.containsKey("SYSTEMROOT"))
106                         configuration.addEnvironmentValue("SYSTEMROOT", (String) stringVars
107                                         .get("SYSTEMROOT"), true);
108
109         }
110
111         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
112                 StringBuffer loadPath = new StringBuffer();
113
114                 JavaProject project = configuration.getProject();
115                 addToLoadPath(loadPath, project.getProject());
116
117                 Iterator referencedProjects = project.getReferencedProjects()
118                                 .iterator();
119                 while (referencedProjects.hasNext())
120                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
121
122                 return loadPath.toString();
123         }
124
125         protected void addToLoadPath(StringBuffer loadPath, IProject project) {
126                 loadPath.append(" -I "
127                                 + osDependentPath(project.getFullPath().toOSString()));
128         }
129
130         protected String osDependentPath(String aPath) {
131                 if (Platform.getOS().equals(Platform.OS_WIN32))
132                         aPath = "\"" + aPath + "\"";
133
134                 return aPath;
135         }
136
137         protected String getDebugCommandLineArgument() {
138                 return "";
139         }
140 }