Created
April 9, 2021 14:40
-
-
Save bwaidelich/6f39539fc5a42c6b44692b913835fa2c to your computer and use it in GitHub Desktop.
Neos Eel evaluator as XML-RPC service (just a crazy demo, don't use this in production!)
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
<?php | |
declare(strict_types=1); | |
namespace Some\Package; | |
use GuzzleHttp\Psr7\Response; | |
use Neos\Eel\Context; | |
use Neos\Eel\EelEvaluatorInterface; | |
use Psr\Http\Message\ResponseInterface; | |
use Psr\Http\Message\ServerRequestInterface; | |
use Psr\Http\Server\MiddlewareInterface; | |
use Psr\Http\Server\RequestHandlerInterface; | |
final class EelRpcMiddleware implements MiddlewareInterface | |
{ | |
private EelEvaluatorInterface $eelEvaluator; | |
public function __construct(EelEvaluatorInterface $eelEvaluator) | |
{ | |
$this->eelEvaluator = $eelEvaluator; | |
} | |
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface | |
{ | |
// FIXME better way to detect XML RPC calls? | |
if ($request->getHeaderLine('Content-Type') !== 'text/xml') { | |
return $next->handle($request); | |
} | |
$server = xmlrpc_server_create(); | |
xmlrpc_server_register_method($server, 'eel', function(string $methodName, array $params) { | |
try { | |
return $this->eelEvaluator->evaluate($params[0], new Context($params[1] ?? [])); | |
} catch (\Throwable $exception) { | |
return ['faultCode' => 1, 'faultString' => $exception->getMessage()]; | |
} | |
}); | |
$response = xmlrpc_server_call_method($server, $request->getBody()->getContents(), null); | |
xmlrpc_server_destroy($server); | |
return new Response(200, ['Content-Type' => 'Content-Type: text/xml'], $response); | |
} | |
} |
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
Neos: | |
Flow: | |
http: | |
middlewares: | |
'EelRpc': | |
middleware: 'Some\Package\EelRpcMiddleware' | |
position: 'before routing' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DISCLAIMER: Don't use this example without modifications. Exposing the (whole) power of Eel to the client is unsafe!
Example request payload: