/** * Encodes request into an traversable of records. * * @return Traversable|Record[] */ public function toRecords() { $result = [new Record(new Header(RecordType::beginRequest(), $this->getRequestId(), 8), \pack('xCCxxxxx', $this->role->value(), 0xff & ($this->keepConnection ? 1 : 0)))]; foreach ($this->getParameters()->encode($this->getRequestId()) as $value) { $result[] = $value; } while ($chunk = $this->stdin->read(65535)) { $result[] = new Record(new Header(RecordType::stdin(), $this->getRequestId(), strlen($chunk)), $chunk); } $result[] = new Record(new Header(RecordType::stdin(), $this->getRequestId(), 0, 0), ''); return new ArrayIterator($result); }
private function buildRequest() { /** @var Record $record */ $record = array_shift($this->records); $role = Role::instance(ord($record->getContent()[1])); $keepConnection = $record->getContent()[2] !== ""; $params = ''; while ($this->records && $this->records[0]->getType()->isParams()) { /** @var Record $record */ $record = array_shift($this->records); $params .= $record->getContent(); } $stdin = ''; while ($this->records && $this->records[0]->getType()->isStdin()) { /** @var Record $record */ $record = array_shift($this->records); $stdin .= $record->getContent(); } if ($this->records) { // TODO Not empty, something went wrong } return new Request($role, $record->getRequestId(), $keepConnection, RequestParameters::decode($params), new StringReader($stdin)); }
/** * @covers ::getStdin */ public function testInstanceKeepsStdin() { $request = new Request(Role::responder(), 5, false, new RequestParameters(['foo' => 'bar']), new StringReader('baz')); self::assertEquals('baz', $request->getStdin()->read(3)); }
/** * Creates a new responder request. * * Although you can create a Request instance manually it is highly * recommended to use this factory method, because only this one * ensures, that the request uses a previously unused request id. * * @param RequestParametersInterface|null $parameters * @param ReaderInterface|null $stdin * * @return RequestInterface */ public function newRequest(RequestParametersInterface $parameters = null, ReaderInterface $stdin = null) { return new Request(Role::responder(), $this->nextRequestId++, true, $parameters, $stdin); }
/** * @covers ::filter */ public function testDirectInstanciationMethodOfFilter() { $expectedRole = Role::instance(Role::FILTER); $role = Role::filter(); self::assertSame($expectedRole, $role); }