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


PHP Middleware::hasAttribute方法代码示例

本文整理汇总了PHP中Psr7Middlewares\Middleware::hasAttribute方法的典型用法代码示例。如果您正苦于以下问题:PHP Middleware::hasAttribute方法的具体用法?PHP Middleware::hasAttribute怎么用?PHP Middleware::hasAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Psr7Middlewares\Middleware的用法示例。


在下文中一共展示了Middleware::hasAttribute方法的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 (!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);
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:42,代码来源:DebugBar.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 (!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);
     });
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:39,代码来源:Csrf.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)
 {
     if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
         throw new RuntimeException('The Piwik middleware needs FormatNegotiator executed before');
     }
     if (FormatNegotiator::getFormat($request) === 'html' && !Utils\Helpers::isAjax($request)) {
         $response = $this->inject($response, $this->getCode());
     }
     return $next($request, $response);
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:19,代码来源:Piwik.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)
 {
     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

示例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 (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
         throw new RuntimeException('Minify middleware needs FormatNegotiator executed before');
     }
     $resolver = $this->resolver ?: new Transformers\Minifier();
     $transformer = $resolver->resolve(FormatNegotiator::getFormat($request));
     if ($transformer) {
         $response = $transformer($response);
     }
     return $next($request, $response);
 }
开发者ID:alexpts,项目名称:psr7-middlewares,代码行数:21,代码来源:Minify.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)
 {
     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

示例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)
 {
     if (!Middleware::hasAttribute($request, EncodingNegotiator::KEY)) {
         throw new RuntimeException('Gzip middleware needs EncodingNegotiator executed before');
     }
     $resolver = $this->resolver ?: new Transformers\Encoder();
     $transformer = $resolver->resolve(EncodingNegotiator::getEncoding($request));
     $response = $next($request, $response);
     if ($transformer) {
         $response = $transformer($response);
     }
     return $response;
 }
开发者ID:schnittstabil,项目名称:psr7-middlewares,代码行数:22,代码来源:Gzip.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)
 {
     if (!Middleware::hasAttribute($request, ClientIp::KEY)) {
         throw new RuntimeException('AccessLog middleware needs ClientIp executed before');
     }
     $message = $this->combined ? self::combinedFormat($request, $response) : self::commonFormat($request, $response);
     if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
         $this->logger->error($message);
     } else {
         $this->logger->info($message);
     }
     return $next($request, $response);
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:22,代码来源:AccessLog.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 (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
         throw new RuntimeException('FormTimestamp middleware needs FormatNegotiator executed before');
     }
     if (FormatNegotiator::getFormat($request) !== 'html') {
         return $next($request, $response);
     }
     if ($this->isPost($request) && !$this->isValid($request)) {
         return $response->withStatus(403);
     }
     $response = $next($request, $response);
     return $this->insertIntoPostForms($response, '<input type="hidden" name="' . $this->inputName . '" value="' . $this->encrypt(time()) . '">');
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:23,代码来源:FormTimestamp.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('Recaptcha middleware needs ClientIp executed before');
     }
     if (Utils\Helpers::isPost($request)) {
         $recaptcha = new GoogleRecaptcha($this->secret);
         $data = $request->getParsedBody();
         $res = $recaptcha->verify(isset($data['g-recaptcha-response']) ? $data['g-recaptcha-response'] : '', ClientIp::getIp($request));
         if (!$res->isSuccess()) {
             return $response->withStatus(403);
         }
     }
     return $next($request, $response);
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:24,代码来源:Recaptcha.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, FormatNegotiator::KEY)) {
         throw new RuntimeException('Honeypot middleware needs FormatNegotiator executed before');
     }
     if (FormatNegotiator::getFormat($request) !== 'html') {
         return $next($request, $response);
     }
     if (Utils\Helpers::isPost($request) && !$this->isValid($request)) {
         return $response->withStatus(403);
     }
     $response = $next($request, $response);
     return $this->insertIntoPostForms($response, function ($match) {
         return $match[0] . '<input type="text" name="' . $this->inputName . '" class="' . $this->inputClass . '">';
     });
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:25,代码来源:Honeypot.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)
 {
     if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
         throw new RuntimeException('ResponsiveImage middleware needs FormatNegotiator executed before');
     }
     //If it's not an image or basePath does not match or invalid transform values, don't do anything
     if (!in_array(FormatNegotiator::getFormat($request), ['jpg', 'jpeg', 'gif', 'png']) || !$this->testBasePath($request->getUri()->getPath()) || !($info = $this->parsePath($request->getUri()->getPath()))) {
         return $next($request, $response);
     }
     list($path, $transform) = $info;
     $uri = $request->getUri()->withPath($path);
     $request = $request->withUri($uri);
     $response = $next($request, $response);
     //Check the response and transform the image
     if ($transform && $response->getStatusCode() === 200 && $response->getBody()->getSize()) {
         return $this->transform($response, $transform);
     }
     return $response;
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:28,代码来源:ImageTransformer.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)
 {
     if ($this->forCache && !static::isCacheable($request, $response)) {
         return $next($request, $response);
     }
     if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
         throw new RuntimeException('Minify middleware needs FormatNegotiator executed before');
     }
     switch (FormatNegotiator::getFormat($request)) {
         case 'css':
             return $next($request, $this->minifyCss($response));
         case 'js':
             return $next($request, $this->minifyJs($response));
         case 'html':
             return $next($request, $this->minifyHtml($response));
         default:
             return $next($request, $response);
     }
 }
开发者ID:jordiwes,项目名称:psr7-middlewares,代码行数:28,代码来源:Minify.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)
 {
     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;
 }
开发者ID:basz,项目名称:psr7-middlewares,代码行数:51,代码来源:DebugBar.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)
 {
     if (!Middleware::hasAttribute($request, ClientIp::KEY)) {
         throw new RuntimeException('Firewall middleware needs ClientIp executed before');
     }
     $ips = ClientIp::getIps($request) ?: [];
     $firewall = new IpFirewall();
     if (!empty($this->trusted)) {
         $firewall->addList($this->trusted, 'trusted', true);
     }
     if (!empty($this->untrusted)) {
         $firewall->addList($this->untrusted, 'untrusted', false);
     }
     foreach ($ips as $ip) {
         $ok = $firewall->setIpAddress($ip)->handle();
         if (!$ok) {
             return $response->withStatus(403);
         }
     }
     return $next($request, $response);
 }
开发者ID:snapshotpl,项目名称:psr7-middlewares,代码行数:30,代码来源:Firewall.php


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