0%

PHP各种扩展

Lua是一个简洁、轻量、可扩展的脚本语言,它的官方版本只包括一个精简的核心和最基本的库。这使得Lua体积小、启动速度快。它用ANSI C语言编写并以源代码形式开放,编译后仅仅一百余K,可以很方便的嵌入别的程序里。

Lua

一、安装lua

  1. cd /usr/local/src

  2. sudo wget https://www.lua.org/ftp/lua-5.3.5.tar.gz

  3. sudo tar -zxvf lua-5.3.5.tar.gz

  4. cd lua-5.3.5

  5. sudo make macosx test

    • 查看下MakeFile文件内容cat MakeFile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
     # Makefile for installing Lua
    # See doc/readme.html for installation and customization instructions.

    # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

    # Your platform. See PLATS for possible values.
    PLAT= none

    # Where to install. The installation starts in the src and doc directories,
    # so take care if INSTALL_TOP is not an absolute path. See the local target.
    # You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with
    # LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h.
    INSTALL_TOP= /usr/local
    INSTALL_BIN= $(INSTALL_TOP)/bin
    INSTALL_INC= $(INSTALL_TOP)/include
    INSTALL_LIB= $(INSTALL_TOP)/lib
    INSTALL_MAN= $(INSTALL_TOP)/man/man1
    INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V
    INSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V

    # How to install. If your install program does not support "-p", then
    # you may have to run ranlib on the installed liblua.a.
    INSTALL= install -p
    INSTALL_EXEC= $(INSTALL) -m 0755
    INSTALL_DATA= $(INSTALL) -m 0644
    #
    # If you don't have "install" you can use "cp" instead.
    # INSTALL= cp -p
    # INSTALL_EXEC= $(INSTALL)
    # INSTALL_DATA= $(INSTALL)

    # Other utilities.
    MKDIR= mkdir -p
    RM= rm -f

    # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE =======

    # Convenience platforms targets.
    PLATS= aix bsd c89 freebsd generic linux macosx mingw posix solaris

    # What to install.
    TO_BIN= lua luac
    TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
    TO_LIB= liblua.a
    TO_MAN= lua.1 luac.1

    # Lua version and release.
    V= 5.3
    R= $V.4

    # Targets start here.
    all: $(PLAT)

    $(PLATS) clean:
    cd src && $(MAKE) $@

    test: dummy
    src/lua -v

    install: dummy
    cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD)
    cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN)
    cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC)
    cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB)
    cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN)

    uninstall:
    cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN)
    cd src && cd $(INSTALL_INC) && $(RM) $(TO_INC)
    cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIB)
    cd doc && cd $(INSTALL_MAN) && $(RM) $(TO_MAN)

    local:
    $(MAKE) install INSTALL_TOP=../install

    none:
    @echo "Please do 'make PLATFORM' where PLATFORM is one of these:"
    @echo " $(PLATS)"
    @echo "See doc/readme.html for complete instructions."

    # make may get confused with test/ and install/
    dummy:

    # echo config parameters
    echo:
    @cd src && $(MAKE) -s echo
    @echo "PLAT= $(PLAT)"
    @echo "V= $V"
    @echo "R= $R"
    @echo "TO_BIN= $(TO_BIN)"
    @echo "TO_INC= $(TO_INC)"
    @echo "TO_LIB= $(TO_LIB)"
    @echo "TO_MAN= $(TO_MAN)"
    @echo "INSTALL_TOP= $(INSTALL_TOP)"
    @echo "INSTALL_BIN= $(INSTALL_BIN)"
    @echo "INSTALL_INC= $(INSTALL_INC)"
    @echo "INSTALL_LIB= $(INSTALL_LIB)"
    @echo "INSTALL_MAN= $(INSTALL_MAN)"
    @echo "INSTALL_LMOD= $(INSTALL_LMOD)"
    @echo "INSTALL_CMOD= $(INSTALL_CMOD)"
    @echo "INSTALL_EXEC= $(INSTALL_EXEC)"
    @echo "INSTALL_DATA= $(INSTALL_DATA)"

    # echo pkg-config data
    pc:
    @echo "version=$R"
    @echo "prefix=$(INSTALL_TOP)"
    @echo "libdir=$(INSTALL_LIB)"
    @echo "includedir=$(INSTALL_INC)"

    # list targets that do not create files (but not all makes understand .PHONY)
    .PHONY: all $(PLATS) clean test install local none dummy echo pecho lecho
  6. sudo make install

  7. lua -v

二、安装php的lua扩展

  1. 下载扩展包,传送门
  2. 解压sudo tar -zxvf lua-2.0.6.tgz
  3. 生成configure文件sudo /usr/local/php7.1/bin/phpize
  4. 检测,生成MakeFile,指定安装参数sudo ./configure --with-php-config=/usr/local/php7.1/bin/php-config --with-lua=/usr/local
    • 不加--with-lua=/usr/local选项报错
1
2
3
checking for lua in default path... found in /usr/local
checking for lua library in default path... not found
configure: error: Please reinstall the lua distribution - lua library should be in <lua-dir>/lib/
  1. 解决
    • 方式一:老老实实地加--with-lua=/usr/local
    • 方式二:sudo cp /usr/local/lib/liblua.a /usr/lib/liblua.a,此命令只在低版本的mac中可用,升级到Catalina后执行报错sudo cp /usr/local/lib/liblua.a /usr/lib/liblua.a cp: /usr/lib/liblua.a: Read-only file system,这是因为 Catalina 分离了系统文件和用户文件,系统文件都被 mount 到了只读分区,当然好处是显而易见的,防止系统文件被恶意篡改,处理方法如下:
      • 首先,禁止SIP(重启mac,command+r进入恢复模式,打开终端输入命令csrutil disable)
      • 重启后在terminal输入sudo mount -uw /,意思是把分区mount成可写模式,在系统重启后失效
      • 执行之前没有权限的操作,如复制、移动文件等
      • 重启后再启用SIP(重启mac,command+r进入恢复模式,打开终端输入命令csrutil enable)
      • 重新执行sudo ./configure --with-php-config=/usr/local/php7.1/bin/php-config
  2. 安装sudo make && sudo make install
  3. 加载扩展extension=lua.so(extension_dir之前已设置好)

三、使用

1
2
3
4
5
6
7
8
9
10
11
12
13
$lua = new Lua();
$lua->eval(<<<CODE
function dummy(foo, bar)
print(foo, ",", bar)
end
CODE
);
$lua->call("dummy", array("Hello", "php"));
echo "<br>";
$lua->dummy("Hello", "lua");
echo "<br>";
$res = $lua->call(array("table", "concat"), array(array(1=>11, 2=>22, 3=>33), "-"));
print_r($res);

四、参考

  1. 参考一
  2. 参考二

swoole

一、概念

  1. Swoole是由C语言开发的php扩展类,由于有着C语言的优势,swoole在内存管理、数据结构、通信协议解析等方面优势明显。它是一个面向生产环境的PHP异步网络通信引擎,使PHP开发人员可以编写高性能的异步并发TCP、UDP、Unix Socket、HTTP、WebSocket服务。Swoole可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用PHP+Swoole作为网络通信框架,可以使企业IT研发团队的效率大大提升。

二、安装

  1. 下载源码包/usr/local/src目录下
  2. 解压 sudo tar -zxvf swoole-4.4.3.tgz
  3. 切换目录 cd swoole-4.4.3
  4. 运行sudo /usr/local/php/7.3/bin/phpize命令,生成configure可执行文件
  5. 运行配置sudo ./configure --with-php-config=/usr/local/php/7.3/bin/php-config
  6. 安装扩展sudo make && sudo make install
  7. 修改php.ini文件,增加extension=/path/swoole.so
  8. 重启php-fpm
  9. 查看是否安装成功php7.3 -m,显示swoole则成功

三、参考

  1. 参考一
  2. 参考二
  3. 参考三
  4. 参考四

Yaf-Yar-Yac

一、概念

  1. Yaf,全称Yet Another Framework,一个C语言编写的以PHP扩展形式提供高性能的PHP开发框架,它更快,更轻便,提供了Bootstrap,路由,分发,视图,插件等,是一个全功能的PHP框架。
  2. Yar,全称Yet Another RPC Framework,一个C语言编写的PHP扩展的RPC框架,它轻量级,可并行化,支持多种打包协议(msgpack,json,php)。
  3. Yac,全称Yet Another Cache,一个一个基于共享内存的高性能的缓存扩展。

