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'); }
/** * Test that emit invokes the appropriate methods on the emitter. * * @return void */ public function testEmit() { $response = new Response('php://memory', 200, ['x-testing' => 'source header']); $final = $response->withHeader('X-First', 'first')->withHeader('X-Second', 'second'); $emitter = $this->getMock('Zend\\Diactoros\\Response\\EmitterInterface'); $emitter->expects($this->once())->method('emit')->with($final); $app = new MiddlewareApplication($this->config); $server = new Server($app); $server->emit($server->run(null, $response), $emitter); }
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']); }
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'); }