Skip to content

Commit 3ea45a4

Browse files
committed
Adding JavaPHP v1.0.0 source code
1 parent 576c8ad commit 3ea45a4

File tree

5 files changed

+106
-23
lines changed

5 files changed

+106
-23
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 BreadEater
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## JavaPHP
2+
3+
JavaPHP is a lightweight Java library that permits to execute PHP code into Java so instead of using Java servlets,
4+
you can just use PHP and execute it as a part of your existing Java code, does not rely on any dependencies, its fully standalone,
5+
and you can use it in a compiled .jar file or in a simple .java file without having to compile it
6+
7+
### Features
8+
- PHP file execution in java
9+
- Customizable error handling
10+
11+
### How to use
12+
Simply import it to your file and do like this:
13+
```java
14+
import java.io.File;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
public class Main {
19+
public static void main(String[] args) throws Throwable {
20+
PHPJava phpjava = new PHPJava(false); // Determines if PHP errors should be ignored
21+
Map<String, String> env = new HashMap<>(); // Stores PHP global variables (e.g REQUEST_METHOD, etc...)
22+
23+
env.put("REQUEST_METHOD", "GET");
24+
25+
// Defines a lambda function that will be called each time an error occurs
26+
phpjava.setErrorCallback((error) -> System.out.println("ERROR: " + error));
27+
28+
phpjava.setPHPVars(env); // Gives the global variables to the PHP file
29+
phpjava.run(new File("./test.php").getAbsolutePath()); // Runs the PHP file
30+
31+
// Prints the result (HTML code if used with a webserver)
32+
System.out.println(phpjava.getResult());
33+
}
34+
}
35+
```
36+
37+
### Installation
38+
Simply download the .jar / .java file from the repository Releases and import it in your project
39+
40+
To import the .java file, simply drag it into your project folder.<br>
41+
To import the .jar file, simply use Maven / Gradle and add new dependency to your configuration that points to the .jar file
42+
43+
### Contribution
44+
You can contribute as much as you want, the conditions:
45+
- know Java.
46+
- use Java JDK 21 or later.
47+
- keep it standalone (means no external dependencies).
48+
- provide least a minimum amount of documentation to the modifications you're doing.
49+
- Make your modifications clean for readability.
50+
51+
### Contact
52+
You can contact me via [mail](mailto:contact@breadeater.fr) or send message in the discussions of the repository
53+
54+
### License
55+
56+
This project is licensed under the MIT license, see [LICENSE](./LICENSE) for more infos.

pom.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@
66

77
<groupId>fr.breadeater</groupId>
88
<artifactId>JavaPHP</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.0-ALPHA</version>
1010

1111
<properties>
1212
<maven.compiler.source>21</maven.compiler.source>
1313
<maven.compiler.target>21</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</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>
2416

2517
<build>
2618
<plugins>
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package fr.breadeater;
22

3-
import org.jetbrains.annotations.TestOnly;
4-
53
import java.io.File;
64
import java.util.HashMap;
75
import java.util.Map;
@@ -10,17 +8,20 @@
108
* The Main class is used to test the library
119
*/
1210

13-
@TestOnly
14-
public class Main extends PHPJava {
11+
public class Main {
1512
public static void main(String[] args) throws Throwable {
16-
PHPJava phpjava = new PHPJava(true);
13+
/*
14+
PHPJava phpjava = new PHPJava(false);
1715
Map<String, String> env = new HashMap<>();
1816
1917
env.put("REQUEST_METHOD", "GET");
2018
19+
phpjava.setErrorCallback((error) -> System.out.println("ERROR: " + error));
20+
2121
phpjava.setPHPVars(env);
2222
phpjava.run(new File("./test.php").getAbsolutePath());
2323
2424
System.out.println(phpjava.getResult());
25+
*/
2526
}
2627
}

src/main/java/fr/breadeater/PHPJava.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.BufferedReader;
44
import java.io.InputStreamReader;
55
import java.util.Map;
6+
import java.util.function.Consumer;
67

78
public class PHPJava {
89
private final String PHP_BIN_PATH;
@@ -11,6 +12,8 @@ public class PHPJava {
1112
private Map<String, String> env_vars;
1213
private String response = null;
1314

15+
private Consumer<String> ERR_CALLBACK;
16+
1417
/**
1518
* Creates a PHPJava instance
1619
* @param php_bin_path PHP binary file path (e.g /usr/bin/php)
@@ -67,12 +70,11 @@ public void run(String php_filepath) throws Throwable {
6770
} catch (Throwable error){
6871
String err = error.getMessage();
6972

70-
if (!this.NO_PHP_WARN && (err.contains("PHP Warning:") || err.contains("PHP Startup:"))){
71-
onError(err);
72-
}
73+
if (!this.NO_PHP_WARN && (err.contains("PHP Warning:") || err.contains("PHP Startup:"))) onError(err);
7374
}
7475

7576
assert process != null;
77+
7678
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))){
7779
StringBuilder resBuilder = new StringBuilder();
7880
String line;
@@ -91,13 +93,19 @@ public void run(String php_filepath) throws Throwable {
9193
if (!errorBuilder.toString().isEmpty()){
9294
String err = new Throwable(errorBuilder.toString()).getMessage();
9395

94-
if (!this.NO_PHP_WARN && (err.contains("PHP Warning:") || err.contains("PHP Startup:"))){
95-
onError(err);
96-
}
96+
if (!this.NO_PHP_WARN && (err.contains("PHP Warning:") || err.contains("PHP Startup:"))) onError(err);
9797
}
9898
}
9999
}
100100

101+
/**
102+
* Sets the function that will be called to handle the error instead of printing it into the console
103+
* @param cb The function to be executed on error
104+
*/
105+
public void setErrorCallback(Consumer<String> cb){
106+
this.ERR_CALLBACK = cb;
107+
}
108+
101109
/**
102110
* Gets the result of the executed PHP file
103111
* @return The result of the PHP file, returns null if no results
@@ -107,10 +115,15 @@ public String getResult(){
107115
}
108116

109117
/**
110-
* Error event listener, outputs error with System.err by default, it needs to be overridden
118+
* Error event listener, outputs error with System.err by default, to set custom error handler, use {@link #setErrorCallback}
111119
* @param error The error
112120
*/
113-
public void onError(String error){
114-
System.err.println(error);
121+
private void onError(String error){
122+
if (this.ERR_CALLBACK == null) {
123+
System.err.println(error);
124+
return;
125+
}
126+
127+
this.ERR_CALLBACK.accept(error);
115128
}
116129
}

0 commit comments

Comments
 (0)