二、安装

      本身就是以扩展的形式存在,和其他扩展安装没什么区别,自己gg一下就出来了。在php命令行下可通过命令php --ri extension_name获得扩展相关信息

  1. yaf配置信息

    • yaf.environ:环境名称,当用INI作为Yaf的配置文件时,这个指明了Yaf将要在INI配置中读取的节的名字,默认product。
    • yaf.library:全局类库的目录路径,默认NULL
    • yaf.cache_config:是否缓存配置文件(只针对INI配置文件生效),打开此选项可在复杂配置的情况下提高性能,默认0。
    • yaf.name_suffix:在处理Controller/Action/Plugin/Model的时候,类名中关键信息是否是后缀式,比如UserModel,而在前缀模式下则是ModelUser,默认1。
    • yaf.name_separator:在处理Controller,Action,Plugin,Model的时候,前缀和名字之间的分隔符,默认为空,也就是UserPlugin,加入设置为”_”,则判断的依据就会变成:”User_Plugin”,这个主要是为了兼容ST已有的命名规范,默认””。
    • yaf.forward_limit:forward最大嵌套深度,默认5。
    • yaf.use_namespace:开启的情况下,Yaf将会使用命名空间方式注册自己的类,比如Yaf_Application将会变成Yaf\Application,默认0。
    • yaf.use_spl_autoload:开启的情况下,Yaf在加载不成功的情况下,会继续让PHP的自动加载函数加载,从性能考虑,除非特殊情况,否则保持这个选项关闭,默认0。
  2. yar配置信息

    • yar.packager string:设置Yar的打包工具,可以是PHP(serialize),JSON,Msgpack(这个需要编译的时候指定–enable-msgpack).
    • yar.debug string:打开的时候,Yar会把请求过程的日志都打印出来(到stderr).
    • yar.connect_timeout integer:连接超时(毫秒为单位)
    • yar.timeout integer:处理超时(毫秒为单位)
    • yar.expose_info bool:如果关闭,则当通过浏览器访问Server的时候,不会出现Server Info信息.
  3. yac配置信息

    • yac.enable = 1
    • yac.keys_memory_size = 4M ; 4M can get 30K key slots,32M can get 100K key slots
    • yac.values_memory_size = 64M
    • yac.compress_threshold = -1
    • yac.enable_cli = 0 ; whether enable yac with cli,default 0

四、使用

  1. Yaf
  2. Yar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//filename operator.php
class Operator
{
public function add($a, $b) {
sleep(20);
return $this->_add($a, $b);
}

public function sub($a, $b) {
sleep(10);
return $a - $b;
}

public function mul($a, $b) {
sleep(5);
return $a * $b;
}

protected function _add($a, $b) {
return $a + $b;
}
}

$server = new Yar_Server(new Operator());
$server->handle();

方式一:传统调用方式:http://localhost/operator.php

//filename client.php

方式二:实例化new yar_client对象然后调用
$client = new yar_client("http://localhost/operator.php");
var_dump($client->add(1, 2));
var_dump($client->call("add", array(3, 2)));
var_dump($client->_add(1, 2));

方式三:回调
ini_set("yar.debug", false);
$client = new yar_client("http://localhost/operator.php");

function callback($ret, $callinfo) {
print_r($ret);
echo PHP_EOL;

print_r($callinfo);
echo PHP_EOL;
}

Yar_Concurrent_Client::call("http://localhost/operator.php", "add", array(1, 2), "callback");
Yar_Concurrent_Client::call("http://localhost/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::call("http://localhost/operator.php", "mul", array(2, 2), "callback");

echo 123;

Yar_Concurrent_Client::loop();

echo 456;
  1. Yac
1
2
3
4
5
6
7
8
$yac  = new Yac();
$info = $yac->info();
$set1 = $yac->set("foo", "bar");
$get1 = $yac->get('foo');

$set2 = $yac->set(["key1" => "val1","key2" => "val2"]);
$get2 = $yac->get("key1");
$get3 = $yac->get(array("key1", "key2"));

三、参考

  1. Yaf
  2. Yar
  3. Yac
  4. Yac
  5. Yaf配置