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-04-24 17:55:11 UTC


README

tests codecov.io PHPStan Enabled 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f61326561616264663630623462393837313739612f6d61696e7461696e6162696c697479 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
}