本文整理汇总了PHP中Psr\Http\Message\ResponseInterface::withBody方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::withBody方法的具体用法?PHP ResponseInterface::withBody怎么用?PHP ResponseInterface::withBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::withBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* Create response.
* @param FilesystemInterface $cache Cache file system.
* @param string $path Cached file path.
* @return Response Response object.
*/
public function create(FilesystemInterface $cache, $path)
{
$stream = $this->streamCallback->__invoke($cache->readStream($path));
$contentType = $cache->getMimetype($path);
$contentLength = (string) $cache->getSize($path);
$cacheControl = 'max-age=31536000, public';
$expires = date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT';
return $this->response->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength)->withHeader('Cache-Control', $cacheControl)->withHeader('Expires', $expires);
}
示例2: create
/**
* Create response.
* @param FilesystemInterface $cache Cache file system.
* @param string $path Cached file path.
* @return ResponseInterface Response object.
*/
public function create(FilesystemInterface $cache, $path)
{
$stream = $this->streamCallback->__invoke($cache->readStream($path));
$contentType = $cache->getMimetype($path);
$contentLength = (string) $cache->getSize($path);
$cacheControl = 'max-age=31536000, public';
$expires = date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT';
if ($contentType === false) {
throw new FilesystemException('Unable to determine the image content type.');
}
if ($contentLength === false) {
throw new FilesystemException('Unable to determine the image content length.');
}
return $this->response->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength)->withHeader('Cache-Control', $cacheControl)->withHeader('Expires', $expires);
}
示例3: render
public function render(ResponseInterface $response, $templateName, array $data = [])
{
$source = $this->fetch($templateName, $data);
$body = $response->getBody();
$body->write($source);
return $response->withBody($body);
}
示例4: __wakeup
public function __wakeup()
{
// We re-create the stream of the response
if ($this->response !== null) {
$this->response = $this->response->withBody(\GuzzleHttp\Psr7\stream_for($this->responseBody));
}
}
示例5: withBody
/**
* Proxy to PsrResponseInterface::withBody()
*
* {@inheritdoc}
*/
public function withBody(StreamInterface $body)
{
if ($this->complete) {
return $this;
}
$new = $this->psrResponse->withBody($body);
return new self($new);
}
示例6: withBody
/**
* Proxy to PsrResponseInterface::withBody()
*
* {@inheritdoc}
* @throws RuntimeException if response is already completed
*/
public function withBody(StreamInterface $body)
{
if ($this->complete) {
throw $this->responseIsAlreadyCompleted(__METHOD__);
}
$new = $this->psrResponse->withBody($body);
return new self($new);
}
示例7: transform
/**
* Transform the image.
*
* @param ResponseInterface $response
* @param string $transform
*
* @return ResponseInterface
*/
private function transform(ResponseInterface $response, $transform)
{
$image = Image::createFromString((string) $response->getBody());
$image->transform($transform);
$body = Middleware::createStream();
$body->write($image->getString());
return $response->withBody($body)->withHeader('Content-Type', $image->getMimeType());
}
示例8: marshalResponse
public function marshalResponse(ViewModelInterface $model, ResponseInterface $response)
{
$template = $model->getTemplate();
$vars = $model->getVariables();
$stream = $response->getBody()->detach();
$this->assign($vars);
$this->render($template, $stream);
return $response->withBody(new Stream($stream));
}
示例9: __invoke
/**
* Execute the middleware.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
if ($request->getUri()->getPath() === '/robots.txt') {
$body = Middleware::createStream();
$body->write("User-Agent: *\nDisallow: /");
return $response->withBody($body);
}
$response = $next($request, $response);
return $response->withHeader(self::HEADER, 'noindex, nofollow, noarchive');
}
示例10: fetchFromCache
private function fetchFromCache(string $id, Response $res)
{
$cachePath = sprintf('%s/%s', $this->cachePath, $id);
if (!file_exists($cachePath)) {
// Nothing in cache, but should be cached
return false;
}
// Cache hit!
return $res->withBody(new Stream(fopen($cachePath, 'r')));
}
示例11: insertIntoPostForms
/**
* Insert content into all POST forms.
*
* @param ResponseInterface $response
* @param callable $replace
*
* @return ResponseInterface
*/
private function insertIntoPostForms(ResponseInterface $response, callable $replace)
{
$html = (string) $response->getBody();
$html = preg_replace_callback('/(<form\\s[^>]*method=["\']?POST["\']?[^>]*>)/i', $replace, $html, -1, $count);
if (!empty($count)) {
$body = self::createStream();
$body->write($html);
return $response->withBody($body);
}
return $response;
}
示例12: __invoke
/**
* Execute the middleware.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
if ($request->getMethod() !== 'GET') {
return $response->withStatus(405);
}
$file = $this->getFilename($request);
if (!is_file($file)) {
return $response->withStatus(404);
}
return $next($request, $response->withBody(Middleware::createStream($file)));
}
示例13: __invoke
/**
* Process an incoming error, along with associated request and response.
*
* Accepts an error, a server-side request, and a response instance, and
* does something with them; if further processing can be done, it can
* delegate to `$out`.
*
* @see MiddlewareInterface
* @param mixed $error
* @param Request $request
* @param Response $response
* @param null|callable $out
* @return null|Response
*/
public function __invoke($error, Request $request, Response $response, callable $out = null)
{
//Show error in browser if display_errors is on in php.ini
if ($this->displayErrors) {
$body = $response->getBody();
$body->write($error);
return $response->withBody($body);
}
$body = $response->getBody();
$body->write('500 Internal Server Error');
return $response->withStatus(500)->withBody($body);
}
示例14: inject
/**
* Inject some code just before any tag.
*
* @param ResponseInterface $response
* @param string $code
* @param string $tag
*
* @return ResponseInterface
*/
private function inject(ResponseInterface $response, $code, $tag = 'body')
{
$html = (string) $response->getBody();
$pos = strripos($html, "</{$tag}>");
if ($pos === false) {
$response->getBody()->write($code);
return $response;
}
$body = self::createStream();
$body->write(substr($html, 0, $pos) . $code . substr($html, $pos));
return $response->withBody($body);
}
示例15: after
public function after(ResponseInterface $response)
{
if (config('micro_debug.handlers.fire_php')) {
$db = \app('db');
if ($db) {
$profiler = $db->getProfiler();
if ($profiler->getEnabled()) {
$totalTime = $profiler->getTotalElapsedSecs();
$queryCount = $profiler->getTotalNumQueries();
$longestTime = 0;
$longestQuery = \null;
$total = sprintf('%.6f', microtime(\true) - $_SERVER['REQUEST_TIME_FLOAT']);
$label = 'Executed ' . $queryCount . ' queries in ' . sprintf('%.6f', $totalTime) . ' seconds. (' . ($total ? \round($totalTime / $total * 100, 2) : 0) . '%)';
$table = [];
$table[] = ['Time', 'Event', 'Parameters'];
if ($profiler->getQueryProfiles()) {
foreach ($profiler->getQueryProfiles() as $k => $query) {
if ($query->getElapsedSecs() > $longestTime) {
$longestTime = $query->getElapsedSecs();
$longestQuery = $k;
}
}
foreach ($profiler->getQueryProfiles() as $k => $query) {
$table[] = [\sprintf('%.6f', $query->getElapsedSecs()) . ($k == $longestQuery ? ' !!!' : ''), $query->getQuery(), ($params = $query->getQueryParams()) ? $params : \null];
}
}
FirePHP\FirePHP::getInstance()->table('DB - ' . $label, $table);
}
}
}
if (\config('micro_debug.handlers.dev_tools')) {
if ($response instanceof HtmlResponse) {
$body = $response->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$b = $body->getContents();
$b = explode('</body>', $b);
$b[0] .= str_replace(array("\n", "\t", "\r"), "", $this->view->render()) . '</body>';
$response->withBody(new TempStream(implode('', $b)));
}
}
if (($fileForCache = \config('micro_debug.handlers.performance')) !== \null) {
$forStore = [];
foreach (\MicroLoader::getFiles() as $class => $file) {
if (\substr($class, 0, 6) === 'Micro\\') {
$forStore[$class] = $file;
}
}
\file_put_contents($fileForCache, "<?php\nreturn " . \var_export($forStore, \true) . ";", \LOCK_EX);
}
return $response;
}