Skip to content

Commit ea84081

Browse files
committed
feat: Add interface builders
1 parent 0f84eef commit ea84081

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed

src/builders/class.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,79 @@ impl ClassBuilder {
381381
}
382382
}
383383

384+
pub struct InterfaceBuilder {
385+
class_builder: ClassBuilder,
386+
}
387+
388+
impl InterfaceBuilder {
389+
pub fn new<T: Into<String>>(name: T) -> Self {
390+
Self {
391+
class_builder: ClassBuilder::new(name),
392+
}
393+
}
394+
395+
pub fn implements(mut self, interface: ClassEntryInfo) -> Self {
396+
self.class_builder = self.class_builder.implements(interface);
397+
398+
self
399+
}
400+
401+
pub fn method(mut self, func: FunctionBuilder<'static>, flags: MethodFlags) -> Self {
402+
self.class_builder = self.class_builder.method(func, flags);
403+
404+
self
405+
}
406+
407+
pub fn constant<T: Into<String>>(
408+
mut self,
409+
name: T,
410+
value: impl IntoZval + 'static,
411+
docs: DocComments,
412+
) -> Result<Self> {
413+
self.class_builder = self.class_builder.constant(name, value, docs)?;
414+
415+
Ok(self)
416+
}
417+
418+
pub fn dyn_constant<T: Into<String>>(
419+
mut self,
420+
name: T,
421+
value: &'static dyn IntoZvalDyn,
422+
docs: DocComments,
423+
) -> Result<Self> {
424+
self.class_builder = self.class_builder.dyn_constant(name, value, docs)?;
425+
426+
Ok(self)
427+
}
428+
429+
pub fn flags(mut self, flags: ClassFlags) -> Self {
430+
self.class_builder = self.class_builder.flags(flags);
431+
self
432+
}
433+
434+
pub fn object_override<T: RegisteredClass>(mut self) -> Self {
435+
self.class_builder = self.class_builder.object_override::<T>();
436+
437+
self
438+
}
439+
440+
pub fn registration(mut self, register: fn(&'static mut ClassEntry)) -> Self {
441+
self.class_builder = self.class_builder.registration(register);
442+
443+
self
444+
}
445+
446+
pub fn docs(mut self, docs: DocComments) -> Self {
447+
self.class_builder = self.class_builder.docs(docs);
448+
self
449+
}
450+
451+
pub fn builder(mut self) -> ClassBuilder {
452+
self.class_builder = self.class_builder.flags(ClassFlags::Interface);
453+
self.class_builder
454+
}
455+
}
456+
384457
#[cfg(test)]
385458
mod tests {
386459
use crate::test::test_function;

src/builders/module.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{convert::TryFrom, ffi::CString, mem, ptr};
22

33
use super::{ClassBuilder, FunctionBuilder};
44
use crate::{
5+
builders::class::InterfaceBuilder,
56
class::RegisteredClass,
67
constant::IntoConst,
78
describe::DocComments,
@@ -168,6 +169,34 @@ impl ModuleBuilder<'_> {
168169
self
169170
}
170171

172+
pub fn interface<T: RegisteredClass>(mut self) -> Self {
173+
self.classes.push(|| {
174+
let mut builder = InterfaceBuilder::new(T::CLASS_NAME);
175+
for (method, flags) in T::method_builders() {
176+
builder = builder.method(method, flags);
177+
}
178+
for (name, value, docs) in T::constants() {
179+
builder = builder
180+
.dyn_constant(*name, *value, docs)
181+
.expect("Failed to register constant");
182+
}
183+
184+
let mut class_builder = builder.builder();
185+
186+
if let Some(modifier) = T::BUILDER_MODIFIER {
187+
class_builder = modifier(class_builder);
188+
}
189+
190+
class_builder
191+
.object_override::<T>()
192+
.registration(|ce| {
193+
T::get_metadata().set_ce(ce);
194+
})
195+
.docs(T::DOC_COMMENTS)
196+
});
197+
self
198+
}
199+
171200
/// Adds a class to the extension.
172201
///
173202
/// # Panics

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ pub mod prelude {
5151
pub use crate::php_println;
5252
pub use crate::types::ZendCallable;
5353
pub use crate::{
54-
php_class, php_const, php_extern, php_function, php_impl, php_module, wrap_constant,
55-
wrap_function, zend_fastcall, ZvalConvert,
54+
php_class, php_const, php_extern, php_function, php_impl, php_interface, php_module,
55+
wrap_constant, wrap_function, zend_fastcall, ZvalConvert,
5656
};
5757
}
5858

@@ -66,6 +66,6 @@ pub const PHP_DEBUG: bool = cfg!(php_debug);
6666
pub const PHP_ZTS: bool = cfg!(php_zts);
6767

6868
pub use ext_php_rs_derive::{
69-
php_class, php_const, php_extern, php_function, php_impl, php_module, wrap_constant,
70-
wrap_function, zend_fastcall, ZvalConvert,
69+
php_class, php_const, php_extern, php_function, php_impl, php_interface, php_module,
70+
wrap_constant, wrap_function, zend_fastcall, ZvalConvert,
7171
};

0 commit comments

Comments
 (0)