承接 klitsche/ffigen 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

klitsche/ffigen

Composer 安装命令:

composer require klitsche/ffigen

包简介

FFI binding generator

README 文档

README

Build Status Test Coverage Maintainability Packagist Version

ffigen is a simple cli helper to quickly generate and update low level PHP FFI bindings for C libraries.

It generates two PHP files out of provided C header file(s):

  • constants.php - holding constant values
  • Methods.php - holding function bindings as static methods plus phpdoc in a trait

It is heavily inspired by FFIMe and depends on PHPCParser by ircmaxell.

WIP: Expect breaking changes along all 0.* pre-releases.

Requirements

  • PHP ^7.4 || ^8.0
  • For examples: FFI extension must be available and enabled

Quick Start

Install in your project:

composer require --dev klitsche/ffigen

Install a c library (eg. uuid).

Add a config file to your project root:

.ffigen.yml

Tweak this config file (example):

headerFiles:
  - uuid/uuid.h
libraryFile: libuuid.so.1
parserClass: Klitsche\FFIGen\Examples\UUID\FFIGen\Parser
outputPath: ./
excludeConstants:
  - /^(?!(FFI|UUID)_).*/
excludeMethods:
namespace: Klitsche\FFIGen\Examples\UUID

Optional: add your own Parser class to customize pre oder post processing logic (example):

<?php

declare(strict_types=1);

namespace Klitsche\FFIGen\Examples\UUID\FFIGen;

use Klitsche\FFIGen\Config;

class Parser extends \Klitsche\FFIGen\Adapter\PHPCParser\Parser
{
    public function __construct(Config $config)
    {
        parent::__construct($config);

        $this->context->defineInt('_SYS_TYPES_H', 1);
        $this->context->defineInt('_SYS_TIME_H', 1);
        $this->context->defineInt('_TIME_H', 1);
    }

    protected function parseHeaderFile(string $file): array
    {
        $file = $this->searchHeaderFilePath($file);

        $prependHeaderFile = '
            typedef long time_t;
        ';
        $tmpfile = tempnam(sys_get_temp_dir(), 'ffigen');
        file_put_contents($tmpfile, $prependHeaderFile . file_get_contents($file));

        $declarations = parent::parseHeaderFile($tmpfile);

        unlink($tmpfile);

        return $declarations;
    }

    private function searchHeaderFilePath(string $file): string
    {
        if (file_exists($file)) {
            return $file;
        }
        foreach ($this->context->headerSearchPaths as $headerSearchPath) {
            if (file_exists($headerSearchPath . '/' . $file)) {
                return $headerSearchPath . '/' . $file;
            }
        }

        throw new \RuntimeException(sprintf('File not found: %s', $file));
    }
}

Do not forget to register the Parser namespace in your composer.json for autoloading (dev is okay):

    "autoload-dev": {
        "psr-4": {
            "Klitsche\\FFIGen\\Examples\\": "examples"
        }
    },

Dump autoloading with

composer dump-autoload

Run ffigen to generate binding files

vendor/bin/ffigen

This generates the two files in the output path:

  • constants.php - add this to your autoloading
  • Methods.php - add this to your own class context and use it within your own high level php library

Do not forget to add constants.php to your compose.json for autoloading:

    "autoload": {
        "files": [
          "tweak-path-to/constants.php",
        ]
    },

Play with examples

Build docker image with preinstalled c libraries (uuid, snappy & librdkafka):

 docker-compose build php74

Run uuid example

docker-compose run --rm php74 php bin/ffigen generate -c examples/UUID/.ffigen.yml
docker-compose run --rm php74 php examples/UUID/test.php

Run snappy example (see Snappy class for a simple high level example)

docker-compose run --rm php74 bin/ffigen generate -c examples/Snappy/.ffigen.yml
docker-compose run --rm php74 php examples/Snappy/test.php

Run rdkafka example (librdkafka 1.5.2 & mock cluster)

docker-compose run --rm php74 bin/ffigen generate -c examples/RdKafka/.ffigen.yml
docker-compose run --rm php74 php examples/RdKafka/test.php

Todos

  • Add travis
  • Add more tests
  • Add documentation
  • Add support for Windows, macOS
  • Add more examples (and learn from them)
  • Think about multi version support
  • Think about custom interface / class generation for types
  • Think about clang / cpp / readelf adapter (cpp defines only & clean file, clang -c11 ast-dump=json, readelf --dyn-syms)

klitsche/ffigen 适用场景与选型建议

klitsche/ffigen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.05k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2020 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「binding」 「ffi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 klitsche/ffigen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 klitsche/ffigen 我们能提供哪些服务?
定制开发 / 二次开发

基于 klitsche/ffigen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 10
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-08