Skip to content

Commit

Permalink
KeepaliveAssetItem class
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Feb 2, 2019
1 parent 2aea481 commit b86dcb6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 33 deletions.
1 change: 1 addition & 0 deletions build/media_src/system/joomla.asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"keepalive": {
"name": "keepalive",
"class": "KeepaliveAssetItem",
"dependencies": [
"core"
],
Expand Down
36 changes: 3 additions & 33 deletions libraries/cms/html/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,42 +375,12 @@ public static function simplecolorpicker()
* @return void
*
* @since 1.5
*
* @deprecated 5.0 Use Joomla\CMS\WebAsset\WebAssetManager::enableAsset();
*/
public static function keepalive()
{
// Only load once
if (isset(static::$loaded[__METHOD__]))
{
return;
}

$app = Factory::getApplication();
$sessionHandler = $app->get('session_handler', 'database');

// If the handler is not 'Database', we set a fixed, small refresh value (here: 5 min)
$refreshTime = 300;

if ($sessionHandler === 'database')
{
$lifeTime = $app->getSession()->getExpire();
$refreshTime = $lifeTime <= 60 ? 45 : $lifeTime - 60;

// The longest refresh period is one hour to prevent integer overflow.
if ($refreshTime > 3600 || $refreshTime <= 0)
{
$refreshTime = 3600;
}
}

// If we are in the frontend or logged in as a user, we can use the ajax component to reduce the load
$uri = 'index.php' . ($app->isClient('site') || !Factory::getUser()->guest ? '?option=com_ajax&format=json' : '');

// Add keepalive script options.
Factory::getDocument()->addScriptOptions('system.keepalive', array('interval' => $refreshTime * 1000, 'uri' => Route::_($uri)));

Factory::getDocument()->getWebAssetManager()->enableAsset('keepalive');

static::$loaded[__METHOD__] = true;
Factory::getApplication()->getDocument()->getWebAssetManager()->enableAsset('keepalive');

return;
}
Expand Down
62 changes: 62 additions & 0 deletions libraries/src/WebAsset/AssetItem/KeepaliveAssetItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\WebAsset\AssetItem;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Document\Document;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\CMS\WebAsset\WebAssetAttachBehaviorInterface;
use Joomla\CMS\WebAsset\WebAssetItem;

/**
* Web Asset Item class for Keepalive asset
*
* @since __DEPLOY_VERSION__
*/
class KeepaliveAssetItem extends WebAssetItem implements WebAssetAttachBehaviorInterface
{
/**
* Method called when asset attached to the Document.
* Useful for Asset to add a Script options.
*
* @param Document $doc Active document
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onAttachCallback(Document $doc): void
{
$app = Factory::getApplication();
$sessionHandler = $app->get('session_handler', 'database');

// If the handler is not 'Database', we set a fixed, small refresh value (here: 5 min)
$refreshTime = 300;

if ($sessionHandler === 'database')
{
$lifeTime = $app->getSession()->getExpire();
$refreshTime = $lifeTime <= 60 ? 45 : $lifeTime - 60;

// The longest refresh period is one hour to prevent integer overflow.
if ($refreshTime > 3600 || $refreshTime <= 0)
{
$refreshTime = 3600;
}
}

// If we are in the frontend or logged in as a user, we can use the ajax component to reduce the load
$uri = 'index.php' . ($app->isClient('site') || !Factory::getUser()->guest ? '?option=com_ajax&format=json' : '');

// Add keepalive script options.
$doc->addScriptOptions('system.keepalive', array('interval' => $refreshTime * 1000, 'uri' => Route::_($uri)));
}
}

0 comments on commit b86dcb6

Please sign in to comment.