|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Neo4j PHP Client and Driver package. |
| 7 | + * |
| 8 | + * (c) Nagels <https://nagels.tech> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Laudis\Neo4j\TestkitBackend; |
| 15 | + |
| 16 | +use DI\ContainerBuilder; |
| 17 | +use Exception; |
| 18 | + |
| 19 | +use function get_debug_type; |
| 20 | +use function json_decode; |
| 21 | +use function json_encode; |
| 22 | + |
| 23 | +use const JSON_THROW_ON_ERROR; |
| 24 | + |
| 25 | +use JsonException; |
| 26 | +use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface; |
| 27 | +use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface; |
| 28 | +use Laudis\Neo4j\TestkitBackend\Responses\BackendErrorResponse; |
| 29 | + |
| 30 | +use const PHP_EOL; |
| 31 | + |
| 32 | +use Psr\Container\ContainerInterface; |
| 33 | +use Psr\Log\LoggerInterface; |
| 34 | +use Throwable; |
| 35 | +use UnexpectedValueException; |
| 36 | + |
| 37 | +final class Backend |
| 38 | +{ |
| 39 | + private Socket $socket; |
| 40 | + private LoggerInterface $logger; |
| 41 | + private ContainerInterface $container; |
| 42 | + private RequestFactory $factory; |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + Socket $socket, |
| 46 | + LoggerInterface $logger, |
| 47 | + ContainerInterface $container, |
| 48 | + RequestFactory $factory, |
| 49 | + ) { |
| 50 | + $this->socket = $socket; |
| 51 | + $this->logger = $logger; |
| 52 | + $this->container = $container; |
| 53 | + $this->factory = $factory; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @throws Exception |
| 58 | + */ |
| 59 | + public static function boot(): self |
| 60 | + { |
| 61 | + $builder = new ContainerBuilder(); |
| 62 | + $builder->addDefinitions(__DIR__.'/../register.php'); |
| 63 | + $builder->useAutowiring(true); |
| 64 | + $container = $builder->build(); |
| 65 | + |
| 66 | + $logger = $container->get(LoggerInterface::class); |
| 67 | + $logger->info('Booting testkit backend ...'); |
| 68 | + Socket::setupEnvironment(); |
| 69 | + $tbr = new self(Socket::fromEnvironment(), $logger, $container, new RequestFactory()); |
| 70 | + $logger->info('Testkit booted'); |
| 71 | + |
| 72 | + return $tbr; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @throws JsonException |
| 77 | + */ |
| 78 | + public function handle(): void |
| 79 | + { |
| 80 | + while (true) { |
| 81 | + $message = $this->socket->readMessage(); |
| 82 | + if ($message === null) { |
| 83 | + $this->socket->reset(); |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + [$handler, $request] = $this->extractRequest($message); |
| 89 | + $this->properSendoff($handler->handle($request)); |
| 90 | + } catch (Throwable $e) { |
| 91 | + $this->logger->error($e->__toString()); |
| 92 | + $this->properSendoff(new BackendErrorResponse($e->getMessage())); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private function loadRequestHandler(string $name): RequestHandlerInterface |
| 98 | + { |
| 99 | + $action = $this->container->get('Laudis\\Neo4j\\TestkitBackend\\Handlers\\'.$name); |
| 100 | + if (!$action instanceof RequestHandlerInterface) { |
| 101 | + $str = printf( |
| 102 | + 'Expected action to be an instance of %s, received %s instead', |
| 103 | + RequestHandlerInterface::class, |
| 104 | + get_debug_type($action) |
| 105 | + ); |
| 106 | + throw new UnexpectedValueException($str); |
| 107 | + } |
| 108 | + |
| 109 | + return $action; |
| 110 | + } |
| 111 | + |
| 112 | + private function properSendoff(TestkitResponseInterface $response): void |
| 113 | + { |
| 114 | + $message = json_encode($response, JSON_THROW_ON_ERROR); |
| 115 | + |
| 116 | + $this->logger->debug('Sending: '.$message); |
| 117 | + $this->socket->write('#response begin'.PHP_EOL); |
| 118 | + $this->socket->write($message.PHP_EOL); |
| 119 | + $this->socket->write('#response end'.PHP_EOL); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return array{0: RequestHandlerInterface, 1: object} |
| 124 | + */ |
| 125 | + private function extractRequest(string $message): array |
| 126 | + { |
| 127 | + $this->logger->debug('Received: '.$message); |
| 128 | + /** @var array{name: string, data: iterable<array|scalar|null>} $response */ |
| 129 | + $response = json_decode($message, true, 512, JSON_THROW_ON_ERROR); |
| 130 | + |
| 131 | + $handler = $this->loadRequestHandler($response['name']); |
| 132 | + $request = $this->factory->create($response['name'], $response['data']); |
| 133 | + |
| 134 | + return [$handler, $request]; |
| 135 | + } |
| 136 | +} |
0 commit comments