2017 © Pedro Peláez
 

library yin-middlewares

Woohoo Labs. Yin Middleware

image

woohoolabs/yin-middlewares

Woohoo Labs. Yin Middleware

  • Wednesday, July 4, 2018
  • by woohoolabs
  • Repository
  • 1 Watchers
  • 7 Stars
  • 139 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 17 Versions
  • 0 % Grown

The README.md

Woohoo Labs. Yin Middleware

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads [Gitter][link-support], (*1)

Woohoo Labs. Yin Middleware is a collection of middleware which helps you to integrate Woohoo Labs. Yin into your PHP applications., (*2)

Table of Contents

Introduction

Features

  • 100% PSR-15 compatibility
  • 100% PSR-7 compatibility
  • Validation of requests against the JSON schema
  • Validation of responses against the JSON and JSON:API schema
  • Dispatching of JSON:API-aware controllers
  • JSON:API exception handling

Install

The only thing you need before getting started is Composer., (*3)

Install a PSR-7 implementation:

Because Yin Middleware requires a PSR-7 implementation (a package which provides the psr/http-message-implementation virtual package), you must install one first. You may use Zend Diactoros or any other library of your preference:, (*4)

$ composer require zendframework/zend-diactoros

Install Yin Middleware:

To install the latest version of this library, run the command below:, (*5)

$ composer require woohoolabs/yin-middleware

Note: The tests and examples won't be downloaded by default. You have to use composer require woohoolabs/yin-middleware --prefer-source or clone the repository if you need them., (*6)

Yin Middleware 4.1 requires PHP 7.4 at least, but you may use Yin Middleware 4.0.0 for PHP 7.1., (*7)

Install the optional dependencies:

If you want to use JsonApiRequestValidatorMiddleware and JsonApiResponseValidatorMiddleware from the default middleware stack then you have to require the following dependencies too:, (*8)

$ composer require seld/jsonlint
$ composer require justinrainbow/json-schema

Basic Usage

Supported middleware interface design

The interface design of Yin-Middleware is based on the PSR-15 de-facto standard. That's why it is compatible with Woohoo Labs. Harmony, Zend-Stratigility, Zend-Expressive and many other frameworks., (*9)

The following sections will guide you through how to use and configure the provided middleware., (*10)

Note: When passing a ServerRequestInterface instance to your middleware dispatcher, a WoohooLabs\Yin\JsonApi\Request\JsonApiRequestInterface instance must be used in fact (the WoohooLabs\Yin\JsonApi\Request\JsonApiRequest class possibly), otherwise the JsonApiDispatcherMiddleware and the JsonApiExceptionHandlerMiddleware will throw an exception., (*11)

YinCompatibilityMiddleware

This middleware facilitates the usage of Yin and Yin-Middleware in other frameworks. It does so by upgrading a basic PSR-7 request object to JsonApiRequest, which is suitable for working with Yin. Please keep in mind, that this middleware should precede any other middleware that uses JsonApiRequest as $request parameter., (*12)

$harmony->addMiddleware(new YinCompatibilityMiddleware());

Available configuration options for the middleware (they can be passed to the constructor):, (*13)

JsonApiRequestValidatorMiddleware

The middleware is mainly useful in a development environment, and it is able to validate a PSR-7 request against the JSON and the JSON:API schema. Just add it to your application (the example is for Woohoo Labs. Harmony):, (*14)

$harmony->addMiddleware(new JsonApiRequestValidatorMiddleware());

If validation fails, an exception containing the appropriate JSON:API errors will be thrown. If you want to customize the error messages or the response, provide an Exception Factory of your own. For other customizations, feel free to extend the class., (*15)

Available configuration options for the middleware (they can be passed to the constructor):, (*16)

  • exceptionFactory: The ExceptionFactoryInterface instance to be used
  • includeOriginalMessageInResponse: If true, the original request body will be included in the "meta" top-level member
  • negotiate: If true, the middleware performs content-negotiation as specified by the JSON:API spec. In this case, the "Content-Type" and the "Accept" header is checked.
  • validateQueryParams: If true, query parameters are validated against the JSON:API specification
  • validateJsonBody: If true, the request body gets validated against the JSON schema

JsonApiResponseValidatorMiddleware

The middleware is mainly useful in a development environment, and it is able to validate a PSR-7 response against the JSON and the JSON:API schema. Just add it to your application (the example is for Woohoo Labs. Harmony):, (*17)

$harmony->addMiddleware(new JsonApiResponseValidatorMiddleware());

