Skip to content

Commit 576c8ad

Browse files
committed
Adding JavaPHP v1.0.0 source code
0 parents  commit 576c8ad

10 files changed

Lines changed: 262 additions & 0 deletions

File tree

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

Whitespace-only changes.

README.md

Whitespace-only changes.

pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>fr.breadeater</groupId>
8+
<artifactId>JavaPHP</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.jetbrains</groupId>
19+
<artifactId>annotations</artifactId>
20+
<version>26.0.1</version>
21+
<scope>compile</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<artifactId>maven-assembly-plugin</artifactId>
29+
<configuration>
30+
<archive>
31+
<manifest>
32+
<mainClass>fr.breadeater.Main</mainClass>
33+
</manifest>
34+
</archive>
35+
<descriptorRefs>
36+
<descriptorRef>jar-with-dependencies</descriptorRef>
37+
</descriptorRefs>
38+
</configuration>
39+
<executions>
40+
<execution>
41+
<id>make-assembly</id>
42+
<phase>package</phase>
43+
<goals>
44+
<goal>single</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fr.breadeater;
2+
3+
import org.jetbrains.annotations.TestOnly;
4+
5+
import java.io.File;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* The Main class is used to test the library
11+
*/
12+
13+
@TestOnly
14+
public class Main extends PHPJava {
15+
public static void main(String[] args) throws Throwable {
16+
PHPJava phpjava = new PHPJava(true);
17+
Map<String, String> env = new HashMap<>();
18+
19+
env.put("REQUEST_METHOD", "GET");
20+
21+
phpjava.setPHPVars(env);
22+
phpjava.run(new File("./test.php").getAbsolutePath());
23+
24+
System.out.println(phpjava.getResult());
25+
}
26+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package fr.breadeater;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.Map;
6+
7+
public class PHPJava {
8+
private final String PHP_BIN_PATH;
9+
private final boolean NO_PHP_WARN;
10+
11+
private Map<String, String> env_vars;
12+
private String response = null;
13+
14+
/**
15+
* Creates a PHPJava instance
16+
* @param php_bin_path PHP binary file path (e.g /usr/bin/php)
17+
*/
18+
public PHPJava(String php_bin_path, boolean ignorePHPWarnings){
19+
this.PHP_BIN_PATH = php_bin_path;
20+
this.NO_PHP_WARN = ignorePHPWarnings;
21+
}
22+
23+
/**
24+
* Creates a PHPJava instance
25+
* @param ignorePHPWarnings Specifies if PHP Warning and PHP Startup errors should be ignored
26+
*/
27+
public PHPJava(boolean ignorePHPWarnings){
28+
this.PHP_BIN_PATH = "php";
29+
this.NO_PHP_WARN = ignorePHPWarnings;
30+
}
31+
32+
/**
33+
* Creates a PHPJava instance
34+
*/
35+
public PHPJava(){
36+
this.PHP_BIN_PATH = "php";
37+
this.NO_PHP_WARN = false;
38+
}
39+
40+
/**
41+
* Sets PHP global variables like REQUEST_METHOD, REQUEST_ADDR, etc...<br>
42+
* See <a href="https://www.php.net/manual/en/reserved.variables.php">PHP global variables</a> for other examples.<br><br>
43+
*
44+
* WARNING: THIS IS REQUIRED IF USING $_SERVER VARIABLES OR OTHERS !
45+
*/
46+
public void setPHPVars(Map<String, String> variables){
47+
this.env_vars = variables;
48+
}
49+
50+
/**
51+
* Runs the PHP file specified in the {@link PHPJava} constructor.<br><br>
52+
* The result can be retrieved with {@link #getResult()} function.
53+
*
54+
* @param php_filepath The file path of the PHP file to be executed.
55+
*/
56+
public void run(String php_filepath) throws Throwable {
57+
ProcessBuilder processBuilder = new ProcessBuilder(this.PHP_BIN_PATH, "-f", php_filepath);
58+
Process process = null;
59+
60+
if (this.env_vars != null){
61+
Map<String, String> environment = processBuilder.environment();
62+
environment.putAll(this.env_vars);
63+
}
64+
65+
try {
66+
process = processBuilder.start();
67+
} catch (Throwable error){
68+
String err = error.getMessage();
69+
70+
if (!this.NO_PHP_WARN && (err.contains("PHP Warning:") || err.contains("PHP Startup:"))){
71+
onError(err);
72+
}
73+
}
74+
75+
assert process != null;
76+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))){
77+
StringBuilder resBuilder = new StringBuilder();
78+
String line;
79+
80+
while ((line = reader.readLine()) != null) resBuilder.append(line).append("\n");
81+
82+
this.response = resBuilder.toString();
83+
}
84+
85+
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))){
86+
StringBuilder errorBuilder = new StringBuilder();
87+
String line;
88+
89+
while ((line = errorReader.readLine()) != null) errorBuilder.append(line).append("\n");
90+
91+
if (!errorBuilder.toString().isEmpty()){
92+
String err = new Throwable(errorBuilder.toString()).getMessage();
93+
94+
if (!this.NO_PHP_WARN && (err.contains("PHP Warning:") || err.contains("PHP Startup:"))){
95+
onError(err);
96+
}
97+
}
98+
}
99+
}
100+
101+
/**
102+
* Gets the result of the executed PHP file
103+
* @return The result of the PHP file, returns null if no results
104+
*/
105+
public String getResult(){
106+
return this.response;
107+
}
108+
109+
/**
110+
* Error event listener, outputs error with System.err by default, it needs to be overridden
111+
* @param error The error
112+
*/
113+
public void onError(String error){
114+
System.err.println(error);
115+
}
116+
}

0 commit comments

Comments
 (0)