jeyroik/extas-protocols

There is no license information available for the latest version (3.0.0) of this package.

Extas protocols package

3.0.0 2020-08-24 10:06 UTC

This package is auto-updated.

Last update: 2024-10-24 18:58:47 UTC


README

tests codecov.io PHPStan Enabled Latest Stable Version Total Downloads Dependents

Описание

Пакет для поддержки протоколов для Extas'a.

Использование

Создаём протокол

namespace my\extas\protocols;

use extas\components\protocols\Protocol;use Psr\Http\Message\RequestInterface;

class JsonProtocol extends Protocol
{
    public function __invoke(array &$args = [], RequestInterface $request = null) : void{
    {
        $json = file_get_contents('php://input');
        if ($json) {
            $jsonData = json_decode($json, true);
            $args = array_merge($args, $jsonData);
        }
    }
}

Установка протокола

В extas-совместимой конфигурации

{
  "protocols": [
    {
      "name": "json",
      "title": "JSON protocol",
      "description": "JSON protocol, extracting from php://input",
      "accept": ["application/json", "json"],
      "class": "my\\extas\\protocols\\JsonProtocol"
    }
  ]
}

Установка

/vendor/bin/extas i

Применение

use extas\interafces\protocols\IProtocol;
use extas\components\SystemContainer;

/**
 * @param Psr\Http\Message\RequestInterface $request
 * @param Psr\Http\Message\ResponseInterface $response
 * @param array $args
 */
function ($request, $response, $args) {
    /**
     * @var $protocols IProtocol[]
     */
    $protocols = $this->protocolRepository()->all([
        IProtocol::FIELD__ACCEPT => [$request->getHeader('ACCEPT'), '*']
    ]);
    
    foreach ($protocols as $protocol) {
        $protocol($args, $request);
    }
    
    print_r($args); // содержит данные из json
}