inpsyde/wpsr7

This package is abandoned and no longer maintained. No replacement package was suggested.

PSR-7-compliant HTTP messages for WordPress.

v1.0.0 2017-08-28 09:19 UTC

This package is auto-updated.

Last update: 2022-05-10 18:03:47 UTC


README

Version Status Build Downloads License

PSR-7-compliant HTTP messages for WordPress.

Note

Currently we don't support, maintain the solution. However feel free to use, enhance the library to makes more completely in the PSR7 context.

Introduction

In the PHP world in general, there is a standard (recommendation) when it comes to HTTP messages: PSR-7. Despite things like Calypso, Gutenberg and the growing JavaScript codebase in general, WordPress is written in PHP. Thus, wouldn’t it be nice to do what the rest of the PHP world is doing? Isn’t there some way to leverage all the existing PSR-7 middleware and incorporate them into your (RESTful) WordPress projects?

Well, here it is.

Table of Contents

Installation

Install with Composer:

$ composer require inpsyde/wpsr7

Run the tests:

$ vendor/bin/phpunit

Requirements

This package requires PHP 7 or higher.

Usage

The following sections will help you get started with using the classes included in this package to integrate existing PSR-7 middleware into your (RESTful) WordPress projects.

Creating a PSR-7-compliant WordPress REST Request

If you are interested in a PSR-7-compliant WordPress REST request object, you can, of course, create a new instance yourself. You can do this like so, with all arguments being optional:

use Inpsyde\WPSR7\REST\Request;

$request = new Request(
	$method,
	$route,
	$attributes
);

However, it is rather unlikely, because you usually do not want to define any request-based data on your own, ... since it is already included in the current request. :) More likely is that you want to make an existing WordPress REST request object PSR-7-compliant, like so:

use Inpsyde\WPSR7\REST\Request;

// ...

$request = Request::from_wp_rest_request( $request );

Creating a PSR-7-compliant WordPress REST Response

As for requests, you can also create a new response object yourself. Again, all arguments are optional.

use Inpsyde\WPSR7\REST\Response;

$response = new Response(
	$data,
	$status,
	$headers
);

While this might make somewhat more sense compared to requests, the usual case would be to make an existing WordPress REST response object PSR-7-compliant, which can be done like this:

use Inpsyde\WPSR7\REST\Response;

// ...

$response = Response::from_wp_rest_response( $response );

Using the PSR-7-compliant WordPress HTTP Messages

Once you made a WordPress HTTP message PSR-7-compliant, you can just pass it on to PSR-7 middleware. Since you can do almost anything, it doesn't make too much sense to provide any examples here. But if you think you really have a good one, we're happy to accept pull requests for the readme file. :)

License

Copyright (c) 2017 Thorsten Frommen, Inpsyde GmbH

This code is licensed under the MIT License.