Skip to content

Commit c9db20c

Browse files
committed
Merge branch 'master' into process-globals
2 parents 5acdfe6 + 5d1ffce commit c9db20c

File tree

6 files changed

+128
-10
lines changed

6 files changed

+128
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ pub const IS_OBJECT_EX: u32 = 776;
111111
pub const IS_RESOURCE_EX: u32 = 265;
112112
pub const IS_REFERENCE_EX: u32 = 266;
113113
pub const IS_CONSTANT_AST_EX: u32 = 267;
114+
pub const E_ERROR: u32 = 1;
115+
pub const E_WARNING: u32 = 2;
116+
pub const E_PARSE: u32 = 4;
117+
pub const E_NOTICE: u32 = 8;
118+
pub const E_CORE_ERROR: u32 = 16;
119+
pub const E_CORE_WARNING: u32 = 32;
120+
pub const E_COMPILE_ERROR: u32 = 64;
121+
pub const E_COMPILE_WARNING: u32 = 128;
122+
pub const E_USER_ERROR: u32 = 256;
123+
pub const E_USER_WARNING: u32 = 512;
124+
pub const E_USER_NOTICE: u32 = 1024;
125+
pub const E_STRICT: u32 = 2048;
126+
pub const E_RECOVERABLE_ERROR: u32 = 4096;
127+
pub const E_DEPRECATED: u32 = 8192;
128+
pub const E_USER_DEPRECATED: u32 = 16384;
114129
pub const ZEND_PROPERTY_ISSET: u32 = 0;
115130
pub const ZEND_PROPERTY_EXISTS: u32 = 2;
116131
pub const ZEND_ACC_PUBLIC: u32 = 1;
@@ -1584,6 +1599,14 @@ extern "C" {
15841599
extern "C" {
15851600
pub fn php_printf(format: *const ::std::os::raw::c_char, ...) -> usize;
15861601
}
1602+
extern "C" {
1603+
pub fn php_error_docref(
1604+
docref: *const ::std::os::raw::c_char,
1605+
type_: ::std::os::raw::c_int,
1606+
format: *const ::std::os::raw::c_char,
1607+
...
1608+
);
1609+
}
15871610
pub type php_stream = _php_stream;
15881611
pub type php_stream_wrapper = _php_stream_wrapper;
15891612
pub type php_stream_context = _php_stream_context;
@@ -2128,6 +2151,25 @@ extern "C" {
21282151
extern "C" {
21292152
pub fn php_info_print_table_end();
21302153
}
2154+
#[repr(C)]
2155+
#[derive(Debug, Copy, Clone)]
2156+
pub struct php_file_globals {
2157+
pub pclose_ret: ::std::os::raw::c_int,
2158+
pub def_chunk_size: usize,
2159+
pub auto_detect_line_endings: bool,
2160+
pub default_socket_timeout: zend_long,
2161+
pub user_agent: *mut ::std::os::raw::c_char,
2162+
pub from_address: *mut ::std::os::raw::c_char,
2163+
pub user_stream_current_filename: *const ::std::os::raw::c_char,
2164+
pub default_context: *mut php_stream_context,
2165+
pub stream_wrappers: *mut HashTable,
2166+
pub stream_filters: *mut HashTable,
2167+
pub wrapper_errors: *mut HashTable,
2168+
pub pclose_wait: ::std::os::raw::c_int,
2169+
}
2170+
extern "C" {
2171+
pub static mut file_globals: php_file_globals;
2172+
}
21312173
extern "C" {
21322174
pub static mut zend_ce_throwable: *mut zend_class_entry;
21332175
}

src/builders/module.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ impl ModuleBuilder {
126126
self
127127
}
128128

129+
/// Sets the post request shutdown function for the extension.
130+
///
131+
/// This function can be useful if you need to do any final cleanup at the
132+
/// very end of a request, after all other resources have been released. For
133+
/// example, if your extension creates any persistent resources that last
134+
/// beyond a single request, you could use this function to clean those up. # Arguments
135+
///
136+
/// * `func` - The function to be called when shutdown is requested.
137+
pub fn post_deactivate_function(mut self, func: extern "C" fn() -> i32) -> Self {
138+
self.module.post_deactivate_func = Some(func);
139+
self
140+
}
141+
129142
/// Sets the extension information function for the extension.
130143
///
131144
/// # Arguments

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)]

src/zend/globals.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ impl ExecutorGlobals {
6060
unsafe { self.class_table.as_ref() }
6161
}
6262

63+
/// Attempts to retrieve the global constants table.
64+
pub fn constants(&self) -> Option<&ZendHashTable> {
65+
unsafe { self.zend_constants.as_ref() }
66+
}
67+
6368
/// Attempts to extract the last PHP exception captured by the interpreter.
6469
/// Returned inside a [`ZBox`].
6570
///

0 commit comments

Comments
 (0)