Skip to content

Commit

Permalink
feat: setup the integrate with PHP binding (#2726)
Browse files Browse the repository at this point in the history
* Add PHP bindings for OpenDAL library

A new subdirectory `bindings/php` has been created in order to have PHP bindings for the OpenDAL library. This includes new PHP tests, stdlib stubs for the PHP interface, and addition of composer configuration for dependency management. as well as the initial source code written in Rust to provide PHP bindings. The 'debug' function is used as a test to check the binding. The README has been added to guide users and developers in building and testing the PHP bindings.

* Format License
  • Loading branch information
godruoyi committed Aug 2, 2023
1 parent ead90ca commit 4958c84
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 0 deletions.
25 changes: 25 additions & 0 deletions bindings/php/.cargo/config.toml
@@ -0,0 +1,25 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[target.'cfg(not(target_os = "windows"))']
rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"]

[target.x86_64-pc-windows-msvc]
linker = "rust-lld"

[target.i686-pc-windows-msvc]
linker = "rust-lld"
3 changes: 3 additions & 0 deletions bindings/php/.gitignore
@@ -0,0 +1,3 @@
/vendor
composer.lock
.phpunit.result.cache
35 changes: 35 additions & 0 deletions bindings/php/Cargo.toml
@@ -0,0 +1,35 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "opendal-php"
version = "0.1.0"

edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
ext-php-rs = "0.10.1"
opendal = {version = "0.38.1", path = "../../core" }
32 changes: 32 additions & 0 deletions bindings/php/README.md
@@ -0,0 +1,32 @@
# OpenDAL PHP Binding (WIP)

## Requirements

* PHP 8.1+

## Development

### Install Cargo PHP

we use [cargo-php](https://davidcole1340.github.io/ext-php-rs/getting-started/hello_world.html) to build the PHP extension.

```bash
cargo install cargo-php
```

### Build

Move to the `bindings/php` directory and run the following command, the extension will be built in `target/debug` directory and enable the extension in `php.ini` file.

```bash
cargo build
cargo php install
```

### Test

```bash
composer install
composer test
```

21 changes: 21 additions & 0 deletions bindings/php/composer.json
@@ -0,0 +1,21 @@
{
"authors": [
{
"name": "OpenDAL Contributors",
"email": "dev@opendal.apache.org"
}
],
"require-dev": {
"php": ">=8.1",
"pestphp/pest": "^2.9"
},
"license": "Apache-2.0",
"scripts": {
"test": "vendor/bin/pest"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": false
}
}
}
25 changes: 25 additions & 0 deletions bindings/php/opendal-php.stubs.php
@@ -0,0 +1,25 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// Stubs for opendal-php

namespace {
function debug(): string {}
}
32 changes: 32 additions & 0 deletions bindings/php/phpunit.xml
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
31 changes: 31 additions & 0 deletions bindings/php/src/lib.rs
@@ -0,0 +1,31 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use ext_php_rs::prelude::*;
use opendal::{EntryMode, Metadata};

#[php_function]
pub fn debug() -> String {
let metadata = Metadata::new(EntryMode::FILE);

format!("{:?}", metadata)
}

#[php_module]
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
module
}
27 changes: 27 additions & 0 deletions bindings/php/tests/Unit/BasicTest.php
@@ -0,0 +1,27 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

it('opendal-php extension loaded', function () {
expect(extension_loaded('opendal-php'))->toBeTrue();
});

it('debug function works', function () {
expect(debug())->toStartWith('Metadata');
});

0 comments on commit 4958c84

Please sign in to comment.