当前位置: 首页>>代码示例>>PHP>>正文


PHP Middleware::setAttribute方法代码示例

本文整理汇总了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) . '"');
 }
开发者ID:wolfy-j,项目名称:psr7-middlewares,代码行数:16,代码来源:DigestAuthentication.php

示例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);
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:39,代码来源:AuraRouter.php

示例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 . '"');
 }
开发者ID:wolfy-j,项目名称:psr7-middlewares,代码行数:17,代码来源:BasicAuthentication.php

示例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);
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:18,代码来源:AuraSession.php

示例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);
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:18,代码来源:FormatNegotiator.php

示例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);
 }
开发者ID:jordiwes,项目名称:psr7-middlewares,代码行数:18,代码来源:LanguageNegotiator.php

示例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);
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:18,代码来源:Uuid.php

示例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);
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:18,代码来源:LanguageNegotiator.php

示例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);
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:20,代码来源:BasePath.php

示例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);
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:20,代码来源:Geolocate.php

示例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);
 }
开发者ID:jordiwes,项目名称:psr7-middlewares,代码行数:22,代码来源:Geolocate.php

示例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;
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:27,代码来源:ErrorHandler.php

示例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;
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:29,代码来源:ErrorHandler.php

示例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);
 }
开发者ID:jordiwes,项目名称:psr7-middlewares,代码行数:14,代码来源:ClientIp.php

示例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);
 }
开发者ID:schnittstabil,项目名称:psr7-middlewares,代码行数:42,代码来源:LanguageNegotiator.php


注:本文中的Psr7Middlewares\Middleware::setAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。