【php 擴展開發】擴展生成器

什麼是擴展生成器php

每一個php擴展都包含一些很是公共的而且很是單調的結構和文件,這些文件對全部擴展來講都是通用的。當開始一個新擴展開發的時候,若是這些公共的結構已經存在,而不須要費力去複製每一個文件的內容, 咱們只需考慮填充功能代碼那心情必定會愉快不少。shell

擴展生成器就是實現這些功能的腳本,幫助咱們完成初始化工做。 PHP 源碼中提供一個自帶的生成器 ext_skel。他在 ext 目錄下。svn

ext_skel函數

它是一個 shell 腳本,僅有 300 多行。咱們來看下關鍵部分代碼測試

# 生成 config.m4 配置文件

cat >config.m4 <<eof
dnl \$Id\$
dnl config.m4 for extension $extname

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH($extname, for $extname support,
dnl Make sure that the comment is aligned:
dnl [  --with-$extname             Include $extname support])

dnl Otherwise use enable:

PHP_ARG_ENABLE($extname, whether to enable $extname support,
dnl Make sure that the comment is aligned:
[  --enable-$extname           Enable $extname support])

..... 省略一大串
eof

# 生成核心文件

$ECHO_N " $extname.c$ECHO_C"
echo "s/extname/$extname/g" > sedscript
echo "s/EXTNAME/$EXTNAME/g"  >> sedscript
echo '/__function_entries_here__/r function_entries'  >> sedscript
echo '/__function_stubs_here__/r function_stubs'  >> sedscript
echo '/__header_here__/r ../../header'  >> sedscript
echo '/__footer_here__/r ../../footer'  >> sedscript
echo '/__function_entries_here__/D'  >> sedscript
echo '/__function_stubs_here__/D'  >> sedscript
echo '/__header_here__/D'  >> sedscript
echo '/__footer_here__/D'  >> sedscript
if [ ! -z "$no_help" ]; then
    echo "/confirm_$extname_compiled/D" >> sedscript
    echo '/Remove the following/,/^\*\//D' >> sedscript
    echo 's/[[:space:]]\/\*.\+\*\///' >> sedscript
    echo 's/^\/\*.*\*\/$//' >> sedscript
    echo '/^[[:space:]]*\/\*/,/^[[:space:]]*\*\//D' >> sedscript
fi

sed -f sedscript < $skel_dir/skeleton.c > $extname.c

幫助說明this

./ext_skel --help

它提示了咱們腳本的命令行格式和支持的參數spa

./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module   擴展名稱,全爲小寫字母的標識符,僅包含字母和下劃線,保證在 php 源碼 ext 目錄下的文件夾名惟一
  --proto=file       容許開發人員指定一個頭文件,由此建立一系列 PHP 函數,表面上看就是要開發基於一個函數庫的擴展,不多用
  --stubs=file       僅生成文件中的函數存根,生成 IDE 提示文件可能有用
  --xml              generate xml documentation to be added to phpdoc-svn 沒用
  --skel=dir         path to the skeleton directory 指定擴展骨架目錄,若是你想在 ext 目錄之外的地方生成,那這個有用
  --full-xml         generate xml documentation for a self-contained extension (not yet implemented) 沒用
  --no-help          don't try to be nice and create comments in the code and helper functions to test if the module compiled 去除生成測試函數和註釋等內容,除非你很熟練,不建議操做

示例命令行

/usr/local/src/php-7.2.15/ext/ext_skel --extname=twinkle_log --skel=/usr/local/src/php-7.2.15/ext/skeleton/
相關文章
相關標籤/搜索