asmblah/fast-cgi

安装: 968

依赖: 1

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:项目

v0.1.2 2024-04-08 17:28 UTC

This package is auto-updated.

Last update: 2024-09-08 18:26:34 UTC


README

Build Status

简化了真实 php-cgi 工作进程或 php-fpm 工作池的管理。

利用优秀的 hollodotme/fast-cgi-client 实现对 FastCGI API 的支持。

为什么?

轻松测试 FastCGI 应用程序在请求之间的行为:例如,确保当预期时正确清除 opcache。

使用方法

$ composer install --dev asmblah/fast-cgi

启动 php-cgi 实例并向其发送 GET 请求

使用 PhpCgiLauncher 来启动 php-cgi

test.php

<?php

declare(strict_types=1);

use Asmblah\FastCgi\FastCgi;
use Asmblah\FastCgi\Launcher\PhpCgiLauncher;

require_once __DIR__ . '/vendor/autoload.php';

$baseDir = __DIR__;
$wwwDir = 'www'; // Relative to $baseDir.
$phpCgiBinaryPath = dirname(PHP_BINARY) . '/php-cgi';

$dataDir = $baseDir . '/var/test';
@mkdir($dataDir, 0700, true);
$socketPath = $dataDir . '/php-cgi.test.sock';

$fastCgi = new FastCgi(
    baseDir: $baseDir,
    wwwDir: $wwwDir,
    socketPath: $socketPath,
    launcher: new PhpCgiLauncher($phpCgiBinaryPath)
);
$session = $fastCgi->start();

$response = $session->sendGetRequest(
    'my_script.php',
    '/path/to/my-page',
    [
        'greeting' => 'Hello',
    ]
);

// Will print "Hello from my front controller!".
print $response->getBody() . PHP_EOL;

$session->quit();

www/my_script.php

<?php

declare(strict_types=1);

print ($_GET['greeting'] ?? '(none)') . ' from my front controller!';

运行

$ php test.php
Hello from my front controller!

启动 php-fpm 实例并向其发送 GET 请求

使用 PhpFpmLauncher 来启动 php-fpm

test.php

<?php

declare(strict_types=1);

use Asmblah\FastCgi\FastCgi;
use Asmblah\FastCgi\Launcher\PhpFpmLauncher;

require_once __DIR__ . '/vendor/autoload.php';

$baseDir = __DIR__;
$wwwDir = 'www'; // Relative to $baseDir.
$phpFpmBinaryPath = dirname(PHP_BINARY, 2) . '/sbin/php-fpm';

$dataDir = $baseDir . '/var/test';
@mkdir($dataDir, 0700, true);
$socketPath = $dataDir . '/php-fpm.test.sock';
$logFilePath = $dataDir . '/php-fpm.log';

$configFilePath = $dataDir . '/php-fpm.conf';
file_put_contents($configFilePath, <<<CONFIG
[global]
error_log = $logFilePath

[www]
listen = $socketPath
pm = static
pm.max_children = 1

CONFIG
);

$fastCgi = new FastCgi(
    baseDir: $baseDir,
    wwwDir: $wwwDir,
    socketPath: $socketPath,
    launcher: new PhpFpmLauncher(
        $phpFpmBinaryPath,
        $configFilePath
    )
);
$session = $fastCgi->start();

$response = $session->sendGetRequest(
    'my_script.php',
    '/path/to/my-page',
    [
        'greeting' => 'Hello',
    ]
);

// Will print "Hello from my front controller!".
print $response->getBody() . PHP_EOL;

$session->quit();

www/my_script.php

<?php

declare(strict_types=1);

print ($_GET['greeting'] ?? '(none)') . ' from my front controller!';

运行

$ php test.php
Hello from my front controller!

另请参阅