Skip to content

Commit e9b2b81

Browse files
committed
Add echo api.
1 parent 2822d76 commit e9b2b81

File tree

9 files changed

+58
-21
lines changed

9 files changed

+58
-21
lines changed

examples/log/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use phper::{
2-
deprecated, error, functions::Argument, modules::Module, notice, php_get_module, values::Val,
3-
warning,
2+
deprecated, echo, error, functions::Argument, modules::Module, notice, php_get_module,
3+
values::Val, warning,
44
};
55

66
#[php_get_module]
@@ -10,6 +10,15 @@ pub fn get_module(module: &mut Module) {
1010
module.set_version(env!("CARGO_PKG_VERSION"));
1111
module.set_author(env!("CARGO_PKG_AUTHORS"));
1212

13+
module.add_function(
14+
"log_say",
15+
|params: &mut [Val]| {
16+
let message = params[0].as_string();
17+
echo!("Hello, {}!", message);
18+
},
19+
vec![Argument::by_val("message")],
20+
);
21+
1322
module.add_function(
1423
"log_notice",
1524
|params: &mut [Val]| {

examples/log/tests/integration.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ use std::{env, path::Path, str};
33

44
#[test]
55
fn test_php() {
6+
test_php_scripts_with_condition(
7+
env!("CARGO_BIN_EXE_log"),
8+
&[Path::new(env!("CARGO_MANIFEST_DIR"))
9+
.join("tests")
10+
.join("php")
11+
.join("test_php_say.php")],
12+
|output| {
13+
let stdout = str::from_utf8(&output.stdout).unwrap();
14+
stdout == "Hello, world!" && output.status.success()
15+
},
16+
);
17+
618
test_php_scripts_with_condition(
719
env!("CARGO_BIN_EXE_log"),
820
&[Path::new(env!("CARGO_MANIFEST_DIR"))
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+
log_say("world");

phper/src/functions.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use std::{
22
mem::zeroed,
33
os::raw::c_char,
4-
ptr::null,
54
sync::atomic::{AtomicPtr, Ordering},
65
};
76

87
use crate::{
98
classes::ClassEntry,
109
objects::Object,
1110
sys::*,
11+
utils::ensure_end_with_zero,
1212
values::{ExecuteData, SetVal, Val},
1313
};
14-
use crate::utils::ensure_end_with_zero;
1514

1615
pub trait Function: Send + Sync {
1716
fn call(&self, arguments: &mut [Val], return_value: &mut Val);
@@ -163,17 +162,11 @@ pub(crate) unsafe extern "C" fn invoke(
163162

164163
// Check arguments count.
165164
if execute_data.num_args() < execute_data.common_required_num_args() {
166-
let s = format!(
165+
warning!(
167166
"expects at least {} parameter(s), {} given\0",
168167
execute_data.common_required_num_args(),
169168
execute_data.num_args()
170169
);
171-
php_error_docref1(
172-
null(),
173-
"\0".as_ptr().cast(),
174-
E_WARNING as i32,
175-
s.as_ptr().cast(),
176-
);
177170
return_value.set(());
178171
return;
179172
}

phper/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ pub mod cmd;
131131
mod errors;
132132
pub mod functions;
133133
pub mod ini;
134-
pub mod logs;
135134
pub mod modules;
136135
pub mod objects;
136+
pub mod output;
137137
pub mod strings;
138138
mod utils;
139139
pub mod values;

phper/src/macros.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1+
#[macro_export]
2+
macro_rules! echo {
3+
($($arg:tt)*) => ({
4+
$crate::output::echo(std::format!($($arg)*))
5+
})
6+
}
7+
18
#[macro_export]
29
macro_rules! error {
310
($($arg:tt)*) => ({
4-
phper::logs::log(phper::logs::Level::Error, std::format!($($arg)*))
11+
$crate::output::log($crate::output::LogLevel::Error, std::format!($($arg)*))
512
})
613
}
714

815
#[macro_export]
916
macro_rules! warning {
1017
($($arg:tt)*) => ({
11-
phper::logs::log(phper::logs::Level::Warning, std::format!($($arg)*))
18+
$crate::output::log($crate::output::LogLevel::Warning, std::format!($($arg)*))
1219
})
1320
}
1421

1522
#[macro_export]
1623
macro_rules! notice {
1724
($($arg:tt)*) => ({
18-
phper::logs::log(phper::logs::Level::Notice, std::format!($($arg)*))
25+
$crate::output::log($crate::output::LogLevel::Notice, std::format!($($arg)*))
1926
})
2027
}
2128

2229
#[macro_export]
2330
macro_rules! deprecated {
2431
($($arg:tt)*) => ({
25-
phper::logs::log(phper::logs::Level::Deprecated, std::format!($($arg)*))
32+
$crate::output::log($crate::output::LogLevel::Deprecated, std::format!($($arg)*))
2633
})
2734
}

phper/src/modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
functions::{Argument, Callable, Function, FunctionEntity},
55
ini::{IniEntity, IniValue, Policy, StrPtrBox},
66
sys::*,
7+
utils::ensure_end_with_zero,
78
EXCEPTION_CLASS_NAME,
89
};
910
use once_cell::sync::Lazy;
@@ -16,7 +17,6 @@ use std::{
1617
ptr::{null, null_mut},
1718
sync::RwLock,
1819
};
19-
use crate::utils::ensure_end_with_zero;
2020

2121
static GLOBAL_MODULE: Lazy<RwLock<Module>> = Lazy::new(Default::default);
2222

phper/src/logs.rs renamed to phper/src/output.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::ptr::null;
33

44
#[repr(u32)]
55
#[derive(Copy, Clone, PartialEq)]
6-
pub enum Level {
6+
pub enum LogLevel {
77
Error = E_ERROR,
88
Warning = E_WARNING,
99
Notice = E_NOTICE,
1010
Deprecated = E_DEPRECATED,
1111
}
1212

13-
pub fn log(level: Level, message: impl ToString) {
13+
pub fn log(level: LogLevel, message: impl ToString) {
1414
let message = ensure_end_with_zero(message);
1515
unsafe {
1616
php_error_docref1(
@@ -21,3 +21,13 @@ pub fn log(level: Level, message: impl ToString) {
2121
);
2222
}
2323
}
24+
25+
pub fn echo(message: impl ToString) {
26+
let message = ensure_end_with_zero(message);
27+
unsafe {
28+
zend_write.expect("function zend_write can't be null")(
29+
message.as_ptr().cast(),
30+
message.len() - 1,
31+
);
32+
}
33+
}

phper/src/values.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::{arrays::Array, errors::Throwable, sys::*};
1+
use crate::{arrays::Array, errors::Throwable, sys::*, utils::ensure_end_with_zero};
22
use std::{mem::zeroed, slice::from_raw_parts, str, sync::atomic::Ordering};
3-
use crate::utils::ensure_end_with_zero;
43

54
#[repr(transparent)]
65
pub struct ExecuteData {

0 commit comments

Comments
 (0)