承接 nimbly/capsule 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

nimbly/capsule

Composer 安装命令:

composer require nimbly/capsule

包简介

Capsule is a simple PSR-7 HTTP message and PSR-17 HTTP factory implementation.

README 文档

README

Latest Stable Version GitHub Workflow Status Codecov branch License

Capsule is a simple PSR-7 HTTP message interface and PSR-17 HTTP factory implementation.

Install

composer require nimbly/capsule

HTTP Message (PSR-7)

Request

The Request object represents an outbound HTTP request your application would like to make, typically to be used with a PSR-18 compliant HTTP client.

$request = new Request("get", "https://example.org/books");

$response = $httpClient->sendRequest($request);

ServerRequest

The ServerRequest object represents an incoming HTTP request into your application, to be used with a PSR-7 compliant HTTP framework or other library.

$serverRequest = new ServerRequest("get", "https://example.org/books");

$response = $framework->dispatch($serverRequest);

Creating from globals

Typically, you will want to create a ServerRequest instance from the PHP globals space ($_SERVER, $_POST, $_GET, $_FILES, and $_COOKIES) for your incoming requests. The ServerRequestFactory provides a createFromGlobals static method to create such an instance.

$serverRequest = ServerRequestFactory::createFromGlobals();

$response = $framework->dispatch($serverRequest);

Helpers

The ServerRequest instance offers helpers to test for and access various request property parameters.

Parsed body helpers

if( $serverRequest->hasBodyParam("foo") ){
	// Do the foo...
}

/**
 * Get a single param ("bar") from the parsed body.
 */
$bar = $serverRequest->getBodyParam("bar");

/**
 * Get *only* the provided params from the parsed body.
 */
$serverRequest->onlyBodyParams(["foo", "bar"]);

/**
 * Get all params from the parsed body *except* those provided.
 */
$serverRequest->exceptBodyParams(["foo", "bar"]);

Query param helpers

if( $serverRequest->hasQueryParam("foo") ){
	// Do the foo...
}

$foo = $serverRequest->getQueryParam("foo");

Uploaded file helpers

if( $serverRequest->hasUploadedFile("avatar") ){
	// Do something
}

$avatar = $serverRequest->getUploadedFile("avatar");

Response

The Response object represents an HTTP response to either a Request or a ServerRequest action.

$response = new Response(200, \json_encode(["foo" => "bar"]), ["Content-Type" => "application/json"]);

Response Status

Capsule provides a ResponseStatus enum with HTTP response codes and reason phrases.

$response = new Response(ResponseStatus::NOT_FOUND));
$phrase = ResponseStatus::NOT_FOUND->getPhrase();

echo $phrase; // Outputs "Not Found"

HTTP Factory (PSR-17)

Capsule includes a set of PSR-17 factory classes to be used to create Request, ServerRequest, Response, Stream, UploadedFile, and Uri instances, found in the Nimbly\Capsule\Factory namespace. These factories are typically used with other libraries that are PSR-7 agnostic. They're also useful for creating mocked instances in unit testing.

RequestFactory

$requestFactory = new RequestFactory;
$request = $requestFactory->createRequest("get", "https://api.example.com");

ServerRequestFactory

$serverRequestFactory = new ServerRequestFactory;
$serverRequest = $serverRequestFactory->createServerRequest("post", "https://api.example.com/books");

In addition, the ServerRequestFactory provides several static methods for creating server requests.

Creating ServerRequest from PHP globals

You can create a ServerRequest instance from the PHP globals space ($_POST, $_GET, $_FILES, $_SERVER, and $_COOKIES).

$serverRequest = ServerRequestFactory::createFromGlobals();

Creating ServerRequest from another PSR-7 ServerRequest

You can create a Capsule ServerRequest instance from another PSR-7 ServerRequest instance:

$serverRequest = ServerRequestFactory::createServerRequestFromPsr7($otherServerRequest);

ResponseFactory

$responseFactory = new ResponseFactory;
$response = $responseFactory->createResponse(404);

StreamFactory

Create a stream from string content

$streamFactory = new StreamFactory;
$stream = $streamFactory->createStream(\json_encode($body));

Create a stream from a file

$streamFactory = new StreamFactory;
$stream = $streamFactory->createStreamFromFile("/reports/q1.pdf");

Create a stream from any resource

$resource = \fopen("https://example.com/reports/q1.pdf", "r");

$streamFactory = new StreamFactory;
$stream = $streamFactory->createStreamFromResource($resource);

Alternatively, these methods are also available statically:

// Create a stream from a string.
$stream = StreamFactory::createFromString(\json_encode($body));

// Create a stream from a local file.
$stream = StreamFactory::createFromFile("/reports/q1.pdf");

// Create a stream from a PHP resource.
$resource = \fopen("https://example.com/reports/q1.pdf", "r");
$stream = StreamFactory::createFromResource($resource);

UploadedFileFactory

Create an UploadedFile instance

$uploadedFileFactory = new UploadedFileFactory;

$stream = StreamFactory::createFromFile("/tmp/upload");

$uploadedFile = $uploadedFileFactory->createUploadedFile(
    $stream,
    $stream->getSize(),
    UPLOAD_ERR_OK,
    "q1_report.pdf",
    "application/pdf"
);

UriFactory

The UriFactory allows you to create and parse URIs.

$uriFactory = new UriFactory;
$uri = $uriFactory->createUri("https://api.example.com/v1/books?a=Kurt+Vonnegut");

This method is also available statically:

$uri = UriFactory::createFromString("https://api.example.com/v1/books?a=Kurt+Vonnegut");

nimbly/capsule 适用场景与选型建议

nimbly/capsule 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.25M 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 nimbly/capsule 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.25M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 31
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-05