本文整理匯總了PHP中Psr7Middlewares\Middleware::setAttribute方法的典型用法代碼示例。如果您正苦於以下問題:PHP Middleware::setAttribute方法的具體用法?PHP Middleware::setAttribute怎麽用?PHP Middleware::setAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Psr7Middlewares\Middleware
的用法示例。
在下文中一共展示了Middleware::setAttribute方法的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 ($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) . '"');
}
示例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 (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);
}
示例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)
{
$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 . '"');
}
示例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)
{
$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);
}
示例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)
{
$format = $this->getFromExtension($request) ?: $this->getFromHeader($request) ?: $this->default;
if ($format) {
$request = Middleware::setAttribute($request, self::KEY, $format);
$response = $response->withHeader('Content-Type', $this->formats[$format][0] . '; charset=utf-8');
}
return $next($request, $response);
}
示例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)
{
$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);
}
示例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)
{
$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);
}
示例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)
{
$language = $this->negotiateHeader($request->getHeaderLine('Accept-Language'), new Negotiator(), $this->languages);
if (empty($language)) {
$language = isset($this->languages[0]) ? $this->languages[0] : null;
}
$request = Middleware::setAttribute($request, self::KEY, $language);
return $next($request, $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)
{
if ($this->autodetect) {
$this->basePath(Utils\Helpers::joinPath(self::detectBasePath($request), $this->basePath));
}
$uri = $request->getUri();
$path = $this->getPath($uri->getPath());
$request = $request->withUri($uri->withPath($path));
$request = Middleware::setAttribute($request, self::KEY, $this->basePath);
return $next($request, $response);
}
示例10: __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, ClientIp::KEY)) {
throw new RuntimeException('Geolocate middleware needs ClientIp executed before');
}
$ip = ClientIp::getIp($request);
if ($ip !== null) {
$request = Middleware::setAttribute($request, self::KEY, $this->geocoder->geocode($ip));
}
return $next($request, $response);
}
示例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)
{
if (!Middleware::hasAttribute($request, ClientIp::KEY)) {
throw new RuntimeException('Geolocate middleware needs ClientIp executed before');
}
$geocoder = $this->geocoder ?: $this->getFromContainer(Geocoder::CLASS, false) ?: $this->getGeocoder();
$ip = ClientIp::getIp($request);
if ($ip) {
$ip = '123.9.34.23';
$request = Middleware::setAttribute($request, self::KEY, $geocoder->geocode($ip));
}
return $next($request, $response);
}
示例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)
{
ob_start();
try {
$response = $next($request, $response);
} catch (\Exception $exception) {
if (!$this->catchExceptions) {
throw $exception;
}
$request = Middleware::setAttribute($request, self::KEY, $exception);
$response = $response->withStatus(500);
}
ob_end_clean();
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
return $this->executeHandler($request, $response);
}
return $response;
}
示例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)
{
ob_start();
$level = ob_get_level();
try {
$response = $next($request, $response);
} catch (\Exception $exception) {
if (!$this->catchExceptions) {
throw $exception;
}
$request = Middleware::setAttribute($request, self::KEY, $exception);
$response = $response->withStatus(500);
} finally {
Utils\Helpers::getOutput($level);
}
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
return $this->executeCallable($this->handler, $request, $response);
}
return $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)
{
$request = Middleware::setAttribute($request, self::KEY, $this->scanIps($request));
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 = null;
//Use path
if ($this->usePath) {
$uri = $request->getUri();
$path = ltrim($this->getPath($uri->getPath()), '/');
$dirs = explode('/', $path, 2);
$first = array_shift($dirs);
if (!empty($first) && in_array($first, $this->languages, true)) {
$language = $first;
//remove the language in the path
$request = $request->withUri($uri->withPath('/' . array_shift($dirs)));
}
}
//Use http headers
if ($language === null) {
$language = $this->negotiateHeader($request->getHeaderLine('Accept-Language'), new Negotiator(), $this->languages);
if (empty($language)) {
$language = isset($this->languages[0]) ? $this->languages[0] : null;
}
//Redirect to a path with the language
if ($this->redirectStatus && $this->usePath) {
$path = Utils\Helpers::joinPath($this->basePath, $language, $this->getPath($uri->getPath()));
return self::getRedirectResponse($this->redirectStatus, $uri->withPath($path), $response);
}
}
$request = Middleware::setAttribute($request, self::KEY, $language);
if ($language !== null) {
$response = $response->withHeader('Content-Language', $language);
}
return $next($request, $response);
}