Skip to content

Commit

Permalink
add Action jrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Robert committed Apr 28, 2018
1 parent e9b8161 commit bfc4a4d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Action/JrpcAction.php
@@ -0,0 +1,40 @@
<?php

namespace JRpc\Action;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\TextResponse;

class JrpcAction implements RequestHandlerInterface
{
protected $headers;
protected $json_server;
protected $jrpc_config;

public function __construct($json_server, $jrpc_config, $headers)
{
$this->json_server = $json_server;
$this->jrpc_config = $jrpc_config;
$this->headers = $headers;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$content = "";
$method = $request->getMethod();

if('POST' === $method || ('GET' === $method && $this->jrpc_config['environment'] === 'dev')) {
$this->json_server->setReturnResponse(true);
$this->json_server->initializeClass();
$this->headers = array_merge($this->headers, ['Content-Type' => 'application/json']);
$content = ('POST' === $method) ? $this->json_server->multiHandle() : $this->json_server->getServiceMap();
} else {
$content = "";
}

return new TextResponse((string) $content , 200, $this->headers);
}

}

0 comments on commit bfc4a4d

Please sign in to comment.