* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\HTML;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Environment\Browser;
use Joomla\CMS\Factory;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Uri\Uri;
use Joomla\Utilities\ArrayHelper;
\JLoader::import('joomla.environment.browser');
\JLoader::import('joomla.filesystem.file');
\JLoader::import('joomla.filesystem.path');
/**
* 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
*/
protected static $includePaths = array();
/**
* An array to hold method references
*
* @var callable[]
* @since 1.6
*/
protected static $registry = array();
/**
* 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
*/
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);
$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.
*
* @return mixed Result of HTMLHelper::call($function, $args)
*
* @since 1.5
* @throws \InvalidArgumentException
*/
public static function _($key)
{
list($key, $prefix, $file, $func) = static::extract($key);
if (array_key_exists($key, static::$registry))
{
$function = static::$registry[$key];
$args = func_get_args();
// Remove function name from arguments
array_shift($args);
return static::call($function, $args);
}
$className = $prefix . ucfirst($file);
if (!class_exists($className))
{
$path = \JPath::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);
}
}
$toCall = array($className, $func);
if (!is_callable($toCall))
{
throw new \InvalidArgumentException(sprintf('%s::%s not found.', $className, $func), 500);
}
static::register($key, $toCall);
$args = func_get_args();
// Remove function name from arguments
array_shift($args);
return static::call($toCall, $args);
}
/**
* Registers a function to be called with a specific key
*
* @param string $key The name of the key
* @param string $function Function or method
*
* @return boolean True if the function is callable
*
* @since 1.6
*/
public static function register($key, $function)
{
list($key) = static::extract($key);
if (is_callable($function))
{
static::$registry[$key] = $function;
return true;
}
return false;
}
/**
* 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
*/
public static function unregister($key)
{
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]);
}
/**
* 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($function, $args)
{
if (!is_callable($function))
{
throw new \InvalidArgumentException('Function not supported', 500);
}
// PHP 5.3 workaround
$temp = array();
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 `