PHPackages                             yangweijie/c-to-php-ffi-converter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yangweijie/c-to-php-ffi-converter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yangweijie/c-to-php-ffi-converter
=================================

A PHP tool that automatically generates PHP FFI wrapper classes from C projects using klitsche/ffigen. Provides object-oriented wrappers with validation, error handling, and comprehensive documentation.

10PHP

Since Oct 25Pushed 6mo agoCompare

[ Source](https://github.com/yangweijie/c-to-php-ffi-converter)[ Packagist](https://packagist.org/packages/yangweijie/c-to-php-ffi-converter)[ RSS](/packages/yangweijie-c-to-php-ffi-converter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

C-to-PHP FFI Converter
======================

[](#c-to-php-ffi-converter)

A powerful PHP tool that automatically generates object-oriented PHP FFI wrapper classes from C projects. Built on top of [klitsche/ffigen](https://github.com/klitsche/ffigen), it provides enhanced functionality including parameter validation, error handling, and comprehensive documentation generation.

[中文版 README](README_ZH.md)

Features
--------

[](#features)

- **Automatic Wrapper Generation**: Convert C header files to PHP FFI wrapper classes
- **Parameter Validation**: Runtime type checking and validation for C function parameters
- **Error Handling**: Comprehensive error handling with descriptive exceptions
- **Documentation Generation**: Automatic PHPDoc comments and usage examples
- **CLI Interface**: Easy-to-use command-line interface
- **Flexible Configuration**: Support for YAML configuration files and CLI options
- **Dependency Resolution**: Automatic handling of header file dependencies

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- PHP FFI extension enabled
- Composer
- C compiler (for compiling example libraries)

Installation
------------

[](#installation)

### Global Installation (Recommended)

[](#global-installation-recommended)

Install globally via Composer:

```
composer global require yangweijie/c-to-php-ffi-converter
```

Make sure your global Composer bin directory is in your PATH:

```
export PATH="$PATH:$HOME/.composer/vendor/bin"
```

### Project-specific Installation

[](#project-specific-installation)

Install as a development dependency:

```
composer require --dev yangweijie/c-to-php-ffi-converter
```

Quick Start
-----------

[](#quick-start)

### Basic Usage

[](#basic-usage)

Generate PHP wrappers from a C header file:

```
c-to-php-ffi generate /path/to/header.h --output ./generated --namespace MyLib
```

### With Library File

[](#with-library-file)

Generate wrappers and specify the shared library:

```
c-to-php-ffi generate /path/to/header.h \
    --output ./generated \
    --namespace MyLib \
    --library /path/to/libmylib.so
```

### Using Configuration File

[](#using-configuration-file)

Create a configuration file `config.yaml`:

```
header_files:
  - /path/to/header1.h
  - /path/to/header2.h
library_file: /path/to/libmylib.so
output_path: ./generated
namespace: MyLib\FFI
validation:
  enable_parameter_validation: true
  enable_type_conversion: true
```

Run with configuration:

```
c-to-php-ffi generate --config config.yaml
```

Configuration
-------------

[](#configuration)

### CLI Options

[](#cli-options)

- `--output, -o`: Output directory for generated files
- `--namespace, -n`: PHP namespace for generated classes
- `--library, -l`: Path to shared library file
- `--config, -c`: Path to YAML configuration file
- `--exclude`: Patterns to exclude from generation
- `--verbose, -v`: Enable verbose output

### Configuration File Format

[](#configuration-file-format)

```
# Header files to process
header_files:
  - /path/to/header1.h
  - /path/to/header2.h

# Shared library file
library_file: /path/to/library.so

# Output configuration
output_path: ./generated
namespace: MyProject\FFI

# Validation settings
validation:
  enable_parameter_validation: true
  enable_type_conversion: true
  custom_validation_rules: []

# Generation settings
generation:
  generate_documentation: true
  generate_examples: true
  include_phpdoc: true

# Exclusion patterns
exclude_patterns:
  - "internal_*"
  - "_private_*"
```

Generated Code Structure
------------------------

[](#generated-code-structure)

The tool generates the following structure:

```
generated/
├── Classes/
│   ├── MathLibrary.php      # Main wrapper class
│   └── Structs/
│       └── Point.php        # Struct wrapper classes
├── Constants/
│   └── MathConstants.php    # Constants definitions
├── Documentation/
│   ├── README.md           # Usage documentation
│   └── Examples/
│       └── BasicUsage.php  # Usage examples
└── bootstrap.php           # Autoloader and initialization

```

Usage Examples
--------------

[](#usage-examples)

### Basic Function Calls

[](#basic-function-calls)

```
