Code Monkey home page Code Monkey logo

psr7-adapters's Introduction

robbie/psr7-adapters

Build Status Scrutinizer Code Quality codecov

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

Requirements

  • silverstripe/framework ^4.0
  • guzzlehttp/psr7

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.

psr7-adapters's People

Contributors

robbieaverill avatar

Watchers

Conrad Dobbs avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.