public function testLoop() { $messenger = m::mock(Messenger::class); $messenger->shouldReceive('next')->andReturn(array(0, 1, 'echo', array("hello"))); $messenger->shouldReceive('send')->with(array(1, 1, null, "hello")); $server = new ForkServer($messenger, new EchoHandler()); $server->loop(false); }
use Lvht\MsgpackRpc\StdIo; $log_path = getenv('HOME') . '/.phpcd.log'; $logger = new Logger('PHPCD'); $logger->pushHandler(new StreamHandler($log_path, Logger::DEBUG)); if ($messenger == 'json') { $messenger = new JsonMessenger(new StdIo()); } else { $messenger = new MsgpackMessenger(new StdIo()); } try { /** load autoloader for the project **/ if (is_readable($autoload_file)) { require $autoload_file; } switch ($daemon) { case 'PHPCD': $handler = new PHPCD\PHPCD($root, $logger); break; case 'PHPID': $handler = new PHPCD\PHPID($root, $logger); break; default: throw new \InvalidArgumentException('The second parameter should be PHPCD or PHPID'); } $server = new ForkServer($messenger, $handler); $server->loop(); } catch (\Throwable $e) { $logger->error($e->getMessage(), $e->getTrace()); } catch (\Exception $e) { $logger->error($e->getMessage(), $e->getTrace()); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Lvht\MsgpackRpc\Handler; use Lvht\MsgpackRpc\Server; use Lvht\MsgpackRpc\ForkServer; use Lvht\MsgpackRpc\JsonMessenger; use Lvht\MsgpackRpc\StdIo; class EchoHandler implements Handler { /** * @var Server */ private $server; public function setServer(Server $server) { $this->server = $server; } public function echo($data) { return $data; } } // if message is line terminated json string $messenger = new JsonMessenger(new StdIo()); // if message is msgpack // $messenger = new MsgpackMessenger(new StdIo()); $server = new ForkServer($messenger, new EchoHandler()); $server->loop(false);