isAjax() публичный статический Метод

Check whether a request is or not ajax.
public static isAjax ( Psr\Http\Message\RequestInterface $request ) : boolean
$request Psr\Http\Message\RequestInterface
Результат boolean
Пример #1
0
 /**
  * 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);
 }
Пример #2
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $response = $next($request, $response);
     if (Utils\Helpers::getMimeType($response) === 'text/html' && !Utils\Helpers::isAjax($request)) {
         return $this->inject($response, $this->getCode());
     }
     return $response;
 }
 /**
  * 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 GoogleAnalytics middleware needs FormatNegotiator executed before');
     }
     if (FormatNegotiator::getFormat($request) === 'html' && !Utils\Helpers::isAjax($request)) {
         $response = $this->inject($response, $this->getCode());
     }
     return $next($request, $response);
 }
Пример #4
0
 /**
  * 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;
 }
Пример #5
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $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)) {
             return $response->withBody(self::createStream($file, 'r'));
         }
     }
     $response = $next($request, $response);
     //Fix the render baseUrl
     $generator = BasePath::getGenerator($request);
     if ($generator) {
         $renderer->setBaseUrl($generator($renderer->getBaseUrl()));
     }
     $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 (Utils\Helpers::getMimeType($response) === 'text/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;
 }