2017 © Pedro Peláez
 

silverstripe-vendormodule psr7-adapters

PSR-7 compliant adapters for SilverStripe HTTP classes

image

robbie/psr7-adapters

PSR-7 compliant adapters for SilverStripe HTTP classes

  • Wednesday, July 4, 2018
  • by robbieaverill
  • Repository
  • 1 Watchers
  • 1 Stars
  • 162 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 5 Versions
  • 31 % Grown

The README.md

robbie/psr7-adapters

Build Status Scrutinizer Code Quality codecov, (*1)

PSR-7 compliant, immutable adapter interfaces for SilverStripe HTTP classes., (*2)

Requirements

  • silverstripe/framework ^4.0
  • guzzlehttp/psr7

Installation

Install with Composer:, (*3)

composer require robbie/psr7-adapters

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

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:, (*5)

  • 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:, (*6)

<?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:, (*7)

<?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:, (*8)

<?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:, (*9)

<?php

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

$httpRequest is now a SilverStripe\Control\HTTPRequest instance., (*10)

The Versions

04/07 2018

dev-master

9999999-dev

PSR-7 compliant adapters for SilverStripe HTTP classes

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Robbie Averill

17/11 2017

1.0.x-dev

1.0.9999999.9999999-dev

PSR-7 compliant adapters for SilverStripe HTTP classes

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Robbie Averill

14/11 2017

1.0.0

1.0.0.0

PSR-7 compliant adapters for SilverStripe HTTP classes

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Robbie Averill

15/01 2017

0.2.0

0.2.0.0

PSR-7 compliant adapters for SilverStripe HTTP classes

  Sources   Download

BSD-3-Clause

The Requires

 

by Robbie Averill

13/01 2017

0.1.0

0.1.0.0

PSR-7 compliant adapters for SilverStripe HTTP classes

  Sources   Download

BSD-3-Clause

The Requires

 

by Robbie Averill