定制 robbie/psr7-adapters 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

robbie/psr7-adapters

Composer 安装命令:

composer require robbie/psr7-adapters

包简介

PSR-7 compliant adapters for SilverStripe HTTP classes

README 文档

README

CI

PSR-7 compliant, immutable adapter interfaces for SilverStripe HTTP classes.

Requirements

  • silverstripe/framework ^6.0
  • guzzlehttp/psr7
  • PHP ^8.3

Installation

Install with Composer:

composer require robbie/psr7-adapters

Add ?flush=1 to your browser URL, flush=1 to your sake command arguments or --flush to your ssconsole.

Use

Converting to PSR-7

This module works by providing either a HTTPRequest or a HTTPResponse class that is pre-configured and ready to be sent to the client/server to the corresponding adapter class:

  • HTTPRequest uses the Robbie\Psr7\HttpRequestAdapter class
  • HTTPResponse uses the Robbie\Psr7\HttpResponseAdapter class

To retrieve a bootstrapped PSR-7 ServerRequestInterface or ResponseInterface you can call ->toPsr7($request) on either of these classes, for example:

<?php

$myResponse = new \SilverStripe\Control\HTTPResponse(
    json_encode(['success' => true, 'message' => 'Your request was successful!']),
    200,
    'OK'
);

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = (new \Robbie\Psr7\HttpResponseAdapter)->toPsr7($myResponse);

From here you can use any of the PSR-7 interface methods, and the results will be immutable:

<?php

$newResponse = $response->withHeader('Content-Type', 'application/json');
$newResponse = $newResponse->withHeader('X-Custom-Header', 'my-value-here');

// $response !== $newResponse -> #psr7-ftw

The same concept applies to the HttpRequestAdapter, for example:

<?php

# Context: PageController
use Robbie\Psr7\HttpRequestAdapter;

// ...

$request = $this->getRequest();
$adapter = new HttpRequestAdapter;
$psrInterface = $adapter->toPsr7($request);

// Outputs all your initial request headers:
print_r($psrInterface->getHeaders());

Converting from PSR-7

To return a PSR-7 interface back to either an HTTPRequest or HTTPResponse class you simply need to do the same thing as going to, only use ->fromPsr7($input) instead:

<?php

// $requestInterface is an instance of Psr\Http\Message\ServerRequestInterface
$httpRequest = (new HttpRequestAdapter)->fromPsr7($requestInterface);

$httpRequest is now a SilverStripe\Control\HTTPRequest instance.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-11-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固