Skip to content

Commit 58d4fb7

Browse files
committed
Add http-client example.
1 parent e57334b commit 58d4fb7

File tree

9 files changed

+89
-51
lines changed

9 files changed

+89
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
Cargo.lock
44
/.cargo
55
/vendor
6+
/core

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ members = [
1010
# internal
1111
"examples/hello",
1212
"examples/log",
13-
# "examples/mini-curl",
13+
"examples/http-client",
1414
]

examples/http-client/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "mini-curl"
3+
version = "0.0.0"
4+
authors = ["jmjoy <[email protected]>"]
5+
edition = "2018"
6+
publish = false
7+
license = "Unlicense"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[lib]
12+
crate-type = ["cdylib"]
13+
14+
[dependencies]
15+
phper = { version = "0.2.0-alpha.2", path = "../../phper" }
16+
17+
[dev-dependencies]
18+
phper-test = { version = "0.2.0-alpha.2", path = "../../phper-test" }

examples/http-client/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use phper::{modules::Module, php_get_module};
2+
3+
#[php_get_module]
4+
pub fn get_module() -> Module {
5+
let mut module = Module::new(
6+
env!("CARGO_PKG_NAME"),
7+
env!("CARGO_PKG_VERSION"),
8+
env!("CARGO_PKG_AUTHORS"),
9+
);
10+
11+
// ...
12+
13+
module
14+
}

examples/http-client/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use phper::cmd::make;
2+
3+
fn main() {
4+
make();
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
ini_set("display_errors", "On");
4+
ini_set("display_startup_errors", "On");
5+
error_reporting(E_ALL);
6+
7+
phpinfo();

examples/log/tests/integration.rs

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use phper_test::test_php_scripts_with_condition;
2-
use std::{env, path::Path, str};
3-
use std::process::Output;
2+
use std::{env, path::Path, process::Output, str};
43

54
#[test]
65
fn test_php() {
@@ -11,48 +10,33 @@ fn test_php() {
1110
test_php_scripts_with_condition(
1211
env!("CARGO_BIN_EXE_log"),
1312
&[
14-
(
15-
&base_dir.join("test_php_say.php"),
16-
&|output| {
17-
let stdout = str::from_utf8(&output.stdout).unwrap();
18-
stdout == "Hello, world!" && output.status.success()
19-
},
20-
),
21-
(
22-
&base_dir.join("test_php_notice.php"),
23-
&|output| {
24-
let stdout = str::from_utf8(&output.stdout).unwrap();
25-
stdout.contains("Notice:")
26-
&& stdout.contains("Something happened: just for test")
27-
&& output.status.success()
28-
},
29-
),
30-
(
31-
&base_dir.join("test_php_warning.php"),
32-
&|output| {
33-
let stdout = str::from_utf8(&output.stdout).unwrap();
34-
stdout.contains("Warning:")
35-
&& stdout.contains("Something warning: just for test")
36-
&& output.status.success()
37-
},
38-
),
39-
(
40-
&base_dir.join("test_php_error.php"),
41-
&|output| {
42-
let stdout = str::from_utf8(&output.stdout).unwrap();
43-
stdout.contains("Fatal error:")
44-
&& stdout.contains("Something gone failed: just for test")
45-
},
46-
),
47-
(
48-
&base_dir.join("test_php_deprecated.php"),
49-
&|output| {
50-
let stdout = str::from_utf8(&output.stdout).unwrap();
51-
stdout.contains("Deprecated:")
52-
&& stdout.contains("Something deprecated: just for test")
53-
&& output.status.success()
54-
},
55-
),
13+
(&base_dir.join("test_php_say.php"), &|output| {
14+
let stdout = str::from_utf8(&output.stdout).unwrap();
15+
stdout == "Hello, world!" && output.status.success()
16+
}),
17+
(&base_dir.join("test_php_notice.php"), &|output| {
18+
let stdout = str::from_utf8(&output.stdout).unwrap();
19+
stdout.contains("Notice:")
20+
&& stdout.contains("Something happened: just for test")
21+
&& output.status.success()
22+
}),
23+
(&base_dir.join("test_php_warning.php"), &|output| {
24+
let stdout = str::from_utf8(&output.stdout).unwrap();
25+
stdout.contains("Warning:")
26+
&& stdout.contains("Something warning: just for test")
27+
&& output.status.success()
28+
}),
29+
(&base_dir.join("test_php_error.php"), &|output| {
30+
let stdout = str::from_utf8(&output.stdout).unwrap();
31+
stdout.contains("Fatal error:")
32+
&& stdout.contains("Something gone failed: just for test")
33+
}),
34+
(&base_dir.join("test_php_deprecated.php"), &|output| {
35+
let stdout = str::from_utf8(&output.stdout).unwrap();
36+
stdout.contains("Deprecated:")
37+
&& stdout.contains("Something deprecated: just for test")
38+
&& output.status.success()
39+
}),
5640
],
5741
);
5842
}

phper-macros/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ pub fn c_str_ptr(input: TokenStream) -> TokenStream {
5151
/// use phper::{php_get_module, modules::Module};
5252
///
5353
/// #[php_get_module]
54-
/// pub fn get_module(module: &mut Module) {
55-
/// // set module metadata
56-
/// module.set_name(env!("CARGO_PKG_NAME"));
57-
/// module.set_version(env!("CARGO_PKG_VERSION"));
58-
/// module.set_author(env!("CARGO_PKG_AUTHORS"));
54+
/// pub fn get_module() -> Module {
55+
/// let mut module = Module::new(
56+
/// env!("CARGO_PKG_NAME"),
57+
/// env!("CARGO_PKG_VERSION"),
58+
/// env!("CARGO_PKG_AUTHORS"),
59+
/// );
5960
///
6061
/// // ...
62+
///
63+
/// module
6164
/// }
65+
///
6266
/// ```
6367
#[proc_macro_attribute]
6468
pub fn php_get_module(attr: TokenStream, input: TokenStream) -> TokenStream {

phper/src/cmd.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub fn try_make() -> crate::Result<()> {
3535
let extension_dir = CStr::from_bytes_with_nul(PHP_EXTENSION_DIR)?.to_str()?;
3636
println!("Installing shared extensions: {}", extension_dir);
3737
let ext_path = Path::new(extension_dir).join(ext_name);
38+
fs::create_dir_all(extension_dir)?;
3839
fs::copy(lib_path, ext_path)?;
3940
}
4041
}
@@ -52,7 +53,11 @@ fn get_lib_path_and_ext_name() -> crate::Result<(PathBuf, OsString)> {
5253

5354
let mut exe_name = OsString::new();
5455
exe_name.push("lib");
55-
exe_name.push(exe_stem);
56+
let lib_stem = exe_stem
57+
.to_str()
58+
.context("failed to generate target lib name")?
59+
.replace("-", "_");
60+
exe_name.push(lib_stem);
5661
exe_name.push(".so");
5762

5863
let mut ext_name = OsString::new();

0 commit comments

Comments
 (0)