Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to show PHP warnings (etc) #231

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions allowed_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ bind! {
// ext_php_rs_is_kown_valid_utf8,
// ext_php_rs_set_kown_valid_utf8,
object_properties_init,
php_error_docref,
php_info_print_table_end,
php_info_print_table_header,
php_info_print_table_row,
Expand Down Expand Up @@ -113,6 +114,21 @@ bind! {
CONST_DEPRECATED,
CONST_NO_FILE_CACHE,
CONST_PERSISTENT,
E_ERROR,
E_WARNING,
E_PARSE,
E_NOTICE,
E_CORE_ERROR,
E_CORE_WARNING,
E_COMPILE_ERROR,
E_COMPILE_WARNING,
E_USER_ERROR,
E_USER_WARNING,
E_USER_NOTICE,
E_STRICT,
E_RECOVERABLE_ERROR,
E_DEPRECATED,
E_USER_DEPRECATED,
HT_MIN_SIZE,
IS_ARRAY,
IS_ARRAY_EX,
Expand Down
23 changes: 23 additions & 0 deletions docsrs_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ pub const IS_OBJECT_EX: u32 = 776;
pub const IS_RESOURCE_EX: u32 = 265;
pub const IS_REFERENCE_EX: u32 = 266;
pub const IS_CONSTANT_AST_EX: u32 = 267;
pub const E_ERROR: u32 = 1;
pub const E_WARNING: u32 = 2;
pub const E_PARSE: u32 = 4;
pub const E_NOTICE: u32 = 8;
pub const E_CORE_ERROR: u32 = 16;
pub const E_CORE_WARNING: u32 = 32;
pub const E_COMPILE_ERROR: u32 = 64;
pub const E_COMPILE_WARNING: u32 = 128;
pub const E_USER_ERROR: u32 = 256;
pub const E_USER_WARNING: u32 = 512;
pub const E_USER_NOTICE: u32 = 1024;
pub const E_STRICT: u32 = 2048;
pub const E_RECOVERABLE_ERROR: u32 = 4096;
pub const E_DEPRECATED: u32 = 8192;
pub const E_USER_DEPRECATED: u32 = 16384;
pub const ZEND_PROPERTY_ISSET: u32 = 0;
pub const ZEND_PROPERTY_EXISTS: u32 = 2;
pub const ZEND_ACC_PUBLIC: u32 = 1;
Expand Down Expand Up @@ -1341,6 +1356,14 @@ extern "C" {
extern "C" {
pub fn php_printf(format: *const ::std::os::raw::c_char, ...) -> usize;
}
extern "C" {
pub fn php_error_docref(
docref: *const ::std::os::raw::c_char,
type_: ::std::os::raw::c_int,
format: *const ::std::os::raw::c_char,
...
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _zend_ini_entry {
Expand Down
23 changes: 21 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
//! Error and result types returned from the library functions.

use std::{error::Error as ErrorTrait, ffi::NulError, fmt::Display};
use std::{
error::Error as ErrorTrait,
ffi::{CString, NulError},
fmt::Display,
};

use crate::{
boxed::ZBox,
exception::PhpException,
flags::{ClassFlags, DataType, ZvalTypeFlags},
ffi::php_error_docref,
flags::{ClassFlags, DataType, ErrorType, ZvalTypeFlags},
types::ZendObject,
};

Expand Down Expand Up @@ -108,3 +113,17 @@ impl From<Error> for PhpException {
Self::default(err.to_string())
}
}

/// Trigger an error that is reported in PHP the same way `trigger_error()` is.
///
/// See specific error type descriptions at <https://www.php.net/manual/en/errorfunc.constants.php>.
pub fn php_error(type_: ErrorType, message: &str) {
let c_string = match CString::new(message) {
Ok(string) => string,
Err(_) => {
return;
}
};

unsafe { php_error_docref(std::ptr::null(), type_.bits() as _, c_string.as_ptr()) }
}
39 changes: 31 additions & 8 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use bitflags::bitflags;
#[cfg(not(php82))]
use crate::ffi::ZEND_ACC_REUSE_GET_ITERATOR;
use crate::ffi::{
CONST_CS, CONST_DEPRECATED, CONST_NO_FILE_CACHE, CONST_PERSISTENT, IS_ARRAY, IS_CALLABLE,
IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_LONG, IS_MIXED, IS_NULL, IS_OBJECT, IS_PTR,
IS_REFERENCE, IS_RESOURCE, IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE, IS_TYPE_REFCOUNTED,
IS_UNDEF, IS_VOID, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS, ZEND_ACC_CALL_VIA_TRAMPOLINE,
ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED, ZEND_ACC_CTOR,
ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING, ZEND_ACC_FAKE_CLOSURE,
ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK, ZEND_ACC_HAS_RETURN_TYPE,
ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
CONST_CS, CONST_DEPRECATED, CONST_NO_FILE_CACHE, CONST_PERSISTENT, E_COMPILE_ERROR,
E_COMPILE_WARNING, E_CORE_ERROR, E_CORE_WARNING, E_DEPRECATED, E_ERROR, E_NOTICE, E_PARSE,
E_RECOVERABLE_ERROR, E_STRICT, E_USER_DEPRECATED, E_USER_ERROR, E_USER_NOTICE, E_USER_WARNING,
E_WARNING, IS_ARRAY, IS_CALLABLE, IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_LONG, IS_MIXED,
IS_NULL, IS_OBJECT, IS_PTR, IS_REFERENCE, IS_RESOURCE, IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE,
IS_TYPE_REFCOUNTED, IS_UNDEF, IS_VOID, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS,
ZEND_ACC_CALL_VIA_TRAMPOLINE, ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED,
ZEND_ACC_CTOR, ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING,
ZEND_ACC_FAKE_CLOSURE, ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK,
ZEND_ACC_HAS_RETURN_TYPE, ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
ZEND_ACC_IMPLICIT_ABSTRACT_CLASS, ZEND_ACC_INTERFACE, ZEND_ACC_LINKED, ZEND_ACC_NEARLY_LINKED,
ZEND_ACC_NEVER_CACHE, ZEND_ACC_NO_DYNAMIC_PROPERTIES, ZEND_ACC_PRELOADED, ZEND_ACC_PRIVATE,
ZEND_ACC_PROMOTED, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC, ZEND_ACC_RESOLVED_INTERFACES,
Expand Down Expand Up @@ -171,6 +173,27 @@ bitflags! {
}
}

bitflags! {
/// Represents error types when used via php_error_docref for example.
pub struct ErrorType: u32 {
const Error = E_ERROR;
const Warning = E_WARNING;
const Parse = E_PARSE;
const Notice = E_NOTICE;
const CoreError = E_CORE_ERROR;
const CoreWarning = E_CORE_WARNING;
const CompileError = E_COMPILE_ERROR;
const CompileWarning = E_COMPILE_WARNING;
const UserError = E_USER_ERROR;
const UserWarning = E_USER_WARNING;
const UserNotice = E_USER_NOTICE;
const Strict = E_STRICT;
const RecoverableError = E_RECOVERABLE_ERROR;
const Deprecated = E_DEPRECATED;
const UserDeprecated = E_USER_DEPRECATED;
}
}

/// Valid data types for PHP.
#[repr(C, u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
Expand Down