PHPackages                             kleijnweb/php-api-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. kleijnweb/php-api-middleware

AbandonedArchivedLibrary[HTTP &amp; Networking](/categories/http)

kleijnweb/php-api-middleware
============================

Middleware for kleijnweb/php-api-descriptions.

v1.0.0-alpha1(9y ago)16.4k2[1 PRs](https://github.com/kleijnweb/php-api-middleware/pulls)1LGPL-3.0PHPPHP ^7.0.0

Since May 15Pushed 2y ago1 watchersCompare

[ Source](https://github.com/kleijnweb/php-api-middleware)[ Packagist](https://packagist.org/packages/kleijnweb/php-api-middleware)[ RSS](/packages/kleijnweb-php-api-middleware/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (11)Versions (2)Used By (1)

This project is no longer maintained
====================================

[](#this-project-is-no-longer-maintained)

KleijnWeb\\PhpApi\\Middleware
=============================

[](#kleijnwebphpapimiddleware)

[![Build Status](https://camo.githubusercontent.com/ca0ca96e93016c5c8ed5399a22bd413c441d0e975f493a2bbaeadd302d4f698b/68747470733a2f2f7472617669732d63692e6f72672f6b6c65696a6e7765622f7068702d6170692d6d6964646c65776172652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kleijnweb/php-api-middleware)[![Coverage Status](https://camo.githubusercontent.com/ce4e6b0e4b34666ec1669192050fa9f1b4e35f74b8b790562fb0d6fa1de5524c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6b6c65696a6e7765622f7068702d6170692d6d6964646c65776172652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/kleijnweb/php-api-middleware?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/99e29d5026b4a65269cb51c7841961a14db673a08a45f5a4f80c494fef69937b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6c65696a6e7765622f7068702d6170692d6d6964646c65776172652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/kleijnweb/php-api-middleware/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/d0e341bd054fb7a0c4840444c1b7204111a8f507aaa17f03f2daca8293ca7a09/68747470733a2f2f706f7365722e707567782e6f72672f6b6c65696a6e7765622f7068702d6170692d6d6964646c65776172652f762f737461626c65)](https://packagist.org/packages/kleijnweb/php-api-middleware)

Middleware for kleijnweb/php-api-descriptions.

Components
==========

[](#components)

`DefaultPipe`
-------------

[](#defaultpipe)

A complete chain of middleware that can process OpenAPI requests from begin to end, once you add some command handlers. The middleware in this section is executed in order listed.

You can append and prepend 3rd party middleware, `ResultSerializer` is appended just before dispatching.

### `OperationMatcher`

[](#operationmatcher)

Determines which Operation object a request maps to (routing), or returning 404/405 responses respectively. Returns an request object some API description objects as well as the path parameters added as attributes.

### `BodyParsing`

[](#bodyparsing)

Parses the request body. In `DefaultPipe` uses `JsonBodyParser` but the parser is injectable.

### `ParameterAssembler`

[](#parameterassembler)

Coerces all the request data that qualify as OpenAPI parameters (bar the body) and sets them as request attributes.

### `MessageValidator`

[](#messagevalidator)

Validates the incoming request using the OpenAPI document and returns a 400 response with failure messages when invalid.

### `ParameterHydrator`

[](#parameterhydrator)

Uses `kleijnweb/php-api-hydrator` to hydrate custom typed objects and `\DateTime`.

### `CommandDispatcher`

[](#commanddispatcher)

Dead simple command dispatcher that invokes `callable`s with the arguments in order as found in the request.

### `ResponseBodyDehydrator`

[](#responsebodydehydrator)

Picks up the result of `CommandDispatcher` and produces a response object.

Example
=======

[](#example)

```
require __DIR__.'/../vendor/autoload.php';

use Doctrine\Common\Cache\ApcuCache;
use KleijnWeb\PhpApi\Descriptions\Description\Repository;
use KleijnWeb\PhpApi\Middleware\DefaultPipe;
use KleijnWeb\PhpApi\Middleware\Tests\Functional\Pet;
use Middlewares\Utils\Factory;
use Zend\Diactoros\Response\SapiEmitter;
use Zend\Diactoros\ServerRequestFactory;

$cache = new ApcuCache();

$commands = [
    '/pets/{id}:get' => function (int $id) use ($cache): Pet {
        return unserialize($cache->fetch($id));
    },
    '/pets:post'     => function (Pet $pet) use ($cache) {
        $count = $cache->fetch('count');
        $pet->setId($id = $count + 1);
        $cache->save($id, serialize($pet));
        $cache->save('count', $id);

        return $pet;
    },
];

$repository = new Repository(null, $cache);
$repository->register(__DIR__.'/../tests/Functional/petstore.yml');

$pipe = new DefaultPipe(
    $repository,
    $commands,
    ['KleijnWeb\PhpApi\Middleware\Tests\Functional']
);

$request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);

$response = $pipe->dispatch($request, function () {
    return Factory::createResponse(500);
});

(new SapiEmitter())->emit($response);
```

Verify that it works properly by starting a dev server and issuing some cURL requests:

```
php -S localhost:1234 test.php
curl -vH "Content-Type: application/json" -d '{"name":"doggie"}' http://localhost:1234/pets
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 1234 (#0)
> POST /pets HTTP/1.1
> Host: localhost:1234
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 17
>
* upload completely sent off: 17 out of 17 bytes
 POST /pets HTTP/1.1
> Host: localhost:1234
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 2
>
* upload completely sent off: 2 out of 2 bytes
 GET /pets/1 HTTP/1.1
> Host: localhost:1234
> User-Agent: curl/7.47.0
> Accept: */*
>
