本文整理汇总了PHP中Psr7Middlewares\Middleware类的典型用法代码示例。如果您正苦于以下问题:PHP Middleware类的具体用法?PHP Middleware怎么用?PHP Middleware使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Middleware类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testMinify
public function testMinify()
{
$body = <<<EOT
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<style type="text/css">
.is-red {
color: red;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<script type="text/javascript">
document.querySelector('h1').className = 'is-red';
</script>
</body>
</html>
EOT;
$body_minified = <<<EOT
<!DOCTYPE html><html><head><title>Title</title><style type="text/css">.is-red{color:red}</style></head><body><h1>Hello world!</h1> <script type="text/javascript">document.querySelector('h1').className='is-red'</script> </body></html>
EOT;
$response = $this->response(['Content-Type' => 'text/html']);
$response->getBody()->write($body);
$dispatcher = $this->dispatcher([Middleware::Minify()]);
$response = $dispatcher($this->request(), $response);
$this->assertEquals($body_minified, (string) $response->getBody());
}
示例2: __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('Csrf middleware needs FormatNegotiator executed before');
}
if (!Middleware::hasAttribute($request, ClientIp::KEY)) {
throw new RuntimeException('Csrf middleware needs ClientIp executed before');
}
if ($this->storage === null) {
if (session_status() !== PHP_SESSION_ACTIVE) {
throw new RuntimeException('Csrf middleware needs an active php session or a storage defined');
}
if (!isset($_SESSION[$this->sessionIndex])) {
$_SESSION[$this->sessionIndex] = [];
}
$this->storage =& $_SESSION[$this->sessionIndex];
}
if (FormatNegotiator::getFormat($request) !== 'html') {
return $next($request, $response);
}
if (Utils\Helpers::isPost($request) && !$this->validateRequest($request)) {
return $response->withStatus(403);
}
$response = $next($request, $response);
return $this->insertIntoPostForms($response, function ($match) use($request) {
preg_match('/action=["\']?([^"\'\\s]+)["\']?/i', $match[0], $matches);
$action = empty($matches[1]) ? $request->getUri()->getPath() : $matches[1];
return $match[0] . $this->generateTokens($request, $action);
});
}
示例3: __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);
}
示例4: __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');
}
$ajax = Utils\Helpers::isAjax($request);
$debugBar = $this->debugBar ?: new StandardDebugBar();
//Redirection response
if (Utils\Helpers::isRedirect($response)) {
if ($debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) {
$debugBar->stackData();
}
//Html response
} elseif (FormatNegotiator::getFormat($request) === 'html') {
$renderer = $debugBar->getJavascriptRenderer();
ob_start();
echo '<style>';
$renderer->dumpCssAssets();
echo '</style>';
echo '<script>';
$renderer->dumpJsAssets();
echo '</script>';
echo $renderer->render(!$ajax);
$response = $this->inject($response, ob_get_clean());
//Ajax response
} elseif ($ajax && $this->captureAjax) {
$headers = $debugBar->getDataAsHeaders();
foreach ($headers as $name => $value) {
$response = $response->withHeader($name, $value);
}
}
return $next($request, $response);
}
示例5: __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 ($this->login($request, $username)) {
return $next(Middleware::setAttribute($request, self::KEY, $username), $response);
}
return $response->withStatus(401)->withHeader('WWW-Authenticate', 'Digest realm="' . $this->realm . '",qop="auth",nonce="' . ($this->nonce ?: uniqid()) . '",opaque="' . md5($this->realm) . '"');
}
示例6: __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 (empty($this->router)) {
throw new RuntimeException('No RouterContainer instance has been provided');
}
$matcher = $this->router->getMatcher();
$route = $matcher->match($request);
if (!$route) {
$failedRoute = $matcher->getFailedRoute();
switch ($failedRoute->failedRule) {
case 'Aura\\Router\\Rule\\Allows':
return $response->withStatus(405);
// 405 METHOD NOT ALLOWED
// 405 METHOD NOT ALLOWED
case 'Aura\\Router\\Rule\\Accepts':
return $response->withStatus(406);
// 406 NOT ACCEPTABLE
// 406 NOT ACCEPTABLE
default:
return $response->withStatus(404);
// 404 NOT FOUND
}
}
$request = Middleware::setAttribute($request, self::KEY, $route);
foreach ($route->attributes as $name => $value) {
$request = $request->withAttribute($name, $value);
}
$response = $this->executeCallable($route->handler, $request, $response);
return $next($request, $response);
}
示例7: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
foreach ($this->mapping as $middleware => $attribute) {
$request = $request->withAttribute($attribute, Middleware::getAttribute($request, $middleware));
}
return $next($request, $response);
}
示例8: testTypes
/**
* @dataProvider formatsProvider
*/
public function testTypes($url, $accept, $format)
{
$response = $this->execute([Middleware::FormatNegotiator(), function ($request, $response, $next) {
$response->getBody()->write($request->getAttribute('FORMAT'));
return $response;
}], $url, ['Accept' => $accept]);
$this->assertEquals($format, (string) $response->getBody());
}
示例9: testLanguages
/**
* @dataProvider languagesProvider
*/
public function testLanguages($acceptLanguage, array $languages, $language)
{
$response = $this->execute([Middleware::LanguageNegotiator($languages), function ($request, $response, $next) use($language) {
$response->getBody()->write($request->getAttribute('LANGUAGE'));
return $response;
}], '', ['Accept-Language' => $acceptLanguage]);
$this->assertEquals($language, (string) $response->getBody());
}
示例10: testTrailingSlash
/**
* @dataProvider pathsProvider
*/
public function testTrailingSlash($url, $result, $basePath)
{
$response = $this->execute([Middleware::trailingSlash()->basePath($basePath), function ($request, $response, $next) {
$response->getBody()->write($request->getUri()->getPath());
return $response;
}], $url);
$this->assertEquals($result, (string) $response->getBody());
}
示例11: 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());
}
示例12: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$authorization = self::parseAuthorizationHeader($request->getHeaderLine('Authorization'));
if ($authorization && $this->checkUserPassword($authorization['username'], $authorization['password'])) {
return $next(Middleware::setAttribute($request, self::KEY, $authorization['username']), $response);
}
return $response->withStatus(401)->withHeader('WWW-Authenticate', 'Basic realm="' . $this->realm . '"');
}
示例13: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$session = $this->factory->newInstance($request->getCookieParams());
if ($this->name !== null) {
$session->setName($this->name);
}
$request = Middleware::setAttribute($request, self::KEY, $session);
return $next($request, $response);
}
示例14: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$uuid = $this->generateUuid();
$request = Middleware::setAttribute($request, self::KEY, $uuid);
if (!empty($this->header)) {
$request = $request->withHeader($this->header, (string) $uuid);
}
return $next($request, $response);
}
示例15: __invoke
/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$language = $this->getFromHeader($request);
if (empty($language)) {
$language = isset($this->languages[0]) ? $this->languages[0] : null;
}
$request = Middleware::setAttribute($request, self::KEY, $language);
return $next($request, $response);
}