/** * {@inheritdoc} */ public function __toString() { try { return Util::copyToString($this); } catch (Exception $e) { return ''; } }
/** * {@inheritdoc} */ public function createServerRequestFromGlobals() { $server = $_SERVER; $method = $server['REQUEST_METHOD'] ?? 'GET'; $headers = function_exists('getallheaders') ? getallheaders() : []; $uri = self::getUriFromGlobals(); $body = new LazyOpenStream('php://input', 'r+'); $protocol = isset($server['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1'; $serverRequest = new ServerRequest($uri, $method, $headers, $body, $protocol, $server); return $serverRequest->withCookieParams($_COOKIE)->withQueryParams($_GET)->withParsedBody($_POST)->withUploadedFiles(Util::normalizeFiles($_FILES)); }
/** * {@inheritdoc} */ public function getContents() { return Util::copyToString($this); }
/** * Creates the underlying stream lazily when required. * * @return \Psr\Http\Message\StreamInterface */ protected function createStream() : StreamInterface { return new Stream(Util::tryFopen($this->filename, $this->mode)); }
/** * {@inheritdoc} * * @see http://php.net/is_uploaded_file * @see http://php.net/move_uploaded_file * * @param string $targetPath Path to which to move the uploaded file. * * @throws RuntimeException if the upload was not successful. * @throws InvalidArgumentException if the $path specified is invalid. * @throws RuntimeException on any error during the move operation, or on * the second or subsequent call to the method. */ public function moveTo($targetPath) { $this->validateActive(); if ($this->isStringNotEmpty($targetPath) === false) { throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string'); } if ($this->file) { $this->moved = php_sapi_name() == 'cli' ? rename($this->file, $targetPath) : move_uploaded_file($this->file, $targetPath); } else { Util::copyToStream($this->getStream(), new LazyOpenStream($targetPath, 'w')); $this->moved = true; } if ($this->moved === false) { throw new RuntimeException(sprintf('Uploaded file could not be moved to %s', $targetPath)); } }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Invalid value in files specification */ public function testNormalizeFilesRaisesException() { Util::normalizeFiles(['test' => 'something']); }
/** * @param string|resource $stream */ public function __construct($stream = 'php://input') { $stream = Util::tryFopen($stream, 'r'); $this->stream = new Stream($stream); }