/** * @param array $server * @param array $get * @param array $post * @param array $cookies * @param array $files * @param \Psr\Http\Message\StreamInterface $stream * @return \Psr\Http\Message\ServerRequestInterface */ public function factory(array $server, array $get, array $post, array $cookies, array $files, StreamInterface $stream = null) { if (!isset($stream)) { $stream = new Stream('php://memory'); } $headers = $this->getHeadersFromServerParams($server); if (isset($headers['content-type']) && strpos($headers['content-type'][0], 'application/json') === 0) { $post = json_decode($stream->__toString(), true); } return new ServerRequest($server, $cookies, $get, $this->fileFactory->fromFiles($files), $post, [], isset($server['REQUEST_METHOD']) ? $server['REQUEST_METHOD'] : 'GET', $this->getUri($server), '1.1', $headers, $stream); }
public function testCloseAndException() { $stream = new Stream('php://memory', 'w+'); $stream->close(); $stream->close(); $this->assertFalse($stream->isWritable()); $this->assertFalse($stream->isReadable()); $this->assertFalse($stream->isSeekable()); $this->assertSame('', $stream->__toString()); $this->assertNull($stream->getSize()); $this->assertTrue($stream->eof()); try { $stream->write('...?'); $this->fail(); } catch (RuntimeException $e) { $this->assertEquals('No resource available.', $e->getMessage()); } }
/** * @param array $server * @param array $get * @param array $post * @param array $cookies * @param array $files * @param \Psr\Http\Message\StreamInterface $stream * @return \Psr\Http\Message\ServerRequestInterface */ public function create(array $server, array $get, array $post, array $cookies, array $files, StreamInterface $stream = null) { if (!isset($stream)) { $stream = new Stream('php://memory'); } $headers = $this->getPsrHeadersFromServerParams($server); // exists body, but not exists posts $bodyContent = $stream->__toString(); if ($bodyContent !== '' && count($post) === 0) { if (isset($headers['content-type'])) { // do not define multipart/form-data // because, it use only in POST method. // ref. en: https://issues.apache.org/jira/browse/FILEUPLOAD-197#comment-13595136 // ref. kr: https://blog.outsider.ne.kr/1001 if (strpos($headers['content-type'], 'application/json') === 0) { $jsonBody = json_decode($bodyContent, true); $post = $jsonBody ? $jsonBody : $post; } elseif (strpos($headers['content-type'], 'application/x-www-form-urlencoded') === 0) { parse_str($bodyContent, $post); } } } return new ServerRequest($server, $get, $post, $cookies, $this->fileFactory->createFromFiles($files), [], isset($server['REQUEST_METHOD']) ? $server['REQUEST_METHOD'] : 'GET', $this->getUri($server), $stream, $headers, '1.1'); }
/** * Set up for testing. * * @author Benjamin Carl <*****@*****.**> */ protected function setUp() { // Get a null driver (testing) based instance of cache pool $cacheItemPool = new CacheItemPool('Null'); // Dummy factory One $cacheItemFactory = function ($key) { return new CacheItem($key); }; // Dummy factory Two $cacheItemKeyFactory = function (RequestInterface $request) { return sha1(serialize($request)); }; // Create instance of CachingMiddleware for testing $this->cachingMiddleware = new CachingMiddleware($cacheItemPool, $cacheItemFactory, $cacheItemKeyFactory); // Create a default body for testing $this->body = new Stream('php://memory', 'w'); $this->body->write('<html><head><title>Test</title></head><body><h1>Hello World!</h1></body></html>'); // Create fake next callable $this->next = function (Request $request, Response $response) { return $response; }; // Map globals for inject in later use $this->server = $_SERVER; $this->cookie = $_COOKIE; $this->request = $_REQUEST; $this->files = $_FILES; }
public function __construct() { $stream = new Stream('php://input'); parent::__construct($stream->__toString()); }
/** * Builds response instance with HTML body from HTML passed in. * * @param string $html HTML used for response body * @param \Psr\Http\Message\ResponseInterface $response Response used as base for response * * @author Benjamin Carl <*****@*****.**> * * @return \Psr\Http\Message\ResponseInterface */ protected function buildResponse($html, ResponseInterface $response) { // @codeCoverageIgnoreStart $body = new Stream('php://memory', 'w'); $body->write($html); $response = $response->withBody($body); return $response; // @codeCoverageIgnoreEnd }