If validation fails, an exception containing the appropriate JSON:API errors will be thrown. If you want to customize the error messages or the response, provide an Exception Factory of your own. For other customizations, feel free to extend the class., (*18)

Available configuration options for the middleware (they can be passed to the constructor):, (*19)

  • exceptionFactory: The ExceptionFactoryInterface instance to be used
  • serializer: The SerializerInterface instance to be used
  • includeOriginalMessageInResponse: If true, the original response will be included in the "meta" top-level member
  • validateJsonBody: If true, the response body gets validated against the JSON schema
  • validateJsonApiBody: If true, the response is validated against the JSON:API schema

JsonApiDispatcherMiddleware

This middleware is able to dispatch JSON:API-aware controllers. Just add it to your application (the example is for Woohoo Labs. Harmony):, (*20)

$harmony->addMiddleware(new JsonApiDispatcherMiddleware());

This middleware works exactly as the one in Woohoo Labs. Harmony, the only difference is that it dispatches controller actions with the following signature:, (*21)

public function myAction(JsonApi $jsonApi): ResponseInterface;

instead of:, (*22)

public function myAction(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface;

The difference is subtle, as the $jsonApi object contains a PSR-7 compatible request, and PSR-7 responses can also be created with it. Learn more from the documentation of Woohoo Labs. Yin., (*23)

Available configuration options for the middleware (they can be passed to the constructor):, (*24)

  • container: A PSR-11 compliant container instance to be used to instantiate the controller
  • exceptionFactory: The ExceptionFactoryInterface instance to be used (e.g.: when dispatching fails)
  • serializer: The SerializerInterface instance to be used
  • handlerAttribute: The name of the request attribute which stores a dispatchable controller (it is usually provided by a router).

JsonApiExceptionHandlerMiddleware

It catches exceptions and responds with an appropriate JSON:API error response., (*25)

Available configuration options for the middleware (they can be passed to the constructor):, (*26)

  • errorResponsePrototype: In case of an error, this response object will be manipulated and returned
  • catching: If false, the middleware won't catch JsonApiExceptions
  • verbose: If true, additional meta information will be provided about the exception thrown
  • exceptionFactory: The ExceptionFactoryInterface instance to be used
  • serializer: The SerializerInterface instance to be used

Versioning

This library follows SemVer v2.0.0., (*27)

Change Log

Please see CHANGELOG for more information what has changed recently., (*28)

Testing

Woohoo Labs. Yin Middleware has a PHPUnit test suite. To run the tests, run the following command from the project folder after you have copied phpunit.xml.dist to phpunit.xml:, (*29)

bash $ phpunit, (*30)

Additionally, you may run docker-compose up or make test in order to execute the tests., (*31)

Contributing

Please see CONTRIBUTING for details., (*32)

Support

Please see SUPPORT for details., (*33)

Credits

License

The MIT License (MIT). Please see the License File for more information., (*34)

The Versions

04/07 2018

dev-master

9999999-dev

Woohoo Labs. Yin Middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api psr-15 yin woohoo labs.

12/02 2018

3.0.1

3.0.1.0

Woohoo Labs. Yin Middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api psr-15 yin woohoo labs.

02/02 2018

3.0.0

3.0.0.0

Woohoo Labs. Yin Middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api psr-15 yin woohoo labs.

21/11 2017

2.2.0

2.2.0.0

Woohoo Labs. Yin Middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api yin woohoo labs.

12/09 2017

2.1.0

2.1.0.0

Woohoo Labs. Yin Middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api yin woohoo labs.

12/03 2017

2.0.0

2.0.0.0

Woohoo Labs. Yin Middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api yin woohoo labs.

29/10 2016

1.0.0

1.0.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api hateoas yin woohoo labs.

22/08 2016

0.8.0

0.8.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

middleware psr-7 json api hateoas yin woohoo labs.

01/03 2016

0.7.1

0.7.1.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

28/02 2016

0.7.0

0.7.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

16/01 2016

0.6.0

0.6.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

18/11 2015

0.5.0

0.5.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

05/10 2015

0.4.0

0.4.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

23/09 2015

0.3.0

0.3.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

27/08 2015

0.2.0

0.2.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

19/08 2015

0.1.1

0.1.1.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.

17/08 2015

0.1.0

0.1.0.0 http://yin.woohoolabs.com

Woohoo Labs. Yin Middlewares

  Sources   Download

MIT

The Requires

 

The Development Requires

by Máté Kocsis

psr-7 json api hateoas middlewares yin woohoo labs.