webgriffe/cgi-http-kernel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

webgriffe/cgi-http-kernel

Composer 安装命令:

composer require webgriffe/cgi-http-kernel

包简介

Adapter from HttpKernelInterface to CGI. Originally was igorw/cgi-http-kerl which is abandoned.

README 文档

README

Adapter from HttpKernelInterface to CGI.

The HttpKernelInterface is a lie

You thought that you need to rewrite your whole application to use HttpFoundation in order to benefit from HttpKernelInterface functional testing?

Well, it turns out that you don't need that at all. Some very smart people came up with this thing called CGI (Common Gateway Interface) which defines an interface between a web server and a web application. It's great because it uses UNIX pipes for communication, which means it is also very easy to pretend to be a web server, and just call the app with any input and env vars on the command line by hand.

In PHP that works by using php-cgi, which fortunately ships with almost every PHP distribution. The most basic way of calling it on the command line is this:

$ php-cgi hello.php

If you want to learn how to do more advanced stuff, read the fucking CGI spec.

So what does this have to do with HttpKernelInterface? CGI and that interface do pretty much the same thing, they abstract communication between web server and app. The kernel interface does this within PHP, CGI does it in a language agnostic way.

The CgiHttpKernel translates between those two interfaces. As a user of the library you interact with it as if it were a true HttpKernelInterface app, but in the background it will actually go ahead and call php-cgi on the command line, parse the output, and return a Response instance.

For example:

$kernel = new CgiHttpKernel(__DIR__.'/../phpBB');

$request = Request::create('/index.php');
$response = $kernel->handle($request);

var_dump($response->getContent());

You can also pass a second argument to the constructor if you want all requests to go through a front controller.

$kernel = new CgiHttpKernel(__DIR__.'/../web', 'app.php');

$request = Request::create('/foo');
$response = $kernel->handle($request);

You can also set the php-cgi binary path and a process timeout (which by default is set to 60 seconds):

$kernel = new CgiHttpKernel(__DIR__.'/../web', 'app.php', '/path/to/php-cgi', 300);

// Or set the timeout to null to disable the timeout at all
$kernel = new CgiHttpKernel(__DIR__.'/../web', 'app.php', '/path/to/php-cgi', null);

The real power however comes from using libraries that integrate with the HttpKernelInterface, such as Symfony\Component\HttpKernel\Client.

$kernel = new CgiHttpKernel(__DIR__.'/../phpBB');
$client = new Client($kernel);

$crawler = $client->request('GET', '/index.php');
$this->assertGreaterThan(0, $crawler->filter('.topiclist')->count());

Is it really a lie?

Not really. The CgiHttpKernel only makes sense for functional testing, since it is quite slow. It is slow because it must spawn a new process for every request. This is also the reason why some very smart people came up with FastCGI, which is like CGI but faster.

FastCGI allows the app to start a long-running process that listens on a port and thus does not have the process spawning overhead. In PHP land this is usually managed by PHP-FPM, aka FastCGI Process Manager.

FastCGI is good for production but not really practical for testing since it needs to run in a separate process, listen on a port, requires configuration, etc.

However, there is an FcgiHttpKernel that ports the idea of this project to FastCGI. It may in fact become useful for tests.

When to use CgiHttpKernel?

It is mainly intended to write functional tests for legacy applications. That will hopefully enable you to refactor your legacy code with some confidence of not breaking stuff.

Good luck.

Specifics

Attributes

Request attributes are serialized and provided to the target script through the SYMFONY_ATTRIBUTES env variable. This means that it can get the original attributes as follows:

$attributes = unserialize($_SERVER['SYMFONY_ATTRIBUTES']);

webgriffe/cgi-http-kernel 适用场景与选型建议

webgriffe/cgi-http-kernel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.19k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 webgriffe/cgi-http-kernel 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 26.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-28