CoreFiles by mySites.guru
WebAssetManager.php642 lines, 15,001 bytes, from Joomla_4.0.0-alpha10-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha9
md5
06b5a9e7364aba9c18623e8a0d2de176
sha1
efccc26cf161abf59e7cd9792beea79ae9a70f58
sha256
35bc3a7b3b2b1dad12570811c881bad185e6594b6ba2b5e5b51af61841ab0ca5
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
<?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;
use Joomla\CMS\Event\AbstractEvent;
use Joomla\CMS\WebAsset\Exception\InvalidActionException;
use Joomla\CMS\WebAsset\Exception\UnknownAssetException;
use Joomla\CMS\WebAsset\Exception\UnsatisfiedDependencyException;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;

/**
 * Web Asset Manager class
 *
 * @since  4.0.0
 */
class WebAssetManager implements WebAssetManagerInterface, DispatcherAwareInterface
{
	use DispatcherAwareTrait;

	/**
	 * Mark inactive asset
	 *
	 * @var    integer
	 *
	 * @since  4.0.0
	 */
	const ASSET_STATE_INACTIVE = 0;

	/**
	 * Mark active asset. Just enabled, but WITHOUT dependency resolved
	 *
	 * @var    integer
	 *
	 * @since  4.0.0
	 */
	const ASSET_STATE_ACTIVE = 1;

	/**
	 * Mark active asset that is enabled as dependency to another asset
	 *
	 * @var    integer
	 *
	 * @since  4.0.0
	 */
	const ASSET_STATE_DEPENDENCY = 2;

	/**
	 * The WebAsset Registry instance
	 *
	 * @var    WebAssetRegistry
	 *
	 * @since  4.0.0
	 */
	protected $registry;

	/**
	 * A list of active assets (including their dependencies).
	 * Array of Name => State
	 *
	 * @var    array
	 *
	 * @since  4.0.0
	 */
	protected $activeAssets = [];

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

	/**
	 * Whether append asset version to asset path
	 *
	 * @var    bool
	 *
	 * @since  4.0.0
	 */
	protected $useVersioning = true;

	/**
	 * Internal marker to keep track when need to recheck dependencies
	 *
	 * @var    bool
	 *
	 * @since  4.0.0
	 */
	protected $dependenciesIsActual = false;

	/**
	 * Class constructor
	 *
	 * @param   WebAssetRegistry  $registry  The WebAsset Registry instance
	 *
	 * @since   4.0.0
	 */
	public function __construct(WebAssetRegistry $registry)
	{
		$this->registry = $registry;
	}

	/**
	 * Get associated registry instance
	 *
	 * @return   WebAssetRegistry
	 *
	 * @since  4.0.0
	 */
	public function getRegistry(): WebAssetRegistry
	{
		return $this->registry;
	}

	/**
	 * Activate the Asset item
	 *
	 * @param   string  $name  The asset name
	 *
	 * @return self
	 *
	 * @throws  UnknownAssetException  When Asset cannot be found
	 * @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');
		}

		// Check whether asset exists
		$this->registry->get($name);

		// Asset already enabled
		if (!empty($this->activeAssets[$name]))
		{
			// Set state to active, in case it was ASSET_STATE_DEPENDENCY
			$this->activeAssets[$name] = static::ASSET_STATE_ACTIVE;

			return $this;
		}

		$this->activeAssets[$name] = static::ASSET_STATE_ACTIVE;

		// To re-check dependencies
		$this->dependenciesIsActual = false;

		return $this;
	}

	/**
	 * Deactivate the Asset item
	 *
	 * @param   string  $name  The asset name
	 *
	 * @return  self
	 *
	 * @throws  UnknownAssetException  When Asset cannot be found
	 * @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]);

		// To re-check dependencies
		$this->dependenciesIsActual = false;

		return $this;
	}

	/**
	 * Get a state for the Asset
	 *
	 * @param   string  $name  The asset name
	 *
	 * @return  int
	 *
	 * @throws  UnknownAssetException  When Asset cannot be found
	 *
	 * @since  4.0.0
	 */
	public function getAssetState(string $name): int
	{
		// Check whether asset exists first
		$this->registry->get($name);

		// Make sure that all dependencies are active
		if (!$this->dependenciesIsActual)
		{
			$this->enableDependencies();
		}

		if (!empty($this->activeAssets[$name]))
		{
			return $this->activeAssets[$name];
		}

		return static::ASSET_STATE_INACTIVE;
	}

	/**
	 * Check whether the asset are enabled
	 *
	 * @param   string  $name  The asset name
	 *
	 * @return  bool
	 *
	 * @throws  UnknownAssetException  When Asset cannot be found
	 *
	 * @since  4.0.0
	 */
	public function isAssetActive(string $name): bool
	{
		return $this->getAssetState($name) !== static::ASSET_STATE_INACTIVE;
	}

	/**
	 * Get all assets that was enabled
	 *
	 * @param   bool  $sort  Whether need to sort the assets to follow the dependency Graph
	 *
	 * @return  WebAssetItem[]
	 *
	 * @throws  UnknownAssetException  When Asset cannot be found
	 * @throws  UnsatisfiedDependencyException When Dependency cannot be found
	 *
	 * @since  4.0.0
	 */
	public function getAssets(bool $sort = false): array
	{
		// Make sure that all dependencies are active
		if (!$this->dependenciesIsActual)
		{
			$this->enableDependencies();
		}

		if ($sort)
		{
			return $this->calculateOrderOfActiveAssets();
		}

		$assets = [];

		foreach (array_keys($this->activeAssets) as $name)
		{
			$assets[$name] = $this->registry->get($name);
		}

		return $assets;
	}

	/**
	 * Update Dependencies state for all active Assets or only for given
	 *
	 * @param   WebAssetItem  $asset  The asset instance to which need to enable dependencies
	 *
	 * @return  self
	 *
	 * @since  4.0.0
	 */
	protected function enableDependencies(WebAssetItem $asset = null): self
	{
		if ($asset)
		{
			$allDependencies = $this->getDependenciesForAsset($asset, true);

			foreach ($allDependencies as $depItem)
			{
				// Set dependency state only when it is inactive, to keep a manually activated Asset in their original state
				if (empty($this->activeAssets[$depItem->getName()]))
				{
					$this->activeAssets[$depItem->getName()] = static::ASSET_STATE_DEPENDENCY;
				}
			}
		}
		else
		{
			// Re-Check for Dependencies for all active assets
			$this->activeAssets = array_filter(
				$this->activeAssets,
				function ($state){
					return $state === WebAssetManager::ASSET_STATE_ACTIVE;
				}
			);

			foreach (array_keys($this->activeAssets) as $name)
			{
				$asset = $this->registry->get($name);
				$this->enableDependencies($asset);
			}

			$this->dependenciesIsActual = true;
		}

		return $this;
	}

	/**
	 * Attach active assets to the document
	 *
	 * @param   Document  $doc  Document for attach StyleSheet/JavaScript
	 *
	 * @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())
		{
			$event = AbstractEvent::create(
				'onWebAssetBeforeAttach',
				[
					'eventClass' => 'Joomla\\CMS\\Event\\WebAsset\\WebAssetBeforeAttachEvent',
					'subject'  => $this,
					'document' => $doc,
				]
			);
			$this->getDispatcher()->dispatch($event->getName(), $event);
		}

		// Resolve an Order of Assets and their Dependencies
		$assets = $this->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 = [];

		// Attach active assets to the document
		foreach ($assets as $asset)
		{
			// Add StyleSheets of the asset
			foreach ($asset->getStylesheetFiles(true) as $path => $attr)
			{
				unset($attr['__isExternal'], $attr['__pathOrigin']);
				$version = $this->useVersioning ? ($asset->getVersion() ?: 'auto') : false;
				$doc->addStyleSheet($path, ['version' => $version], $attr);
			}

			// Add Scripts of the asset
			foreach ($asset->getScriptFiles(true) as $path => $attr)
			{
				unset($attr['__isExternal'], $attr['__pathOrigin']);
				$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
		$doc->_scripts = array_replace($doc->_scripts, $jsBackup);

		return $this;
	}

	/**
	 * Calculate weight of active Assets, by its Dependencies
	 *
	 * @return  WebAssetItem[]
	 *
	 * @since  4.0.0
	 */
	protected function calculateOrderOfActiveAssets(): array
	{
		// See https://en.wikipedia.org/wiki/Topological_sorting#Kahn.27s_algorithm
		$graphOrder    = [];
		$activeAssets  = $this->getAssets();

		// Get Graph of Outgoing and Incoming connections
		$connectionsGraph = $this->getConnectionsGraph($activeAssets);
		$graphOutgoing    = $connectionsGraph['outgoing'];
		$graphIncoming    = $connectionsGraph['incoming'];

		// Make a copy to be used during weight processing
		$graphIncomingCopy = $graphIncoming;

		// Find items without incoming connections
		$emptyIncoming = array_keys(
			array_filter(
				$graphIncoming,
				function ($el){
					return !$el;
				}
			)
		);

		// Loop through, and sort the graph
		while ($emptyIncoming)
		{
			// Add the node without incoming connection to the result
			$item = array_shift($emptyIncoming);
			$graphOrder[] = $item;

			// Check of each neighbor of the node
			foreach (array_reverse($graphOutgoing[$item]) as $neighbor)
			{
				// Remove incoming connection of already visited node
				unset($graphIncoming[$neighbor][$item]);

				// If there no more incoming connections add the node to queue
				if (empty($graphIncoming[$neighbor]))
				{
					$emptyIncoming[] = $neighbor;
				}
			}
		}

		// Sync Graph order with FIFO order
		$fifoWeights      = [];
		$graphWeights     = [];
		$requestedWeights = [];

		foreach (array_keys($this->activeAssets) as $index => $name)
		{
			$fifoWeights[$name] = $index * 10 + 10;
		}

		foreach (array_reverse($graphOrder) as $index => $name)
		{
			$graphWeights[$name]     = $index * 10 + 10;
			$requestedWeights[$name] = $activeAssets[$name]->getWeight() ?: $fifoWeights[$name];
		}

		// Try to set a requested weight, or make it close as possible to requested, but keep the Graph order
		while ($requestedWeights)
		{
			$item   = key($requestedWeights);
			$weight = array_shift($requestedWeights);

			// Skip empty items
			if ($weight === null)
			{
				continue;
			}

			// Check the predecessors (Outgoing vertexes), the weight cannot be lighter than the predecessor have
			$topBorder = $weight - 1;
			if (!empty($graphOutgoing[$item]))
			{
				$prevWeights = [];
				foreach ($graphOutgoing[$item] as $pItem)
				{
					$prevWeights[] = $graphWeights[$pItem];
				}
				$topBorder = max($prevWeights);
			}

			// Calculate a new weight
			$newWeight = $weight > $topBorder ? $weight : $topBorder + 1;

			// If a new weight heavier than existing, then we need to update all incoming connections (children)
			if ($newWeight > $graphWeights[$item] && !empty($graphIncomingCopy[$item]))
			{
				// Sort Graph of incoming by actual position
				foreach ($graphIncomingCopy[$item] as $incomingItem)
				{
					// Set a weight heavier than current, then this node to be processed in next iteration
					if (empty($requestedWeights[$incomingItem]))
					{
						$requestedWeights[$incomingItem] = $graphWeights[$incomingItem] + $newWeight;
					}
				}
			}

			// Set a new weight
			$graphWeights[$item] = $newWeight;
		}

		asort($graphWeights);

		// Get Assets in calculated order
		$resultAssets  = [];
		foreach (array_keys($graphWeights) as $name)
		{
			$resultAssets[$name] = $activeAssets[$name];
		}

		return $resultAssets;
	}

	/**
	 * Build Graph of Outgoing and Incoming connections for given assets.
	 *
	 * @param   WebAssetItem[]  $assets  Asset instances
	 *
	 * @return  array
	 *
	 * @since   4.0.0
	 */
	protected function getConnectionsGraph (array $assets): array
	{
		$graphOutgoing = [];
		$graphIncoming = [];

		foreach ($assets as $asset)
		{
			$name = $asset->getName();
			$graphOutgoing[$name] = array_combine($asset->getDependencies(), $asset->getDependencies());

			if (!array_key_exists($name, $graphIncoming))
			{
				$graphIncoming[$name] = [];
			}

			foreach ($asset->getDependencies() as $depName)
			{
				$graphIncoming[$depName][$name] = $name;
			}
		}

		return [
			'outgoing' => $graphOutgoing,
			'incoming' => $graphIncoming,
		];
	}

	/**
	 * Return dependencies for Asset as array of WebAssetItem objects
	 *
	 * @param   WebAssetItem  $asset          Asset instance
	 * @param   boolean       $recursively    Whether to search for dependancy recursively
	 * @param   WebAssetItem  $recursionRoot  Initial item to prevent loop
	 *
	 * @return  WebAssetItem[]
	 *
	 * @throws  UnsatisfiedDependencyException When Dependency cannot be found
	 *
	 * @since   4.0.0
	 */
	protected function getDependenciesForAsset(WebAssetItem $asset, $recursively = false, WebAssetItem $recursionRoot = null): array
	{
		$assets        = [];
		$recursionRoot = $recursionRoot ?? $asset;

		foreach ($asset->getDependencies() as $depName)
		{
			// Skip already loaded in recursion
			if ($recursionRoot->getName() === $depName)
			{
				continue;
			}

			if (!$this->registry->exists($depName))
			{
				throw new UnsatisfiedDependencyException('Unsatisfied dependency "' . $depName . '" for Asset "' . $asset->getName() . '"');
			}

			$dep = $this->registry->get($depName);

			$assets[$depName] = $dep;

			if (!$recursively)
			{
				continue;
			}

			$parentDeps = $this->getDependenciesForAsset($dep, true, $recursionRoot);
			$assets     = array_replace($assets, $parentDeps);
		}

		return $assets;
	}

	/**
	 * Whether append asset version to asset path
	 *
	 * @param   bool  $useVersioning  Boolean flag
	 *
	 * @return  self
	 *
	 * @since   4.0.0
	 */
	public function useVersioning(bool $useVersioning): self
	{
		$this->useVersioning = $useVersioning;

		return $this;
	}

	/**
	 * Dump available assets to simple array, with some basic info
	 *
	 * @return  array
	 *
	 * @since   4.0.0
	 */
	public function debugAssets(): array
	{
		// Get all active assets in final order
		$assets = $this->getAssets(true);
		$result = [];

		foreach ($assets as $asset)
		{
			$result[$asset->getName()] = [
				'name'   => $asset->getName(),
				'deps'   => implode(', ', $asset->getDependencies()),
				'state'  => $this->getAssetState($asset->getName()),
			];
		}

		return $result;
	}
}

No other release has this exact content. See every version of this file.