Skip to content

Instantly share code, notes, and snippets.

@mbabker
Created July 6, 2019 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbabker/a8e1140ae8efc0e505ac13f10267a0fe to your computer and use it in GitHub Desktop.
Save mbabker/a8e1140ae8efc0e505ac13f10267a0fe to your computer and use it in GitHub Desktop.
Asset Registry
<?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\Document\Asset;
use Joomla\CMS\Document\Asset\Exception\UnknownAssetException;
defined('_JEXEC') or die;
/**
* A registry of known UI assets within the application
*
* @since __DEPLOY_VERSION__
*/
class AssetRegistry implements AssetRegistryInterface
{
/**
* The script type registry
*
* @var ResourceTypeAssetRegistryInterface
* @since __DEPLOY_VERSION__
*/
private $scripts;
/**
* The stylesheet type registry
*
* @var ResourceTypeAssetRegistryInterface
* @since __DEPLOY_VERSION__
*/
private $stylesheets;
/**
* Creates an AssetRegistry.
*
* @since __DEPLOY_VERSION__
*/
public function __construct()
{
$this->scripts = new ResourceTypeAssetRegistry;
$this->stylesheets = new ResourceTypeAssetRegistry;
}
/**
* Add a script asset to the registry, overwriting a previously existing asset with the same name
*
* @param Asset $asset The script asset to add
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function addScript(Asset $asset): void
{
$this->scripts->add($asset);
}
/**
* Add a stylesheet asset to the registry, overwriting a previously existing asset with the same name
*
* @param Asset $asset The stylesheet asset to add
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function addStylesheet(Asset $asset): void
{
$this->stylesheets->add($asset);
}
/**
* Retrieve the named script asset from the registry
*
* @param string $asset The named script asset to remove
*
* @return Asset
*
* @since __DEPLOY_VERSION__
* @throws UnknownAssetException if the named script asset does not exist in the registry
*/
public function getScript(string $asset): Asset
{
if (!$this->scripts->has($asset))
{
throw new UnknownAssetException(sprintf('There is not a "%s" asset in the script registry.', $asset));
}
return $this->scripts->get($asset);
}
/**
* Retrieve the named stylesheet asset from the registry
*
* @param string $asset The named stylesheet asset to remove
*
* @return Asset
*
* @since __DEPLOY_VERSION__
* @throws UnknownAssetException if the named stylesheet asset does not exist in the registry
*/
public function getStylesheet(string $asset): Asset
{
if (!$this->stylesheets->has($asset))
{
throw new UnknownAssetException(sprintf('There is not a "%s" asset in the stylesheet registry.', $asset));
}
return $this->stylesheets->get($asset);
}
/**
* Check if the named script asset exists in the registry
*
* @param string $asset The named script asset to check for presence
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function hasScript(string $asset): bool
{
$this->scripts->has($asset);
}
/**
* Check if the named stylesheet asset exists in the registry
*
* @param string $asset The named stylesheet asset to check for presence
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function hasStylesheet(string $asset): bool
{
$this->stylesheets->has($asset);
}
/**
* Remove the named script asset from the registry
*
* @param string $asset The named script asset to remove
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function removeScript(string $asset): void
{
$this->scripts->remove($asset);
}
/**
* Remove the named stylesheet asset from the registry
*
* @param string $asset The named stylesheet asset to remove
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function removeStylesheet(string $asset): void
{
$this->stylesheets->remove($asset);
}
}
<?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\Document\Asset;
use Joomla\CMS\Document\Asset\Exception\UnknownAssetException;
defined('_JEXEC') or die;
/**
* Definition of a registry of known UI assets within the application
*
* @since __DEPLOY_VERSION__
*/
interface AssetRegistryInterface
{
/**
* Add a script asset to the registry, overwriting a previously existing asset with the same name
*
* @param Asset $asset The script asset to add
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function addScript(Asset $asset): void;
/**
* Add a stylesheet asset to the registry, overwriting a previously existing asset with the same name
*
* @param Asset $asset The stylesheet asset to add
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function addStylesheet(Asset $asset): void;
/**
* Retrieve the named script asset from the registry
*
* @param string $asset The named script asset to remove
*
* @return Asset
*
* @since __DEPLOY_VERSION__
* @throws UnknownAssetException if the named script asset does not exist in the registry
*/
public function getScript(string $asset): Asset;
/**
* Retrieve the named stylesheet asset from the registry
*
* @param string $asset The named stylesheet asset to remove
*
* @return Asset
*
* @since __DEPLOY_VERSION__
* @throws UnknownAssetException if the named stylesheet asset does not exist in the registry
*/
public function getStylesheet(string $asset): Asset;
/**
* Check if the named script asset exists in the registry
*
* @param string $asset The named script asset to check for presence
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function hasScript(string $asset): bool;
/**
* Check if the named stylesheet asset exists in the registry
*
* @param string $asset The named stylesheet asset to check for presence
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function hasStylesheet(string $asset): bool;
/**
* Remove the named script asset from the registry
*
* @param string $asset The named script asset to remove
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function removeScript(string $asset): void;
/**
* Remove the named stylesheet asset from the registry
*
* @param string $asset The named stylesheet asset to remove
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function removeStylesheet(string $asset): void;
}
<?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\Document\Asset;
use Joomla\CMS\Document\Asset\Exception\UnknownAssetException;
defined('_JEXEC') or die;
/**
* Registry of known UI assets of a given type within the application
*
* @since __DEPLOY_VERSION__
*/
class ResourceTypeAssetRegistry implements ResourceTypeAssetRegistryInterface
{
/**
* The collection of assets in this registry
*
* @var Asset[]
* @since __DEPLOY_VERSION__
*/
private $assets = [];
/**
* Add an asset to the registry, overwriting a previously existing asset with the same name
*
* @param Asset $asset The asset to add
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function add(Asset $asset): void
{
$this->assets[$asset->getName()] = $asset;
}
/**
* Retrieve the named asset from the registry
*
* @param string $asset The named asset to remove
*
* @return Asset
*
* @since __DEPLOY_VERSION__
* @throws UnknownAssetException if the named asset does not exist in the registry
*/
public function get(string $asset): Asset
{
if (!$this->has($asset))
{
throw new UnknownAssetException(sprintf('There is not a "%s" asset in the registry.', $asset));
}
return $this->assets[$asset];
}
/**
* Check if the named asset exists in the registry
*
* @param string $asset The named asset to check for presence
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function has(string $asset): bool
{
return isset($this->assets[$asset]);
}
/**
* Remove the named asset from the registry
*
* @param string $asset The named asset to remove
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function remove(string $asset): void
{
unset($this->assets[$asset]);
}
}
<?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\Document\Asset;
use Joomla\CMS\Document\Asset\Exception\UnknownAssetException;
defined('_JEXEC') or die;
/**
* Definition of a registry of known UI assets of a given type within the application
*
* @since __DEPLOY_VERSION__
*/
interface ResourceTypeAssetRegistryInterface
{
/**
* Add an asset to the registry, overwriting a previously existing asset with the same name
*
* @param Asset $asset The asset to add
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function add(Asset $asset): void;
/**
* Retrieve the named asset from the registry
*
* @param string $asset The named asset to remove
*
* @return Asset
*
* @since __DEPLOY_VERSION__
* @throws UnknownAssetException if the named asset does not exist in the registry
*/
public function get(string $asset): Asset;
/**
* Check if the named asset exists in the registry
*
* @param string $asset The named asset to check for presence
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function has(string $asset): bool;
/**
* Remove the named asset from the registry
*
* @param string $asset The named asset to remove
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function remove(string $asset): void;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment