* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\HTML;
use Joomla\CMS\Environment\Browser;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\Utilities\ArrayHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Utility class for all HTML drawing classes
*
* @since 1.5
*/
abstract class HTMLHelper
{
/**
* Option values related to the generation of HTML output. Recognized
* options are:
* fmtDepth, integer. The current indent depth.
* fmtEol, string. The end of line string, default is linefeed.
* fmtIndent, string. The string to use for indentation, default is
* tab.
*
* @var array
* @since 1.5
*/
public static $formatOptions = array('format.depth' => 0, 'format.eol' => "\n", 'format.indent' => "\t");
/**
* An array to hold included paths
*
* @var string[]
* @since 1.5
* @deprecated 5.0
*/
protected static $includePaths = array();
/**
* An array to hold method references
*
* @var callable[]
* @since 1.6
* @deprecated 5.0
*/
protected static $registry = array();
/**
* The service registry for custom and overridden JHtml helpers
*
* @var Registry
* @since 4.0.0
*/
protected static $serviceRegistry;
/**
* Method to extract a key
*
* @param string $key The name of helper method to load, (prefix).(class).function
* prefix and class are optional and can be used to load custom html helpers.
*
* @return array Contains lowercase key, prefix, file, function.
*
* @since 1.6
* @deprecated 5.0 Use the service registry instead
*/
protected static function extract($key)
{
$key = preg_replace('#[^A-Z0-9_\.]#i', '', $key);
// Check to see whether we need to load a helper file
$parts = explode('.', $key);
if (\count($parts) === 3) {
@trigger_error(
'Support for a three segment service key is deprecated and will be removed in Joomla 5.0, use the service registry instead',
E_USER_DEPRECATED
);
}
$prefix = \count($parts) === 3 ? array_shift($parts) : 'JHtml';
$file = \count($parts) === 2 ? array_shift($parts) : '';
$func = array_shift($parts);
return array(strtolower($prefix . '.' . $file . '.' . $func), $prefix, $file, $func);
}
/**
* Class loader method
*
* Additional arguments may be supplied and are passed to the sub-class.
* Additional include paths are also able to be specified for third-party use
*
* @param string $key The name of helper method to load, (prefix).(class).function
* prefix and class are optional and can be used to load custom
* html helpers.
* @param array $methodArgs The arguments to pass forward to the method being called
*
* @return mixed Result of HTMLHelper::call($function, $args)
*
* @since 1.5
* @throws \InvalidArgumentException
*/
final public static function _(string $key, ...$methodArgs)
{
list($key, $prefix, $file, $func) = static::extract($key);
if (\array_key_exists($key, static::$registry)) {
$function = static::$registry[$key];
return static::call($function, $methodArgs);
}
/*
* Support fetching services from the registry if a custom class prefix was not given (a three segment key),
* the service comes from a class other than this one, and a service has been registered for the file.
*/
if ($prefix === 'JHtml' && $file !== '' && static::getServiceRegistry()->hasService($file)) {
$service = static::getServiceRegistry()->getService($file);
$toCall = array($service, $func);
if (!\is_callable($toCall)) {
throw new \InvalidArgumentException(sprintf('%s::%s not found.', $file, $func), 500);
}
static::register($key, $toCall);
return static::call($toCall, $methodArgs);
}
$className = $prefix . ucfirst($file);
if (!class_exists($className)) {
$path = Path::find(static::$includePaths, strtolower($file) . '.php');
if (!$path) {
throw new \InvalidArgumentException(sprintf('%s %s not found.', $prefix, $file), 500);
}
\JLoader::register($className, $path);
if (!class_exists($className)) {
throw new \InvalidArgumentException(sprintf('%s not found.', $className), 500);
}
}
// If calling a method from this class, do not allow access to internal methods
if ($className === __CLASS__) {
if (!((new \ReflectionMethod($className, $func))->isPublic())) {
throw new \InvalidArgumentException('Access to internal class methods is not allowed.');
}
}
$toCall = array($className, $func);
if (!\is_callable($toCall)) {
throw new \InvalidArgumentException(sprintf('%s::%s not found.', $className, $func), 500);
}
static::register($key, $toCall);
return static::call($toCall, $methodArgs);
}
/**
* Registers a function to be called with a specific key
*
* @param string $key The name of the key
* @param callable $function Function or method
*
* @return boolean True if the function is callable
*
* @since 1.6
* @deprecated 5.0 Use the service registry instead
*/
public static function register($key, callable $function)
{
@trigger_error(
'Support for registering functions is deprecated and will be removed in Joomla 5.0, use the service registry instead',
E_USER_DEPRECATED
);
list($key) = static::extract($key);
static::$registry[$key] = $function;
return true;
}
/**
* Removes a key for a method from registry.
*
* @param string $key The name of the key
*
* @return boolean True if a set key is unset
*
* @since 1.6
* @deprecated 5.0 Use the service registry instead
*/
public static function unregister($key)
{
@trigger_error(
'Support for registering functions is deprecated and will be removed in Joomla 5.0, use the service registry instead',
E_USER_DEPRECATED
);
list($key) = static::extract($key);
if (isset(static::$registry[$key])) {
unset(static::$registry[$key]);
return true;
}
return false;
}
/**
* Test if the key is registered.
*
* @param string $key The name of the key
*
* @return boolean True if the key is registered.
*
* @since 1.6
*/
public static function isRegistered($key)
{
list($key) = static::extract($key);
return isset(static::$registry[$key]);
}
/**
* Retrieves the service registry.
*
* @return Registry
*
* @since 4.0.0
*/
public static function getServiceRegistry(): Registry
{
if (!static::$serviceRegistry) {
static::$serviceRegistry = Factory::getContainer()->get(Registry::class);
}
return static::$serviceRegistry;
}
/**
* Function caller method
*
* @param callable $function Function or method to call
* @param array $args Arguments to be passed to function
*
* @return mixed Function result or false on error.
*
* @link https://www.php.net/manual/en/function.call-user-func-array.php
* @since 1.6
* @throws \InvalidArgumentException
*/
protected static function call(callable $function, $args)
{
// Workaround to allow calling helper methods have arguments passed by reference
$temp = [];
foreach ($args as &$arg) {
$temp[] = &$arg;
}
return \call_user_func_array($function, $temp);
}
/**
* Write a `` element
*
* @param string $url The relative URL to use for the href attribute
* @param string $text The target attribute to use
* @param array|string $attribs Attributes to be added to the `` element
*
* @return string
*
* @since 1.5
*/
public static function link($url, $text, $attribs = null)
{
if (\is_array($attribs)) {
$attribs = ArrayHelper::toString($attribs);
}
return '' . $text . '';
}
/**
* Write a `