/** * Get body for response * * {@inheritdoc} */ public function getBody() { $body = new Stream('php://temp', 'wb+'); $body->write($this->resource->asJson()); $body->rewind(); return $body; }
/** * Creates stream from string data * * @param string $content * * @throws RuntimeException * * @return StreamInterface * * @since 3.00 */ public function createStreamFromString($content) { $stream = new Stream('php://memory', 'wb+'); $stream->write($content); $stream->rewind(); return $stream; }
/** * Create the message body. * * @param QrCode $qrCode * @return StreamInterface */ private function createBody(QrCode $qrCode) { $body = new Stream('php://temp', 'wb+'); $body->write($qrCode->get()); $body->rewind(); return $body; }
/** * @param string $content * * @return StreamInterface */ protected function createBody($content) { $body = new Stream('php://temp', 'wb+'); $body->write($content); $body->rewind(); return $body; }
/** * Create a JSON response with the given data. * * Default JSON encoding is performed with the following options, which * produces RFC4627-compliant JSON, capable of embedding into HTML. * * - JSON_HEX_TAG * - JSON_HEX_APOS * - JSON_HEX_AMP * - JSON_HEX_QUOT * - JSON_UNESCAPED_SLASHES * * @param mixed $data Data to convert to JSON. * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. * @param int $encodingOptions JSON encoding options to use. * @throws InvalidArgumentException if unable to encode the $data to JSON. */ public function __construct($data, $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS) { $body = new Stream('php://temp', 'wb+'); $body->write($this->jsonEncode($data, $encodingOptions)); $body->rewind(); $headers = $this->injectContentType('application/json', $headers); parent::__construct($body, $status, $headers); }
/** * Create a JSONP response with the given data. * * @param mixed $data Data to convert to JSON. * @param string $callback The JSONP callback function. * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. * @param int $encodingOptions JSON encoding options to use. * @throws InvalidArgumentException if $data pr $callback invalid. */ public function __construct($data, $callback, $status = 200, array $headers = [], $encodingOptions = JsonResponse::DEFAULT_JSON_FLAGS) { $this->validateCallback($callback); $body = new Stream('php://temp', 'wb+'); $body->write($callback); $body->write('('); $body->write($this->jsonEncode($data, $encodingOptions)); $body->write(');'); $body->rewind(); $headers = $this->injectContentType('application/javascript', $headers); parent::__construct($body, $status, $headers); }
/** * Create the message body. * * @param string|StreamInterface $text * @return StreamInterface * @throws InvalidArgumentException if $html is neither a string or stream. */ private function createBody($text) { if ($text instanceof StreamInterface) { return $text; } if (!is_string($text)) { throw new InvalidArgumentException(sprintf('Invalid content (%s) provided to %s', is_object($text) ? get_class($text) : gettype($text), __CLASS__)); } $body = new Stream('php://temp', 'wb+'); $body->write($text); $body->rewind(); return $body; }
public function testCreateWithJson() { $value = ['value' => 123, '_embedded' => ['key' => 'value']]; $body = json_encode($value); $stream = new Stream('php://memory', 'wb+'); $stream->write($body); $stream->rewind(); $this->storage->has('notfound')->willReturn(false); $this->storage->set('notfound', $value)->willReturn(null); $action = new CreateAction($this->storage->reveal()); $request = (new ServerRequest())->withMethod('POST')->withUri(new Uri('http://example.com/key/notfound'))->withAddedHeader('Accept', 'application/json')->withAddedHeader('Content-Type', 'application/json')->withAttribute('key', 'notfound')->withBody($stream); $response = $action($request, new Response(), function ($request, $response) { return $response; }); $json = json_decode((string) $response->getBody()); $this->assertTrue($response instanceof Response\JsonResponse); $this->assertSame(200, $response->getStatusCode()); $this->assertEmpty($json); }
protected function createBody($content) { if (is_resource($content)) { $this->resource = $content; $content = null; } $this->content = $content; if ($content instanceof StreamInterface) { return $content; } elseif (null === $content || false === $content || '' === $content) { // but not zero $stream = new Stream('php://temp', 'r'); return $stream; } $stream = new Stream('php://temp', 'wb+'); $stream->write((string) $content); $stream->rewind(); return $stream; }
/** * Embed SOAP messages in PSR-7 HTTP Requests * * @param string $name The name of the SOAP function to bind. * @param array $arguments An array of the arguments to the SOAP function. * @param array $options An associative array of options. * The location option is the URL of the remote Web service. * The uri option is the target namespace of the SOAP service. * The soapaction option is the action to call. * @param mixed $inputHeaders An array of headers to be bound along with the SOAP request. * @return RequestInterface * @throws RequestException If SOAP HTTP binding failed using the given parameters. */ public function request($name, array $arguments, array $options = null, $inputHeaders = null) { $soapRequest = $this->interpreter->request($name, $arguments, $options, $inputHeaders); if ($soapRequest->getSoapVersion() == '1') { $this->builder->isSOAP11(); } else { $this->builder->isSOAP12(); } $this->builder->setEndpoint($soapRequest->getEndpoint()); $this->builder->setSoapAction($soapRequest->getSoapAction()); $stream = new Stream('php://temp', 'r+'); $stream->write($soapRequest->getSoapMessage()); $stream->rewind(); $this->builder->setSoapMessage($stream); try { return $this->builder->getSoapHttpRequest(); } catch (RequestException $exception) { $stream->close(); throw $exception; } }
/** * @test */ public function soap12() { $interpreter = new Interpreter('http://www.webservicex.net/uszip.asmx?WSDL', ['soap_version' => SOAP_1_2]); $builder = new RequestBuilder(); $httpBinding = new HttpBinding($interpreter, $builder); $request = $httpBinding->request('GetInfoByCity', [['USCity' => 'New York']]); $this->assertTrue($request instanceof \Psr\Http\Message\RequestInterface); $response = <<<EOD <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetInfoByCityResponse xmlns="http://www.webserviceX.NET"> <GetInfoByCityResult>some information</GetInfoByCityResult> </GetInfoByCityResponse> </soap12:Body> </soap12:Envelope> EOD; $stream = new Stream('php://memory', 'r+'); $stream->write($response); $stream->rewind(); $response = new Response($stream, 200, ['Content-Type' => 'Content-Type: application/soap+xml; charset=utf-8']); $response = $httpBinding->response($response, 'GetInfoByCity'); $this->assertObjectHasAttribute('GetInfoByCityResult', $response); }
public function testMutationMethods() { $request = new CLIRequest("/", "example.com"); $headersSet = new HeadersSet(); $body = new EmptyBody(420); $response = new TierResponse($request, $headersSet, $body); $response = $response->withProtocolVersion("1.3"); $statusResponse = $response->withStatus(503); $this->assertEquals("1.3", $statusResponse->getProtocolVersion()); $this->assertEquals(503, $statusResponse->getStatusCode()); $statusResponse = $response->withStatus(504, "Immutability is preferable"); $this->assertEquals("Immutability is preferable", $statusResponse->getReasonPhrase()); $statusResponse = $response->withAddedHeader("Shamoan", "EeeHee"); $this->assertEquals("EeeHee", $statusResponse->getHeaderLine("Shamoan")); $statusResponse = $response->withoutHeader("Shamoan"); $this->assertEquals("", $statusResponse->getHeaderLine("Shamoan")); $statusResponse = $response->withAddedHeader("Shamoan", ["value1", "value2"]); $this->assertEquals("value1,value2", $statusResponse->getHeaderLine("Shamoan")); $singleResponse = $response->withHeader("Shamoan", "single_value"); $this->assertEquals("single_value", $singleResponse->getHeaderLine("Shamoan")); $singleResponse = $singleResponse->withHeader("Shamoan", "updated_single_value"); $this->assertEquals("updated_single_value", $singleResponse->getHeaderLine("Shamoan")); $singleResponse = $response->withHeader("Shamoan", "single_value"); $multipleResponse = $singleResponse->withHeader("Shamoan", ["updated_value_1", "updated_value_2"]); $this->assertEquals("updated_value_1,updated_value_2", $multipleResponse->getHeaderLine("Shamoan")); $emptyResponse = $multipleResponse->withoutHeader("Shamoan"); $this->assertEquals("", $emptyResponse->getHeaderLine("Shamoan")); $statusResponse = $response->withAddedHeader("Shamoan", ["value1", "value2"]); $this->assertEquals("value1,value2", $statusResponse->getHeaderLine("Shamoan")); $body = new Stream('php://temp', 'wb+'); $body->write("Hello world"); $body->rewind(); $bodyResponse = $response->withBody($body); $this->assertEquals("Hello world", $bodyResponse->getBody()->getContents()); }
/** * Send a HttpFoundation response to the client. * * @param int $requestId The request id to respond to * @param HttpFoundationResponse $response The HTTP foundation response message */ private function sendHttpFoundationResponse($requestId, HttpFoundationResponse $response) { $statusCode = $response->getStatusCode(); $headerData = "Status: {$statusCode}\r\n"; $headerData .= $response->headers . "\r\n"; $stream = new Stream('php://memory', 'r+'); $stream->write($response->getContent()); $stream->rewind(); $this->writeResponse($requestId, $headerData, $stream); }
/** * getBody * * @return Stream */ public function getBody() { $body = new Stream('php://temp', 'wb+'); $body->write($this->getContent()); $body->rewind(); return $body; }
private function buildRequest(Command $command, UriInterface $uri) : RequestInterface { $parameters = sprintf('-db=%s&%s', urlencode($this->database), $command); $body = new Stream('php://temp', 'wb+'); $body->write($parameters); $body->rewind(); $request = (new Request($uri->withUserInfo(''), 'POST'))->withAddedHeader('User-agent', 'SimpleFM')->withAddedHeader('Content-type', 'application/x-www-form-urlencoded')->withAddedHeader('Content-length', (string) strlen($parameters))->withBody($body); $credentials = urldecode($uri->getUserInfo()); if ($command->hasIdentity()) { Assertion::notNull($this->identityHandler, 'An identity handler must be set to use identities on commands'); $identity = $command->getIdentity(); $credentials = sprintf('%s:%s', $identity->getUsername(), $this->identityHandler->decryptPassword($identity)); } $this->logger->info(sprintf('%s?%s', (string) $uri->withUserInfo(''), $parameters)); if ('' === $credentials) { return $request; } return $request->withAddedHeader('Authorization', sprintf('Basic %s', base64_encode($credentials))); }
/** * Gets the body of the message. * * @return StreamInterface Returns the body as a stream. */ public function getBody() { if ($this->overridingStreamInterface !== null) { return $this->overridingStreamInterface; } $contents = $this->body->getData(); $body = new Stream('php://temp', 'wb+'); $body->write($contents); $body->rewind(); return $body; }
/** * Create a PSR7 request from the request spec. * * @param array $spec The request spec. * @return Psr\Http\Message\RequestInterface */ protected function _createRequest($spec) { if (isset($spec['input'])) { $spec['post'] = []; } $request = ServerRequestFactory::fromGlobals(array_merge($_SERVER, $spec['environment'], ['REQUEST_URI' => $spec['url']]), $spec['query'], $spec['post'], $spec['cookies']); $request = $request->withAttribute('session', $spec['session']); if (isset($spec['input'])) { $stream = new Stream('php://memory', 'rw'); $stream->write($spec['input']); $stream->rewind(); $request = $request->withBody($stream); } return $request; }