Skip to content

Commit

Permalink
Working on Classes
Browse files Browse the repository at this point in the history
Signed-off-by: Dusan Malusev <dusan@dusanmalusev.dev>
  • Loading branch information
CodeLieutenant committed Jun 24, 2024
1 parent bf89d9d commit d34724c
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 69 deletions.
70 changes: 70 additions & 0 deletions examples/complex/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
dnl Copyright (c) 2022 PHPER Framework Team
dnl PHPER is licensed under Mulan PSL v2.
dnl You can use this software according to the terms and conditions of the Mulan
dnl PSL v2. You may obtain a copy of Mulan PSL v2 at:
dnl http://license.coscl.org.cn/MulanPSL2
dnl THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
dnl KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
dnl NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
dnl See the Mulan PSL v2 for more details.

PHP_ARG_ENABLE([complex],
[whether to enable hello support],
[AS_HELP_STRING([--enable-complex],
[Enable complex support])],
[no])

dnl If not enable, `cargo build` run with argument `--release`.
PHP_ARG_ENABLE([cargo_debug], [whether to enable cargo debug mode],
[ --enable-cargo-debug Enable cargo debug], no, no)

if test "$PHP_hello" != "no"; then
dnl Check cargo command exists or not.
AC_PATH_PROG(CARGO, cargo, no)
if ! test -x "$CARGO"; then
AC_MSG_ERROR([cargo command missing, please reinstall the cargo distribution])
fi

AC_DEFINE(HAVE_complex, 1, [ Have complex support ])

PHP_NEW_EXTENSION(complex, [ ], $ext_shared)

CARGO_MODE_FLAGS="--release"
CARGO_MODE_DIR="release"

if test "$PHP_CARGO_DEBUG" != "no"; then
CARGO_MODE_FLAGS=""
CARGO_MODE_DIR="debug"
fi

cat >>Makefile.objects<< EOF
all: cargo_build

clean: cargo_clean

cargo_build:
# Build the extension file
PHP_CONFIG=$PHP_PHP_CONFIG cargo build $CARGO_MODE_FLAGS

# Copy the extension file from target dir to modules
if [[ -f ./target/$CARGO_MODE_DIR/libcomplex.dylib ]] ; then \\
cp ./target/$CARGO_MODE_DIR/libcomplex.dylib ./modules/complex.so ; fi
if [[ -f ./target/$CARGO_MODE_DIR/libcomplex.so ]] ; then \\
cp ./target/$CARGO_MODE_DIR/libcomplex.so ./modules/complex.so ; fi

cargo_clean:
cargo clean

.PHONY: cargo_build cargo_clean
EOF

dnl Symbolic link the files for `cargo build`
AC_CONFIG_LINKS([ \
Cargo.lock:Cargo.lock \
Cargo.toml:Cargo.toml \
build.rs:build.rs \
stubs:stubs \
src:src \
tests:tests \
])
fi
64 changes: 64 additions & 0 deletions examples/complex/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2022 PHPER Framework Team
PHPER is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2. You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
-->
<package version="2.0"
xmlns="http://pear.php.net/dtd/package-2.0"
xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<name>complex</name>
<channel>pecl.php.net</channel>
<summary>Hello world example a bit complex.</summary>
<description>The Hello world example of phper.</description>
<lead>
<name>jmjoy</name>
<user>jmjoy</user>
<email>jmjoy@apache.org</email>
<active>yes</active>
</lead>
<date>1970-01-01</date>
<version>
<release>0.0.0</release>
<api>0.0.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://license.coscl.org.cn/MulanPSL2/">MulanPSL-2.0</license>
<notes>Release notes.</notes>
<contents>
<dir name="/">
<file name="Cargo.lock" role="src"/>
<file name="Cargo.toml" role="src"/>
<file name="README.md" role="doc"/>
<file name="build.rs" role="src"/>
<file name="config.m4" role="src"/>
<file name="src/lib.rs" role="src"/>
<file name="src/args_bindings.rs" role="src"/>
<file name="stubs/say_hello.stub.php" role="src"/>
</dir>
</contents>
<dependencies>
<required>
<php>
<min>8.1.0</min>
</php>
<pearinstaller>
<min>1.4.0</min>
</pearinstaller>
</required>
</dependencies>
<providesextension>complex</providesextension>
<extsrcrelease>
<configureoption default="no" name="enable-cargo-debug" prompt="enable cargo debug?"/>
</extsrcrelease>
</package>
73 changes: 35 additions & 38 deletions examples/complex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ use phper::objects::StateObj;
use phper::{modules::Module, php_get_module, values::ZVal, zend_args};

fn say_hello(arguments: &mut [ZVal]) -> phper::Result<String> {
let name = &mut arguments[0];
name.convert_to_string();
let name = name.as_z_str().unwrap().to_str()?;

let name = arguments[0].as_z_str().unwrap().to_str()?;
Ok(format!("Hello, {name}!\n"))
}

Expand All @@ -47,14 +44,14 @@ pub fn get_module() -> Module {
);

// register module ini
module.add_ini("complex.enable", false, Policy::All);
module.add_ini("complex.num", 100, Policy::All);
module.add_ini("complex.ratio", 1.5, Policy::All);
module.add_ini(
"complex.description",
"hello world.".to_owned(),
Policy::All,
);
// module.add_ini("complex.enable", false, Policy::All);
// module.add_ini("complex.num", 100, Policy::All);
// module.add_ini("complex.ratio", 1.5, Policy::All);
// module.add_ini(
// "complex.description",
// "hello world.".to_owned(),
// Policy::All,
// );

