Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christophe Robert
committed
Apr 28, 2018
1 parent
e9b8161
commit bfc4a4d
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |