public function index() { $promises = call_user_func(function () { foreach ($this->usernames as $username) { (yield $this->client->requestAsync('GET', 'https://api.github.com/users/' . $username)); } }); // Wait till all the requests are finished. \GuzzleHttp\Promise\all($promises)->then(function (array $responses) { $this->profiles = array_map(function ($response) { return json_decode($response->getBody(), true); }, $responses); })->wait(); // Return JSON response $response = new Response(); // StreamInterface objects are not immutable! $response->getBody()->write($this->html()); return $response->withHeader('Content-type', 'text/html'); }
public function testFourth() { $request = ServerRequestFactory::fromGlobals(); $response = new Response(); $stack = new Stack(); $stack->push(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $response->getBody()->write(1); return $response; }); $stack->push(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $response->getBody()->write(2); return $next ? $next($request, $response) : $response; }); $this->assertSame('1', (string) $stack->__invoke($request, $response)->getBody()); }
public function createResponse(ApiProblem $apiProblem) { $response = new Response('php://memory', $apiProblem->getDetail('status'), ['Content-Type' => 'application/problem+json']); $json = Json::toJson($apiProblem); $response->getBody()->write($json); return $response; }
public function testToCakeBody() { $psr = new PsrResponse('php://memory', 200, ['X-testing' => ['value', 'value2']]); $psr->getBody()->write('A message for you'); $result = ResponseTransformer::toCake($psr); $this->assertSame('A message for you', $result->body()); }
/** * @param Request $request * @param array $routeParams * @return \Zend\Diactoros\Response */ public function handle(Request $request, array $routeParams = []) { $view = $this->render($request, $routeParams); $response = new Response(); $response->getBody()->write($view->render()); return $response; }
/** * @param Request $request * @return \Zend\Diactoros\Response */ public function handle(Request $request) { $view = $this->render($request); $response = new Response(); $response->getBody()->write($view->render()); return $response; }
public function testRunSingleCheckFromAttribute() { $this->runner->method('run')->with('group-name/label-name')->willReturn($this->collection); $this->request = $this->request->withAttribute('filter', 'group-name')->withAttribute('label', 'label-name'); $responseFromFactory = new Response(); $responseFromFactory->getBody()->write('foo'); $this->resultResponseFactory->method('createResponse')->with($this->request, $this->collection)->willReturn($responseFromFactory); $finalResponse = $this->middleware->__invoke($this->request, $this->response); $this->assertSame($responseFromFactory, $finalResponse); }
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null) { if ($request->getUri()->getPath() === '/robots.txt') { $newReponse = new Response('php://memory', 200, ['Content-Type' => 'text/plain']); $newReponse->getBody()->write("User-Agent: *\nDisallow: /"); return $newReponse; } /* @var $out ResponseInterface */ $response = $out === null ? $response : $out($request, $response); return $response->withHeader(self::ROBOTS_HEADER, 'noindex, nofollow'); }
function make($status = 200, $content = '', $contentType = null, array $headers = []) { if ($contentType) { $headers['Content-Type'] = $contentType; } $response = new Response('php://memory', $status, $headers); if ($content) { $response->getBody()->write($content); } return $response; }
/** * @param string $content * @param string $filename * * @return \Psr\Http\Message\ResponseInterface */ private function fileDownload($content, $filename) { $contentDisposition = sprintf("attachment; filename*=utf-8''%s", rawurlencode($filename)); $response = new Response(); if (is_string($content)) { $body = $response->getBody(); $body->write($content); } elseif ($content instanceof StreamInterface) { $body = $content; } return $response->withBody($body)->withHeader('Content-Disposition', $contentDisposition); }
/** * User login * * @param Request $request * @param Response $response * @return Response */ public function login(Request $request = null, Response $response = null) { $app = $this->app($request); $user = new UserSession($app); $user->logout(); $assets = $this->getAssets(); $assets[] = 'view::Index/css/login.css'; $app->view->addData(['assets' => $assets]); $app->view->addData($this->getData($request)); $content = $app->view->render('view::Index/html/login.html.php'); $response->getBody()->write($content); return $response; }
public function index() { $promises = $this->getProfiles(); $profiles = []; // Wait till all the requests are finished. (new EachPromise($promises, ['concurrency' => 4, 'fulfilled' => function ($profile) use(&$profiles) { $profiles[] = $profile; }]))->promise()->wait(); // Return JSON response $response = new Response(); // StreamInterface objects are not immutable! $response->getBody()->write($this->html($profiles)); return $response->withHeader('Content-type', 'text/html'); }
public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface { $feed = null; if ($this->cache->contains('blog:xml-feed')) { $feed = $this->cache->fetch('blog:xml-feed'); } if (!$feed) { $feed = $this->generateXmlFeed(); $this->cache->save('blog:xml-feed', $feed); } $response = new Response(); $response->getBody()->write($feed); return $response->withHeader('Content-Type', 'application/atom+xml')->withHeader('Cache-Control', ['public', 'max-age=3600']); }
/** * Index action * * @param Request $request * @param Response $response * @return Response */ public function index(Request $request = null, Response $response = null) { $app = $this->app($request); // Increment counter $counter = $app->session->get('counter', 0); $counter++; $app->session->set('counter', $counter); // Add data to template $assets = $this->getAssets(['view::Index/js/index.js']); $text = $this->getText(['Loaded successfully!' => __('Loaded successfully!')]); $data = $this->getData($request, ['assets' => $assets, 'text' => $text, 'content' => 'view::Index/html/index.html.php', 'counter' => $counter]); // Render template $content = $app->view->render('view::Layout/html/layout.html.php', $data); $response->getBody()->write($content); return $response; }
public function testAttachToHtmlResponse() { $request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']); $response = new Response('php://memory', 200, ['Content-Type' => 'text/html']); $response->getBody()->write('ResponseBody'); $calledOut = false; $outFunction = function ($request, $response) use(&$calledOut) { $calledOut = true; return $response; }; $this->debugbarRenderer->expects($this->once())->method('renderHead')->willReturn('RenderHead'); $this->debugbarRenderer->expects($this->once())->method('render')->willReturn('RenderBody'); $result = call_user_func($this->middleware, $request, $response, $outFunction); $this->assertTrue($calledOut, 'Out is not called'); $this->assertSame($response, $result); $this->assertSame("ResponseBodyRenderHeadRenderBody", (string) $result->getBody()); }
/** * @param \OAuth2\Token\AccessTokenInterface|null $access_token On Symfony v3.1+ only, this argument is resolved through the new argument resolver feature * * @return \Psr\Http\Message\ResponseInterface */ public function userinfoAction(AccessTokenInterface $access_token = null) { if (null === $access_token) { $token = $this->token_storage->getToken(); if (!$token instanceof OAuth2Token) { throw new AuthenticationException('Unable to find an OAuth2 Access Token. Are you behind the OAuth2 Firewall?'); } $access_token = $token->getAccessToken(); } try { $info = $this->userinfo_endpoint->handle($access_token); $response = new Response('php://memory', 200, ['Content-Type' => sprintf('application/%s', is_array($info) ? 'json' : 'jwt'), 'Cache-Control' => 'no-store', 'Pragma' => 'no-cache']); $response->getBody()->write(is_array($info) ? json_encode($info) : $info); } catch (BaseException $e) { $response = new Response(); $e->getHttpResponse($response); } return $response; }
/** * Edit * * @param Request $request * @param Response $response * @return Response */ public function edit(Request $request = null, Response $response = null) { // All GET parameters $queryParams = $request->getQueryParams(); // All POST or PUT parameters $postParams = $request->getParsedBody(); // Single GET parameter //$title = $queryParams['title']; // // Single POST/PUT parameter //$data = $postParams['data']; // // Get routing arguments $vars = $request->getAttribute('vars'); $id = $vars['id']; // Get config value $app = $this->app($request); $env = $app->config['env']['name']; // Get GET parameter $id = $app->http->get('id'); // Increment counter $counter = $app->session->get('counter', 0); $counter++; $app->session->set('counter', $counter); $app->logger->info('My log message'); // Set locale //$app->session->set('user.locale', 'de_DE'); // //Model example //$user = new \App\Model\User($app); //$userRows = $user->getAll(); //$userRow = $user->getById($id); // // Add data to template $data = $this->getData($request, ['id' => $id, 'assets' => $this->getAssets(), 'content' => 'view::User/html/edit.html.php']); // Render template $content = $app->view->render('view::Layout/html/layout.html.php', $data); // Return new response $response->getBody()->write($content); return $response; }
public function testSimulateRequest() { $simulatedRequest = "DELETE / HTTP/1.1\r\nBar: faz\r\nHost: php-middleware.com"; $postBody = http_build_query([RequestSimulatorMiddleware::PARAM => $simulatedRequest]); $stream = new Stream('php://memory', 'wb+'); $stream->write($postBody); $request = new ServerRequest([], [], new Uri(), 'POST', $stream, ['Content-type' => 'application/x-www-form-urlencoded']); $responseBody = json_encode(['boo' => 'foo']); $response = new Response('php://memory', 200, ['content-type' => 'application/json']); $response->getBody()->write($responseBody); $next = function (ServerRequestInterface $request, ResponseInterface $response) { $this->assertSame('DELETE', $request->getMethod()); $this->assertSame('faz', $request->getHeaderLine('Bar')); return $response; }; /* @var $result ResponseInterface */ $result = call_user_func($this->middleware, $request, $response, $next); $body = (string) $result->getBody(); $this->assertContains('text/html', $result->getHeaderLine('Content-type')); $this->assertContains('{"boo":"foo"}', $body); $this->assertContains('<html>', $body); $this->assertContains('DELETE / HTTP/1.1', $body); }
public function testContinueWhenActionReturnIsFalse() { $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/')); $response = new Response(); $response->getBody()->write('oh '); $dispatcher = new Dispatcher(call_user_func(function () { (yield function () { return true; } => function () { return false; }); (yield function () { return true; } => function () { return 'matched'; }); }), $this->view, $this->stringRouter, $this->arrayRouter); $result = $dispatcher->dispatch($request, $response); $response = $result->getResponse(); $this->assertSame('oh matched', (string) $response->getBody()); }
/** * @param string $body * @param int $status * @param array $headers * * @return AbstractResponse */ protected function instance(string $body, int $status = 200, array $headers = []) : AbstractResponse { $response = new Response('php://memory', $status, $headers); $response->getBody()->write($body); return $response; }
/** * testNoError * * @return mixed * * @access public */ public function testNoError() { $response = new Response(); $response->getBody()->write('Other Body'); $request = ServerRequestFactory::fromGlobals()->withRequestTarget('/foobarbaz'); $viewd = $this->viewd; $out = $viewd($request, $response); $this->assertEquals('Other Body', (string) $out->getBody()); $this->assertEquals(200, $out->getStatusCode()); }
public function loadedParams(Request $request, Response $response) { $response->getBody()->write($this->di->get('fromFile')); return $response; }
/** * @param string $body * * @return ClientResponse */ public static function makeResponse($body = '') { $response = new ClientResponse(); $response->getBody()->write($body); return $response; }
<?php require __DIR__ . '/../vendor/autoload.php'; use Psr\Http\Message\ServerRequestInterface; use Silex\Application; use SilexPsr7\Adapter\Controller; use Zend\Diactoros\Response; $app = new Application(); $adapter = new Controller($app); $app->get('/', $adapter->createController(function (ServerRequestInterface $request) { $response = new Response(); $body = $response->getBody(); $body->write("Hello World!"); $params = $request->getServerParams(); if (isset($params['HTTP_HOST'])) { $body->write("<br>"); $body->write("<i>Server at " . $params['HTTP_HOST'] . "</i>"); } return $response; })); $app->run();