// register hook functions
module.on_module_init(|_info| {});
Expand All @@ -66,13 +63,13 @@ pub fn get_module() -> Module {
"Complex\\say_hello",
zend_args!(arginfo_Complex_say_hello),
say_hello,
)
.add_function(
"Complex\\throw_exception",
zend_args!(arginfo_Complex_throw_exception),
throw_exception,
);
// .add_function(
// "Complex\\throw_exception",
// zend_args!(arginfo_Complex_throw_exception),
// throw_exception,
// )
// .add_function(
// "Complex\\get_all_ini",
// zend_args!(arginfo_Complex_get_all_ini),
// |_: &mut [ZVal]| {
Expand All @@ -83,30 +80,30 @@ pub fn get_module() -> Module {
//
// let complex_description =
// ZVal::from(ini_get::<Option<&CStr>>("complex.description"));
// arr.insert("complex.description", complex_description);
// Ok::<_, Infallible>(arr)
// },
// );
//
// let mut foo_class = ClassEntity::new(CLASS_COMPLEX_FOO);
//
// foo_class.add_method(
// |this: &mut StateObj, _: &mut [ZVal]| {
// Ok::<_, phper::Error>(this.get_property("foo").clone())
// arr.insert("complex.description",complex_description);
// Ok::<_, Infallible>(arr.clone())
// },
// MethodEntityBuilder::new("getFoo", zend_args!(arginfo_class_Complex_Foo_getFoo))
// .set_public(),
// );
//
// foo_class.add_method(
// |this: &mut StateObj, arguments: &mut [ZVal]| -> phper::Result<()> {
// this.set_property("foo", arguments[0].clone());
// Ok(())
// },
// MethodEntityBuilder::new("setFoo", zend_args!(arginfo_class_Complex_Foo_setFoo))
// .set_public(),
// );
// module.add_class(foo_class);
let mut foo_class = ClassEntity::new(CLASS_COMPLEX_FOO);

foo_class.add_method(
|this: &mut StateObj, _: &mut [ZVal]| {
Ok::<_, phper::Error>(this.get_property("foo").clone())
},
MethodEntityBuilder::new("getFoo", zend_args!(arginfo_class_Complex_Foo_getFoo))
.set_public(),
);

foo_class.add_method(
|this: &mut StateObj, arguments: &mut [ZVal]| -> phper::Result<()> {
this.set_property("foo", arguments[0].clone());
Ok(())
},
MethodEntityBuilder::new("setFoo", zend_args!(arginfo_class_Complex_Foo_setFoo))
.set_public(),
);
module.add_class(foo_class);

module.add_info("extra info key", "extra info value");

Expand Down
25 changes: 11 additions & 14 deletions examples/complex/tests/php/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,23 @@
ini_set("display_startup_errors", "On");
error_reporting(E_ALL);

// print_r(get_defined_functions());
// print_r(ini_get_all());
assert_eq(Complex\say_hello("world"), "Hello, world!\n");
//
// try {
// Complex\throw_exception();
// } catch (ErrorException $e) {
// assert_eq($e->getMessage(), "I am sorry");
// }
//

try {
Complex\throw_exception();
} catch (ErrorException $e) {
assert_eq($e->getMessage(), "I am sorry");
}

// assert_eq(Complex\get_all_ini(), [
// "complex.enable" => false,
// "complex.description" => "hello world.",
// ]);

// $foo = new Complex\\FooClass();
// assert_eq($foo->getFoo(), 100);
//
// $foo->setFoo(200);
// assert_eq($foo->getFoo(), 200);
$foo = new Complex\Foo();
assert_eq($foo->getFoo(), 100);
$foo->setFoo(200);
assert_eq($foo->getFoo(), 200);

function assert_eq($left, $right) {
if ($left !== $right) {
Expand Down
1 change: 1 addition & 0 deletions examples/hello/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ EOF
Cargo.toml:Cargo.toml \
build.rs:build.rs \
src:src \
stubs:stubs \
])
fi
9 changes: 5 additions & 4 deletions examples/hello/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
-->
<package version="2.0"
xmlns="http://pear.php.net/dtd/package-2.0"
xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"
<package version="2.0"
xmlns="http://pear.php.net/dtd/package-2.0"
xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<name>hello</name>
<channel>pecl.php.net</channel>
Expand Down Expand Up @@ -43,12 +43,13 @@ See the Mulan PSL v2 for more details.
<file name="build.rs" role="src" />
<file name="config.m4" role="src" />
<file name="src/lib.rs" role="src" />
<file name="stubs/say_hello.stub.php" role="src" />
</dir>
</contents>
<dependencies>
<required>
<php>
<min>7.2.0</min>
<min>8.1.0</min>
</php>
<pearinstaller>
<min>1.4.0</min>
Expand Down
17 changes: 17 additions & 0 deletions phper-build/php_args_bindings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <php.h>

BEGIN_EXTERN_C()
#define static

/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 06df13993563e31f9f0fecc3b09978b926a9fda6 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_hello, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_END_ARG_INFO()




#undef static
END_EXTERN_C()
5 changes: 5 additions & 0 deletions phper-build/php_args_bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <php.h>

extern const zend_internal_arg_info arginfo_hello[2];
2 changes: 1 addition & 1 deletion phper/src/classes/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<T> crate::modules::Registerer for ClassEntity<T> {

let class_ce = phper_register_class_entry(
Some(self.class_create),
methods.as_ptr().cast(),
Box::into_raw(methods.into_boxed_slice()).cast(),
Some(create_object),
);

Expand Down
Loading

0 comments on commit d34724c

Please sign in to comment.