Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build/media_source/system/joomla.asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"assets": {
"core": {
"name": "core",
"class": "CoreAssetItem",
"js": [
"media/system/js/core.min.js"
]
},
"keepalive": {
"name": "keepalive",
"class": "KeepaliveAssetItem",
"dependencies": [
"core"
],
Expand Down
60 changes: 6 additions & 54 deletions libraries/cms/html/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;

/**
* Utility class for JavaScript behaviors
Expand Down Expand Up @@ -58,29 +57,12 @@ public static function framework($extras = false, $debug = null)
* @return void
*
* @since 3.3
*
* @deprecated 5.0 Use Joomla\CMS\WebAsset\WebAssetManager::enableAsset();
*/
public static function core()
{
// Only load once
if (isset(static::$loaded[__METHOD__]))
{
return;
}

HTMLHelper::_('form.csrf');
Factory::getDocument()->getWebAssetManager()->enableAsset('core');

// Add core and base uri paths so javascript scripts can use them.
Factory::getDocument()->addScriptOptions(
'system.paths',
[
'root' => Uri::root(true),
'rootFull' => Uri::root(),
'base' => Uri::base(true),
]
);

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

/**
Expand Down Expand Up @@ -393,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
4 changes: 2 additions & 2 deletions libraries/cms/html/jquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class JHtmlJquery
*
* @since 3.0
*
* @deprecated 5.0 Use Joomla\CMS\WebAsset\WebAssetRegistry::enableAsset();
* @deprecated 5.0 Use Joomla\CMS\WebAsset\WebAssetManager::enableAsset();
*/
public static function framework($noConflict = true, $debug = null, $migrate = false)
{
Expand Down Expand Up @@ -74,7 +74,7 @@ public static function framework($noConflict = true, $debug = null, $migrate = f
*
* @since 3.0
*
* @deprecated 5.0 Use Joomla\CMS\WebAsset\WebAssetRegistry::enableAsset();
* @deprecated 5.0 Use Joomla\CMS\WebAsset\WebAssetManager::enableAsset();
*/
public static function ui(array $components = array('core'), $debug = null)
{
Expand Down
50 changes: 50 additions & 0 deletions libraries/src/WebAsset/AssetItem/CoreAssetItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\HTML\HTMLHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\WebAsset\WebAssetAttachBehaviorInterface;
use Joomla\CMS\WebAsset\WebAssetItem;

/**
* Web Asset Item class for Core asset
*
* @since __DEPLOY_VERSION__
*/
class CoreAssetItem 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
{
// Add core and base uri paths so javascript scripts can use them.
$doc->addScriptOptions(
'system.paths',
[
'root' => Uri::root(true),
'rootFull' => Uri::root(),
'base' => Uri::base(true),
]
);

HTMLHelper::_('form.csrf');
}
}
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)));
}
}
21 changes: 21 additions & 0 deletions libraries/src/WebAsset/Exception/InvalidActionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\WebAsset\Exception;

defined('JPATH_PLATFORM') or die;

/**
* Exception class defining an Invalid Action error
*
* @since __DEPLOY_VERSION__
*/
class InvalidActionException extends \RuntimeException implements WebAssetExceptionInterface
{

}
33 changes: 33 additions & 0 deletions libraries/src/WebAsset/WebAssetAttachBehaviorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Document\Document;

/**
* Web Asset Behavior interface
*
* @since __DEPLOY_VERSION__
*/
interface 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;
}
40 changes: 40 additions & 0 deletions libraries/src/WebAsset/WebAssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Joomla\CMS\Document\Document;
use Joomla\CMS\Event\AbstractEvent;
use Joomla\CMS\WebAsset\Exception\InvalidActionException;
use Joomla\CMS\WebAsset\Exception\UnsatisfiedDependencyException;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;
Expand Down Expand Up @@ -71,6 +72,15 @@ class WebAssetManager implements WebAssetManagerInterface, DispatcherAwareInterf
*/
protected $activeAssets = [];

/**
* Internal marker to check the manager state, to prevent use the manager after an attach happened
*
* @var bool
*
* @since __DEPLOY_VERSION__
*/
protected $assetsAttached = false;

/**
* Whether append asset version to asset path
*
Expand Down Expand Up @@ -111,10 +121,17 @@ public function getRegistry(): WebAssetRegistry
*
* @return self
*
* @throws InvalidActionException When the Manager already attached to a Document
*
* @since 4.0.0
*/
public function enableAsset(string $name): WebAssetManagerInterface
{
if ($this->assetsAttached)
{
throw new InvalidActionException('WebAssetManager already attached to a Document');
}

$asset = $this->registry->get($name);

// Asset already enabled
Expand All @@ -140,10 +157,17 @@ public function enableAsset(string $name): WebAssetManagerInterface
*
* @return self
*
* @throws InvalidActionException When the Manager already attached to a Document
*
* @since 4.0.0
*/
public function disableAsset(string $name): WebAssetManagerInterface
{
if ($this->assetsAttached)
{
throw new InvalidActionException('WebAssetManager already attached to a Document');
}

unset($this->activeAssets[$name]);

// Re-check dependencies
Expand Down Expand Up @@ -265,10 +289,17 @@ function ($state){
*
* @return self
*
* @throws InvalidActionException When the Manager already attached to a Document
*
* @since 4.0.0
*/
public function attachActiveAssetsToDocument(Document $doc): WebAssetManagerInterface
{
if ($this->assetsAttached)
{
throw new InvalidActionException('WebAssetManager already attached to a Document');
}

// Trigger the event
if ($this->getDispatcher())
{
Expand All @@ -286,6 +317,9 @@ public function attachActiveAssetsToDocument(Document $doc): WebAssetManagerInte
// Resolve an Order of Assets and their Dependencies
$assets = $this->enableDependencies()->getAssets(true);

// Prevent further use of manager if an attach already happened
$this->assetsAttached = true;

// Pre-save existing Scripts, and attach them after requested assets.
$jsBackup = $doc->_scripts;
$doc->_scripts = [];
Expand All @@ -308,6 +342,12 @@ public function attachActiveAssetsToDocument(Document $doc): WebAssetManagerInte
$version = $this->useVersioning ? ($asset->getVersion() ?: 'auto') : false;
$doc->addScript($path, ['version' => $version], $attr);
}

// Allow to Asset to add a Script options
if ($asset instanceof WebAssetAttachBehaviorInterface)
{
$asset->onAttachCallback($doc);
}
}

// Merge with previously added scripts
Expand Down
Loading