pavlakis/slim-cli

通过 CLI 进行模拟 GET 请求并在 CLI 脚本中启用相同的应用程序入口点。

1.2 2019-10-23 21:50 UTC

This package is auto-updated.

Last update: 2024-09-08 03:26:23 UTC


README

Build Status Total Downloads Latest Stable Version codecov PHPStan

Slim 3 框架 CLI 请求中间件

此中间件将 CLI 调用转换为请求。

安装

通过 Composer

composer require pavlakis/slim-cli

用法

按照此顺序传递参数

路由 / 方法 / 查询字符串

php public/index.php /status GET event=true

将其添加到应用程序的中间件部分

$app->add(new \pavlakis\cli\CliRequest());

添加自定义参数

$app->add(
	new \pavlakis\cli\CliRequest(
		new EnvironmentProperties(['SERVER_PORT' => 9000])
	)
);

传递一个路由以进行测试

$app->get('/status', 'PHPMinds\Action\EventStatusAction:dispatch')
    ->setName('status');

检查您是否只使用了 CLI 调用

final class EventStatusAction
{
    ...

    public function dispatch(Request $request, Response $response, $args)
    {

        // ONLY WHEN CALLED THROUGH CLI
        if (PHP_SAPI !== 'cli') {
            return $response->withStatus(404)->withHeader('Location', '/404');
        }

        if (!$request->getParam('event')) {
            return $response->withStatus(404)->withHeader('Location', '/404');
        }

        ...

    }

}

或者,我们可以使用 PHP 服务器接口 (SAPI) 中间件 来进行 SAPI 检查,通过将其添加到路由中来实现

// By default returns a 403 if SAPI not part of the whitelist
$app->get('/status', 'PHPMinds\Action\EventStatusAction:dispatch')
    ->add(new Pavlakis\Middleware\Server\Sapi(["cli"]))

测试

$ composer test

贡献

请参阅 CONTRIBUTING 以获取详细信息。

鸣谢

基于 Bobby DeVeaux (@bobbyjason) 的 Gulp 骨架

许可

BSD 3-Clause 许可证。请参阅 许可证文件 以获取更多信息。