/** * @covers ::getContent */ public function testInstanceKeepsBody() { $header = $this->prophesize('\\Crunch\\FastCGI\\Protocol\\Header'); $header->getLength()->willReturn(3); $record = new Record($header->reveal(), 'foo'); self::assertEquals('foo', $record->getContent()); }
/** * @param Record $record * * @return RequestInterface|null */ public function pushRecord(Record $record) { $this->records[] = $record; if (!$record->getType()->isStdin() || $record->getContent()) { return; } return $this->buildRequest(); }
public function write($data) { if (!$this->writeable) { return; } $this->buffer .= $data; while (strlen($this->buffer) >= 8) { $header = Header::decode(substr($this->buffer, 0, 8)); if (strlen($this->buffer) < $header->getPayloadLength() + 8) { return; } $record = Record::decode($header, substr($this->buffer, 8, $header->getLength()) ?: ''); $this->buffer = substr($this->buffer, 8 + $header->getPayloadLength()) ?: ''; if ($record->getType()->isBeginRequest()) { if (isset($this->requestParser[$record->getRequestId()])) { throw new \Exception('RequestID already in use!'); } $this->requestParser[$record->getRequestId()] = new RequestParser(); } if ($request = $this->requestParser[$record->getRequestId()]->pushRecord($record)) { unset($this->requestParser[$record->getRequestId()]); $receiver = $this->requestReceiver; $receiver($request); } } }
private function read($data) { $this->data .= $data; while (strlen($this->data) >= 8) { $header = Header::decode(substr($this->data, 0, 8)); if (strlen($this->data) < $header->getPayloadLength() + 8) { return; } $rawRecord = substr($this->data, 8, $header->getLength()); $record = Record::decode($header, $rawRecord); $this->data = substr($this->data, 8 + $header->getPayloadLength()); if ($response = $this->responseBuilders[$header->getRequestId()]->pushRecord($record)) { $this->promises[$header->getRequestId()]->resolve($response); } } }
/** * @param Record $record * * @return Response|null * @throws \RuntimeException */ public function pushRecord(Record $record) { switch (true) { case $record->getType()->isStdout(): $this->stdout .= $record->getContent(); break; case $record->getType()->isStderr(): $this->stderr .= $record->getContent(); break; case $record->getType()->isEndRequest(): return new Response($this->requestId, new StringReader($this->stdout), new StringReader($this->stderr)); break; default: throw new \RuntimeException(sprintf('Unknown package type \'%d\'', $record->getType())); break; } }