Skip to content

Commit a9dffe0

Browse files
authored
Add ability to show PHP warnings (etc) (#231)
* Add ability to show PHP warnings (etc) I don't believe there's a way for extensions to trigger PHP notices or warnings currently. This is done using the `php_error_docref` function. I've placed a function in `ext_php_rs::php_error()` however, there might be a better place? * Remove uneeded empty value * Fix url
1 parent 396c874 commit a9dffe0

File tree

4 files changed

+91
-10
lines changed

4 files changed

+91
-10
lines changed

allowed_bindings.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ bind! {
4646
// ext_php_rs_is_kown_valid_utf8,
4747
// ext_php_rs_set_kown_valid_utf8,
4848
object_properties_init,
49+
php_error_docref,
4950
php_info_print_table_end,
5051
php_info_print_table_header,
5152
php_info_print_table_row,
@@ -113,6 +114,21 @@ bind! {
113114
CONST_DEPRECATED,
114115
CONST_NO_FILE_CACHE,
115116
CONST_PERSISTENT,
117+
E_ERROR,
118+
E_WARNING,
119+
E_PARSE,
120+
E_NOTICE,
121+
E_CORE_ERROR,
122+
E_CORE_WARNING,
123+
E_COMPILE_ERROR,
124+
E_COMPILE_WARNING,
125+
E_USER_ERROR,
126+
E_USER_WARNING,
127+
E_USER_NOTICE,
128+
E_STRICT,
129+
E_RECOVERABLE_ERROR,
130+
E_DEPRECATED,
131+
E_USER_DEPRECATED,
116132
HT_MIN_SIZE,
117133
IS_ARRAY,
118134
IS_ARRAY_EX,

docsrs_bindings.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ pub const IS_OBJECT_EX: u32 = 776;
3131
pub const IS_RESOURCE_EX: u32 = 265;
3232
pub const IS_REFERENCE_EX: u32 = 266;
3333
pub const IS_CONSTANT_AST_EX: u32 = 267;
34+
pub const E_ERROR: u32 = 1;
35+
pub const E_WARNING: u32 = 2;
36+
pub const E_PARSE: u32 = 4;
37+
pub const E_NOTICE: u32 = 8;
38+
pub const E_CORE_ERROR: u32 = 16;
39+
pub const E_CORE_WARNING: u32 = 32;
40+
pub const E_COMPILE_ERROR: u32 = 64;
41+
pub const E_COMPILE_WARNING: u32 = 128;
42+
pub const E_USER_ERROR: u32 = 256;
43+
pub const E_USER_WARNING: u32 = 512;
44+
pub const E_USER_NOTICE: u32 = 1024;
45+
pub const E_STRICT: u32 = 2048;
46+
pub const E_RECOVERABLE_ERROR: u32 = 4096;
47+
pub const E_DEPRECATED: u32 = 8192;
48+
pub const E_USER_DEPRECATED: u32 = 16384;
3449
pub const ZEND_PROPERTY_ISSET: u32 = 0;
3550
pub const ZEND_PROPERTY_EXISTS: u32 = 2;
3651
pub const ZEND_ACC_PUBLIC: u32 = 1;
@@ -1341,6 +1356,14 @@ extern "C" {
13411356
extern "C" {
13421357
pub fn php_printf(format: *const ::std::os::raw::c_char, ...) -> usize;
13431358
}
1359+
extern "C" {
1360+
pub fn php_error_docref(
1361+
docref: *const ::std::os::raw::c_char,
1362+
type_: ::std::os::raw::c_int,
1363+
format: *const ::std::os::raw::c_char,
1364+
...
1365+
);
1366+
}
13441367
#[repr(C)]
13451368
#[derive(Debug, Copy, Clone)]
13461369
pub struct _zend_ini_entry {

src/error.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
//! Error and result types returned from the library functions.
22
3-
use std::{error::Error as ErrorTrait, ffi::NulError, fmt::Display};
3+
use std::{
4+
error::Error as ErrorTrait,
5+
ffi::{CString, NulError},
6+
fmt::Display,
7+
};
48

59
use crate::{
610
boxed::ZBox,
711
exception::PhpException,
8-
flags::{ClassFlags, DataType, ZvalTypeFlags},
12+
ffi::php_error_docref,
13+
flags::{ClassFlags, DataType, ErrorType, ZvalTypeFlags},
914
types::ZendObject,
1015
};
1116

@@ -108,3 +113,17 @@ impl From<Error> for PhpException {
108113
Self::default(err.to_string())
109114
}
110115
}
116+
117+
/// Trigger an error that is reported in PHP the same way `trigger_error()` is.
118+
///
119+
/// See specific error type descriptions at <https://www.php.net/manual/en/errorfunc.constants.php>.
120+
pub fn php_error(type_: ErrorType, message: &str) {
121+
let c_string = match CString::new(message) {
122+
Ok(string) => string,
123+
Err(_) => {
124+
return;
125+
}
126+
};
127+
128+
unsafe { php_error_docref(std::ptr::null(), type_.bits() as _, c_string.as_ptr()) }
129+
}

src/flags.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use bitflags::bitflags;
55
#[cfg(not(php82))]
66
use crate::ffi::ZEND_ACC_REUSE_GET_ITERATOR;
77
use crate::ffi::{
8-
CONST_CS, CONST_DEPRECATED, CONST_NO_FILE_CACHE, CONST_PERSISTENT, IS_ARRAY, IS_CALLABLE,
9-
IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_LONG, IS_MIXED, IS_NULL, IS_OBJECT, IS_PTR,
10-
IS_REFERENCE, IS_RESOURCE, IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE, IS_TYPE_REFCOUNTED,
11-
IS_UNDEF, IS_VOID, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS, ZEND_ACC_CALL_VIA_TRAMPOLINE,
12-
ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED, ZEND_ACC_CTOR,
13-
ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING, ZEND_ACC_FAKE_CLOSURE,
14-
ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK, ZEND_ACC_HAS_RETURN_TYPE,
15-
ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
8+
CONST_CS, CONST_DEPRECATED, CONST_NO_FILE_CACHE, CONST_PERSISTENT, E_COMPILE_ERROR,
9+
E_COMPILE_WARNING, E_CORE_ERROR, E_CORE_WARNING, E_DEPRECATED, E_ERROR, E_NOTICE, E_PARSE,
10+
E_RECOVERABLE_ERROR, E_STRICT, E_USER_DEPRECATED, E_USER_ERROR, E_USER_NOTICE, E_USER_WARNING,
11+
E_WARNING, IS_ARRAY, IS_CALLABLE, IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_LONG, IS_MIXED,
12+
IS_NULL, IS_OBJECT, IS_PTR, IS_REFERENCE, IS_RESOURCE, IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE,
13+
IS_TYPE_REFCOUNTED, IS_UNDEF, IS_VOID, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS,
14+
ZEND_ACC_CALL_VIA_TRAMPOLINE, ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED,
15+
ZEND_ACC_CTOR, ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING,
16+
ZEND_ACC_FAKE_CLOSURE, ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK,
17+
ZEND_ACC_HAS_RETURN_TYPE, ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
1618
ZEND_ACC_IMPLICIT_ABSTRACT_CLASS, ZEND_ACC_INTERFACE, ZEND_ACC_LINKED, ZEND_ACC_NEARLY_LINKED,
1719
ZEND_ACC_NEVER_CACHE, ZEND_ACC_NO_DYNAMIC_PROPERTIES, ZEND_ACC_PRELOADED, ZEND_ACC_PRIVATE,
1820
ZEND_ACC_PROMOTED, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC, ZEND_ACC_RESOLVED_INTERFACES,
@@ -171,6 +173,27 @@ bitflags! {
171173
}
172174
}
173175

176+
bitflags! {
177+
/// Represents error types when used via php_error_docref for example.
178+
pub struct ErrorType: u32 {
179+
const Error = E_ERROR;
180+
const Warning = E_WARNING;
181+
const Parse = E_PARSE;
182+
const Notice = E_NOTICE;
183+
const CoreError = E_CORE_ERROR;
184+
const CoreWarning = E_CORE_WARNING;
185+
const CompileError = E_COMPILE_ERROR;
186+
const CompileWarning = E_COMPILE_WARNING;
187+
const UserError = E_USER_ERROR;
188+
const UserWarning = E_USER_WARNING;
189+
const UserNotice = E_USER_NOTICE;
190+
const Strict = E_STRICT;
191+
const RecoverableError = E_RECOVERABLE_ERROR;
192+
const Deprecated = E_DEPRECATED;
193+
const UserDeprecated = E_USER_DEPRECATED;
194+
}
195+
}
196+
174197
/// Valid data types for PHP.
175198
#[repr(C, u8)]
176199
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]

0 commit comments

Comments
 (0)