本文整理汇总了PHP中Psr7Middlewares\Middleware::createStream方法的典型用法代码示例。如果您正苦于以下问题:PHP Middleware::createStream方法的具体用法?PHP Middleware::createStream怎么用?PHP Middleware::createStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr7Middlewares\Middleware
的用法示例。
在下文中一共展示了Middleware::createStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
//If basePath does not match
if (!$this->testBasePath($request->getUri()->getPath())) {
return $next($request, $response);
}
//If the method is not allowed
if ($request->getMethod() !== 'GET') {
return $response->withStatus(405);
}
$body = Middleware::createStream();
$file = $this->getFilename($request);
//If the file does not exists, check if is gzipped
if (!is_file($file)) {
$file .= '.gz';
if (EncodingNegotiator::getEncoding($request) !== 'gzip' || !is_file($file)) {
return $response->withStatus(404);
}
$response = $response->withHeader('Content-Encoding', 'gzip');
}
self::readFile($file, $body);
$response = $response->withBody($body);
//Handle range header
$response = $this->range($request, $response);
return $next($request, $response);
}
示例2: 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());
}
示例3: __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');
}
示例4: __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)));
}
示例5: 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 = Middleware::createStream();
$body->write($html);
return $response->withBody($body);
}
return $response;
}
示例6: 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 = Middleware::createStream();
$body->write(substr($html, 0, $pos) . $code . substr($html, $pos));
return $response->withBody($body);
}
示例7: insertIntoPostForms
/**
* Insert content into all POST forms.
*
* @param ResponseInterface $response
* @param string $input
*
* @return ResponseInterface
*/
protected function insertIntoPostForms(ResponseInterface $response, $input)
{
$html = (string) $response->getBody();
$html = preg_replace_callback('/(<form\\s[^>]*method="?POST"?[^>]*>)/i', function ($match) use($input) {
return $match[0] . $input;
}, $html, -1, $count);
if ($count) {
$body = Middleware::createStream();
$body->write($html);
return $response->withBody($body);
}
return $response;
}
示例8: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
throw new RuntimeException('This middleware needs FormatNegotiator executed before');
}
$renderer = $this->debugBar->getJavascriptRenderer();
//Is an asset?
$path = $request->getUri()->getPath();
$renderPath = $renderer->getBaseUrl();
if (strpos($path, $renderPath) === 0) {
$file = $renderer->getBasePath() . substr($path, strlen($renderPath));
if (file_exists($file)) {
$body = Middleware::createStream();
$body->write(file_get_contents($file));
return $response->withBody($body);
}
}
$response = $next($request, $response);
//Fix the render baseUrl
$renderPath = Utils\Helpers::joinPath(BasePath::getBasePath($request), $renderer->getBaseUrl());
$renderer->setBaseUrl($renderPath);
$ajax = Utils\Helpers::isAjax($request);
//Redirection response
if (Utils\Helpers::isRedirect($response)) {
if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) {
$this->debugBar->stackData();
}
//Html response
} elseif (FormatNegotiator::getFormat($request) === 'html') {
if (!$ajax) {
$response = $this->inject($response, $renderer->renderHead(), 'head');
}
$response = $this->inject($response, $renderer->render(!$ajax), 'body');
//Ajax response
} elseif ($ajax && $this->captureAjax) {
$headers = $this->debugBar->getDataAsHeaders();
foreach ($headers as $name => $value) {
$response = $response->withHeader($name, $value);
}
}
return $response;
}
示例9: __invoke
/**
* Execute the middleware
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
list($headers_file, $stream_file) = $this->getCacheFilename($request);
if (is_file($stream_file) && is_file($headers_file)) {
$headers = (include $headers_file);
if (static::cacheIsFresh($headers, $stream_file)) {
$response = $response->withBody(Factory::createStream($stream_file));
foreach ($headers as $name => $header) {
$response = $response->withHeader($name, $header);
}
return $response;
}
}
$response = $next($request, $response);
if (static::isCacheable($request, $response)) {
static::writeStream($response->getBody(), $stream_file);
file_put_contents($headers_file, '<?php return ' . var_export($response->getHeaders(), true) . ';');
}
return $response;
}
示例10: handlePayload
/**
* Handle the payload.
*
* @param ServerRequestInterface $request
*
* @return ServerRequestInterface
*/
protected function handlePayload(ServerRequestInterface $request)
{
if ($request->getParsedBody() || !in_array($request->getMethod(), ['POST', 'PUT', 'DELETE'], true)) {
return $request;
}
$contentType = trim($request->getHeaderLine('Content-Type'));
//json
if (stripos($contentType, 'application/json') === 0) {
return $request->withParsedBody($this->parseJson($request->getBody()))->withBody(Middleware::createStream());
}
//urlencoded
if (stripos($contentType, 'application/x-www-form-urlencoded') === 0) {
return $request->withParsedBody($this->parseUrlEncoded($request->getBody()))->withBody(Middleware::createStream());
}
//csv
if (stripos($contentType, 'text/csv') === 0) {
return $request->withParsedBody($this->parseCsv($request->getBody()))->withBody(Middleware::createStream());
}
return $request;
}
示例11: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$whoops = $this->getWhoopsInstance($request);
//Catch errors means register whoops globally
if ($this->catchErrors) {
$whoops->register();
}
try {
$response = $next($request, $response);
} catch (\Exception $exception) {
$method = Run::EXCEPTION_HANDLER;
$whoops->allowQuit(false);
$whoops->writeToOutput(false);
$body = Middleware::createStream();
$body->write($whoops->{$method}($exception));
$response = $response->withStatus(500)->withBody($body);
}
$whoops->unregister();
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)
{
$item = $this->cache->getItem(self::getCacheKey($request));
if ($item->isHit()) {
list($headers, $body) = $item->get();
$response = $response->withBody(Middleware::createStream());
$response->getBody()->write($body);
foreach ($headers as $name => $header) {
$response = $response->withHeader($name, $header);
}
return $response;
}
$response = $next($request, $response);
if (self::isCacheable($request, $response)) {
$item->set([$response->getHeaders(), (string) $response->getBody()]);
if (($time = self::getExpiration($response)) !== null) {
$item->expiresAt($time);
}
$this->cache->save($item);
}
return $response;
}
示例13: getRedirectResponse
/**
* Returns a redirect response.
*
* @param int $redirectStatus
* @param UriInterface $uri
* @param ResponseInterface $response
*/
private static function getRedirectResponse($redirectStatus, UriInterface $uri, ResponseInterface $response)
{
return $response->withStatus($redirectStatus)->withHeader('Location', (string) $uri)->withBody(Middleware::createStream());
}
示例14: deflate
/**
* Gzip minifier using gzdeflate().
*
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
public static function deflate(ResponseInterface $response)
{
$stream = Middleware::createStream();
$stream->write(gzdeflate((string) $response->getBody()));
return $response->withHeader('Content-Encoding', 'deflate')->withBody($stream);
}
示例15: minifyJs
/**
* Minify js code.
*
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
protected function minifyJs(ResponseInterface $response)
{
$stream = Middleware::createStream();
$stream->write(JsMinify::minify((string) $response->getBody()));
return $response->withBody($stream);
}