Fix #1371992: Error with switch when last case block omits break or return
[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.ILaunchManager;
17 import org.eclipse.debug.core.Launch;
18 import org.eclipse.debug.core.model.IProcess;
19
20 public class InterpreterRunner {
21
22         public InterpreterRunner() {
23         }
24
25         public IProcess run(InterpreterRunnerConfiguration configuration, ILaunch launch) {             
26                 String commandLine = renderCommandLine(configuration);          
27                 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
28                 
29                 setEnvironmentVariables(configuration);
30                 String[] env = configuration.getEnvironment();
31                 Process nativePHPProcess = null;
32                 try {
33                         nativePHPProcess = configuration.getInterpreter().exec(commandLine, workingDirectory, env);
34                 } catch (IOException e) {
35                         throw new RuntimeException("Unable to execute interpreter: " + commandLine + workingDirectory);
36                 }
37
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);
41         
42                 return process ;
43         }
44
45         protected String renderLabel(InterpreterRunnerConfiguration configuration) {
46                 StringBuffer buffer = new StringBuffer();
47
48                 PHPInterpreter interpreter = configuration.getInterpreter();
49                 buffer.append("PHP ");
50                 buffer.append(interpreter.getCommand());
51                 buffer.append(" : ");
52                 buffer.append(configuration.getFileName());
53
54                 return buffer.toString();
55         }
56
57         protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
58                 PHPInterpreter interpreter = configuration.getInterpreter();
59
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());
67
68                 return buffer.toString();
69         }
70         
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);
80
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);
88
89                 configuration.addEnvironmentValue("GATEWAY_INTERFACE","CGI / 1.1",true);
90                 configuration.addEnvironmentValue("REQUEST_METHOD","GET",true);
91                 
92                 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
93                 if (stringVars.containsKey("SYSTEMROOT"))
94                         configuration.addEnvironmentValue("SYSTEMROOT",(String) stringVars.get("SYSTEMROOT"),true);
95                 
96         }
97
98         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
99                 StringBuffer loadPath = new StringBuffer();
100
101                 JavaProject project = configuration.getProject();
102                 addToLoadPath(loadPath, project.getProject());
103
104                 Iterator referencedProjects = project.getReferencedProjects().iterator();
105                 while (referencedProjects.hasNext())
106                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
107
108                 return loadPath.toString();
109         }
110
111         protected void addToLoadPath(StringBuffer loadPath, IProject project) {
112                 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
113         }
114
115         protected String osDependentPath(String aPath) {
116                 if (Platform.getOS().equals(Platform.OS_WIN32))
117                         aPath = "\"" + aPath + "\"";
118
119                 return aPath;
120         }
121         
122         protected String getDebugCommandLineArgument() {
123                 return "" ;     
124         }       
125 }