[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/WebAsset/AssetItem/ -> KeepaliveAssetItem.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\WebAsset\AssetItem;
  11  
  12  use Joomla\CMS\Document\Document;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Router\Route;
  15  use Joomla\CMS\WebAsset\WebAssetAttachBehaviorInterface;
  16  use Joomla\CMS\WebAsset\WebAssetItem;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Web Asset Item class for Keepalive asset
  24   *
  25   * @since  4.0.0
  26   */
  27  class KeepaliveAssetItem extends WebAssetItem implements WebAssetAttachBehaviorInterface
  28  {
  29      /**
  30       * Method called when asset attached to the Document.
  31       * Useful for Asset to add a Script options.
  32       *
  33       * @param   Document  $doc  Active document
  34       *
  35       * @return void
  36       *
  37       * @since   4.0.0
  38       */
  39      public function onAttachCallback(Document $doc)
  40      {
  41          $app            = Factory::getApplication();
  42          $sessionHandler = $app->get('session_handler', 'database');
  43  
  44          // If the handler is not 'Database', we set a fixed, small refresh value (here: 5 min)
  45          $refreshTime = 300;
  46  
  47          if ($sessionHandler === 'database') {
  48              $lifeTime    = $app->getSession()->getExpire();
  49              $refreshTime = $lifeTime <= 60 ? 45 : $lifeTime - 60;
  50  
  51              // The longest refresh period is one hour to prevent integer overflow.
  52              if ($refreshTime > 3600 || $refreshTime <= 0) {
  53                  $refreshTime = 3600;
  54              }
  55          }
  56  
  57          // If we are in the frontend or logged in as a user, we can use the ajax component to reduce the load
  58          $uri = 'index.php' . ($app->isClient('site') || !Factory::getUser()->guest ? '?option=com_ajax&format=json' : '');
  59  
  60          // Add keepalive script options.
  61          $doc->addScriptOptions('system.keepalive', array('interval' => $refreshTime * 1000, 'uri' => Route::_($uri)));
  62      }
  63  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer