CoreFiles by mySites.guru
WebAssetRegistry.php739 lines, 15,432 bytes, from Joomla_4.0.0-alpha5-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha3
md5
fbda49972b7cb4902622e0b22a9a1683
sha1
ec4c5582df2859ae05c20f175b27158606e06818
sha256
1fccfcd30cc6309a74dba43d8cbe941e3e2becfdaeed38c3914c1a4d7e0b4aec
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
<?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;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Document\Document;
use Joomla\CMS\Event\AbstractEvent;
use Joomla\CMS\Filesystem\Path;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;

/**
 * Web Asset Factory class
 *
 * @since  4.0.0
 */
class WebAssetRegistry implements DispatcherAwareInterface
{
	use DispatcherAwareTrait;

	/**
	 * Mark the new registry file
	 *
	 * @var integer
	 *
	 * @since  4.0.0
	 */
	const REGISTRY_FILE_NEW = 1;

	/**
	 * Mark already parsed registry file
	 *
	 * @var integer
	 *
	 * @since  4.0.0
	 */
	const REGISTRY_FILE_PARSED = 2;

	/**
	 * Mark a broken/non-existing registry file
	 *
	 * @var integer
	 *
	 * @since  4.0.0
	 */
	const REGISTRY_FILE_INVALID = -1;

	/**
	 * Files with Asset info. File path should be relative.
	 *
	 * @example of data file:
	 *
	 * {
	 *		"title" : "Example",
	 *		"name"  : "com_example",
	 *		"author": "Joomla! CMS",
	 *		"assets": [
	 *			{
	 *				"name": "library1",
	 *				"version": "3.5.0",
	 *				"js": [
	 *					"com_example/library1.min.js"
	 *				]
	 *			},
	 *			{
	 *				"name": "library2",
	 *				"version": "3.5.0",
	 *				"js": [
	 *					"com_example/library2.min.js"
	 *				],
	 *				"css": [
	 *					"com_example/library2.css"
	 *				],
	 *				"dependency": [
	 *					"core",
	 *					"library1"
	 *				],
	 *				"attribute": {
	 *					"com_example/library2.min.js": {
	 *						"attrname": "attrvalue"
	 *					},
	 *					"com_example/library2.css": {
	 *						"media": "all"
	 *					}
	 *				}
	 *			},
	 *		]
	 *	}
	 *
	 * @var    array
	 *
	 * @since  4.0.0
	 */
	protected $dataFiles = [];

	/**
	 * Registry of available Assets
	 *
	 * @var array
	 *
	 * @since  4.0.0
	 */
	protected $assets = [];

	/**
	 * Weight off the heaviest and active asset
	 *
	 * @var float
	 *
	 * @since  4.0.0
	 */
	protected $lastItemWeight = 1;

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

	/**
	 * Get an existing Asset from a registry, by asset name.
	 * Return asset object or false if asset does not exist.
	 *
	 * @param   string  $name  Asset name
	 *
	 * @return  WebAssetItem|null
	 *
	 * @since   4.0.0
	 */
	public function getAsset(string $name)
	{
		// Check if any new file was added
		$this->parseRegistryFiles();

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

		return null;
	}

	/**
	 * Search for all active assets.
	 *
	 * @return  WebAssetItem[]  Array with active assets
	 *
	 * @since   4.0.0
	 */
	public function getActiveAssets(): array
	{
		$assets = array_filter(
			$this->assets,
			function($asset)
			{
				/** @var WebAssetItem $asset */
				return $asset->isActive();
			}
		);

		return $assets;
	}

	/**
	 * Search for assets with specific state.
	 *
	 * @param   int  $state  Asset state
	 *
	 * @return  WebAssetItem[]  Array with active assets
	 *
	 * @since   4.0.0
	 */
	public function getAssetsByState(int $state = WebAssetItem::ASSET_STATE_ACTIVE): array
	{
		$assets = array_filter(
			$this->assets,
			function($asset) use ($state)
			{
				/** @var WebAssetItem $asset */
				return $asset->getState() === $state;
			}
		);

		return $assets;
	}

	/**
	 * Add Asset to registry of known assets
	 *
	 * @param   WebAssetItem  $asset  Asset instance
	 *
	 * @return  self
	 *
	 * @since   4.0.0
	 */
	public function addAsset(WebAssetItem $asset): self
	{
		// Check whether the asset already exists, so we must copy its state before override
		if (!empty($this->assets[$asset->getName()]))
		{
			$existing = $this->assets[$asset->getName()];
			$asset->setState($existing->getState());
		}

		$this->assets[$asset->getName()] = $asset;

		return $this;
	}

	/**
	 * Remove Asset from registry.
	 *
	 * @param   string  $name  Asset name
	 *
	 * @return  self
	 *
	 * @since   4.0.0
	 */
	public function removeAsset(string $name): self
	{
		if (!empty($this->assets[$name]))
		{
			unset($this->assets[$name]);
		}

		return $this;
	}

	/**
	 * Change the asset State
	 *
	 * @param   string   $name   Asset name
	 * @param   integer  $state  New state
	 *
	 * @return  self
	 *
	 * @throws  \RuntimeException if asset with given name does not exist
	 *
	 * @since   4.0.0
	 */
	public function setAssetState(string $name, int $state = WebAssetItem::ASSET_STATE_ACTIVE): self
	{
		$asset = $this->getAsset($name);

		if (!$asset)
		{
			throw new \RuntimeException('Asset "' . $name . '" does not exist');
		}

		$currentState = $asset->getState();

		// Asset already has the requested state
		if ($currentState === $state)
		{
			return $this;
		}

		// Change state
		$asset->setState($state);

		// Update Dependency
		$this->updateDependency();

		// Trigger the event
		$event = AbstractEvent::create(
			'onWebAssetStateChangedExternally',
			[
				'eventClass' => 'Joomla\\CMS\\Event\\WebAsset\\WebAssetStateChangedEvent',
				'subject'  => $this,
				'asset'    => $asset,
				'oldState' => $currentState,
				'newState' => $state,
			]
		);
		$this->getDispatcher()->dispatch($event->getName(), $event);

		return $this;
	}

	/**
	 * Activate the Asset item
	 *
	 * @param   string  $name  The asset name
	 *
	 * @return self
	 *
	 * @since  4.0.0
	 */
	public function enableAsset(string $name): self
	{
		return $this->setAssetState($name, WebAssetItem::ASSET_STATE_ACTIVE);
	}

	/**
	 * Deactivate the Asset item
	 *
	 * @param   string  $name  The asset name
	 *
	 * @return self
	 *
	 * @since  4.0.0
	 */
	public function disableAsset(string $name): self
	{
		return $this->setAssetState($name, WebAssetItem::ASSET_STATE_INACTIVE);
	}

	/**
	 * Attach active assets to the document
	 *
	 * @param   Document  $doc  Document for attach StyleSheet/JavaScript
	 *
	 * @return  self
	 *
	 * @since  4.0.0
	 */
	public function attachActiveAssetsToDocument(Document $doc): self
	{
		// Resolve Dependency
		$this->updateDependency()->calculateWeightOfActiveAssets();

		// Trigger the event
		$event = AbstractEvent::create(
			'onWebAssetBeforeAttach',
			[
				'eventClass' => 'Joomla\\CMS\\Event\\WebAsset\\WebAssetBeforeAttachEvent',
				'subject'  => $this,
				'document' => $doc,
			]
		);
		$this->getDispatcher()->dispatch($event->getName(), $event);

		$assets = $this->sortAssetsByWeight($this->getActiveAssets());

		// 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)
		{
			$paths = $asset->getAssetFiles();

			// Add StyleSheets of the asset
			foreach ($paths['stylesheet'] 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 ($paths['script'] as $path => $attr)
			{
				unset($attr['__isExternal'], $attr['__pathOrigin']);
				$version = $this->useVersioning ? ($asset->getVersion() ?: 'auto') : false;
				$doc->addScript($path, ['version' => $version], $attr);
			}
		}

		// Merge with previously added scripts
		$doc->_scripts = array_replace($doc->_scripts, $jsBackup);

		return $this;
	}

	/**
	 * Update Dependencies state for all active Assets
	 *
	 * @return  self
	 *
	 * @since  4.0.0
	 */
	protected function updateDependency(): self
	{
		// First, deactivate all Dependency
		foreach ($this->getAssetsByState(WebAssetItem::ASSET_STATE_DEPENDANCY) as $depItem)
		{
			$depItem->setState(WebAssetItem::ASSET_STATE_INACTIVE);
		}

		// Second, get list of active assets and enable their dependencies
		$assets = $this->getAssetsByState(WebAssetItem::ASSET_STATE_ACTIVE);

		foreach ($assets as $asset)
		{
			$this->updateItemDependency($asset);
		}

		return $this;
	}

	/**
	 * Update Dependencies state for given Asset
	 *
	 * @param   WebAssetItem  $asset  Asset instance
	 *
	 * @return  self
	 *
	 * @throws  \RuntimeException When Dependency cannot be resolved
	 *
	 * @since  4.0.0
	 */
	protected function updateItemDependency(WebAssetItem $asset): self
	{
		foreach ($this->getDependenciesForAsset($asset, true) as $depItem)
		{
			// Set dependency state only when it is inactive, to keep a manually activated Asset in their original state
			if (!$depItem->isActive())
			{
				$depItem->setState(WebAssetItem::ASSET_STATE_DEPENDANCY);
			}
		}

		return $this;
	}

	/**
	 * Calculate weight of active Assets, by its Dependencies
	 *
	 * @return  self
	 *
	 * @since  4.0.0
	 */
	protected function calculateWeightOfActiveAssets(): self
	{
		// See https://en.wikipedia.org/wiki/Topological_sorting#Kahn.27s_algorithm
		$result        = [];
		$graphOutgoing = [];
		$graphIncoming = [];
		$activeAssets  = $this->getActiveAssets();

		// Build Graphs of Outgoing and Incoming connections
		foreach ($activeAssets 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;
			}
		}

		// 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);
			$result[] = $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;
				}
			}
		}

		// Update a weight for each active asset
		foreach (array_reverse($result) as $index => $name)
		{
			$activeAssets[$name]->setWeight($index + 1);
		}

		return $this;
	}

	/**
	 * Return dependancy for Asset as array of AssetItem 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  \RuntimeException 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;
			}

			$dep = $this->getAsset($depName);

			if (!$dep)
			{
				throw new \RuntimeException('Cannot find Dependency "' . $depName . '" for Asset "' . $asset->getName() . '"');
			}

			$assets[$depName] = $dep;

			if (!$recursively)
			{
				continue;
			}

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

		return $assets;
	}

	/**
	 * Sort assets by its weight
	 *
	 * @param   WebAssetItem[]  $assets  Array of assets to sort
	 *
	 * @return  WebAssetItem[]
	 *
	 * @since   4.0.0
	 */
	public function sortAssetsByWeight(array $assets): array
	{
		uasort(
			$assets,
			function($a, $b)
			{
				/** @var WebAssetItem $a */
				/** @var WebAssetItem $b */
				if ($a->getWeight() === $b->getWeight())
				{
					return 0;
				}

				return $a->getWeight() > $b->getWeight() ? 1 : -1;
			}
		);

		return $assets;
	}

	/**
	 * Prepare new Asset instance.
	 *
	 * @param   string  $name  Asset name
	 * @param   array   $data  Asset information
	 *
	 * @return  WebAssetItem
	 *
	 * @since   4.0.0
	 */
	public function createAsset(string $name, array $data = []): WebAssetItem
	{
		return new WebAssetItem($name, $data);
	}

	/**
	 * Register new file with Asset(s) info
	 *
	 * @param   string  $path  Relative path
	 *
	 * @return  self
	 *
	 * @since  4.0.0
	 */
	public function addRegistryFile(string $path): self
	{
		$path = Path::clean($path);

		if (isset($this->dataFiles[$path]))
		{
			return $this;
		}

		$this->dataFiles[$path] = is_file(JPATH_ROOT . '/' . $path) ? static::REGISTRY_FILE_NEW : static::REGISTRY_FILE_INVALID;

		return $this;
	}

	/**
	 * Parse registered files
	 *
	 * @return  void
	 *
	 * @since  4.0.0
	 */
	protected function parseRegistryFiles()
	{
		// Filter new asset data files and parse each
		$constantIsNew = static::REGISTRY_FILE_NEW;
		$files = array_filter(
			$this->dataFiles,
			function($state) use ($constantIsNew)
			{
				return $state === $constantIsNew;
			}
		);

		if (!$files)
		{
			return;
		}

		foreach (array_keys($files) as $path)
		{
			$this->parseRegistryFile($path);

			// Mark as parsed (not new)
			$this->dataFiles[$path] = static::REGISTRY_FILE_PARSED;
		}
	}

	/**
	 * Parse registry file
	 *
	 * @param   string  $path  Relative path to the data file
	 *
	 * @return  void
	 *
	 * @throws  \RuntimeException If file is empty or invalid
	 *
	 * @since   4.0.0
	 */
	protected function parseRegistryFile($path)
	{
		$data = file_get_contents(JPATH_ROOT . '/' . $path);
		$data = $data ? json_decode($data, true) : null;

		if (!$data)
		{
			throw new \RuntimeException('Asset data file "' . $path . '" is broken');
		}

		// Asset exists but empty, skip it silently
		if (empty($data['assets']))
		{
			return;
		}

		// Keep source info
		$assetSource = [
			'registryFile' => $path,
		];

		// Prepare WebAssetItem instances
		foreach ($data['assets'] as $item)
		{
			if (empty($item['name']))
			{
				throw new \RuntimeException('Asset data file "' . $path . '" contains incorrect asset defination');
			}

			$item['assetSource'] = $assetSource;
			$assetItem = $this->createAsset($item['name'], $item);
			$this->addAsset($assetItem);
		}
	}

	/**
	 * Dump available assets to simple array, with some basic info
	 *
	 * @param   bool  $onlyActive  Return only active Assets
	 *
	 * @return  array
	 *
	 * @since   4.0.0
	 */
	public function debugAssets(bool $onlyActive = false): array
	{
		// Update dependencies
		$this->updateDependency()->calculateWeightOfActiveAssets();

		$assets = $onlyActive ? $this->getActiveAssets() : $this->assets;
		$assets = $this->sortAssetsByWeight($assets);
		$result = [];

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

		return $result;
	}

	/**
	 * 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;
	}
}

The same content is in 2 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha6. See them all, or every version of this file.