1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

[ticket/16944] Generate iconify bundle as needed via icons & assets bag

PHPBB3-16944
This commit is contained in:
Marc Alexander
2023-11-12 15:04:01 +01:00
parent bd6cd8f2fc
commit 0434bce5af
15 changed files with 139 additions and 225 deletions

View File

@@ -13,6 +13,8 @@
namespace phpbb\template;
use phpbb\assets\iconify_bundler;
class assets_bag
{
/** @var asset[] */
@@ -21,6 +23,17 @@ class assets_bag
/** @var asset[] */
protected $scripts = [];
/** @var string[] */
protected $iconify_icons = [];
/**
* Constructor for assets bag
*
* @param iconify_bundler $iconify_bundler
*/
public function __construct(protected iconify_bundler $iconify_bundler)
{}
/**
* Add a css asset to the bag
*
@@ -41,6 +54,30 @@ class assets_bag
$this->scripts[] = $asset;
}
public function add_iconify_icon(string $icon): void
{
$this->iconify_icons[] = $icon;
}
/**
* Inject iconify icons into template
*
* @param string $output Output before injection
* @param string $variable_name Variable name for injection
* @param bool $use_cdn Flag whether to use CDN or local data
*
* @return string Output after injection
*/
public function inject_iconify_icons(string $output, string $variable_name, bool $use_cdn): string
{
if (str_contains($output, $variable_name))
{
$output = str_replace($variable_name, $use_cdn ? '' : $this->get_iconify_content(), $output);
}
return $output;
}
/**
* Returns all css assets
*
@@ -92,4 +129,22 @@ class assets_bag
return $output;
}
/**
* Gets the HTML code to include all iconify icons
*
* @return string HTML code for iconify bundle
*/
public function get_iconify_content(): string
{
$output = '';
if (count($this->iconify_icons))
{
$output .= '<script>';
$this->iconify_bundler->add_icons($this->iconify_icons);
$output .= $this->iconify_bundler->run();
$output .= '</script>';
}
return $output;
}
}

View File

@@ -13,26 +13,32 @@
namespace phpbb\template\twig;
use phpbb\config\config;
use phpbb\event\dispatcher_interface;
use phpbb\extension\manager;
use phpbb\filesystem\filesystem;
use phpbb\path_helper;
use phpbb\template\assets_bag;
use Twig\Loader\LoaderInterface;
class environment extends \Twig\Environment
{
/** @var \phpbb\config\config */
/** @var config */
protected $phpbb_config;
/** @var \phpbb\filesystem\filesystem */
/** @var filesystem */
protected $filesystem;
/** @var \phpbb\path_helper */
/** @var path_helper */
protected $phpbb_path_helper;
/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
protected $container;
/** @var \phpbb\extension\manager */
/** @var manager */
protected $extension_manager;
/** @var \phpbb\event\dispatcher_interface */
/** @var dispatcher_interface */
protected $phpbb_dispatcher;
/** @var string */
@@ -50,16 +56,17 @@ class environment extends \Twig\Environment
/**
* Constructor
*
* @param \phpbb\config\config $phpbb_config The phpBB configuration
* @param \phpbb\filesystem\filesystem $filesystem
* @param \phpbb\path_helper $path_helper phpBB path helper
* @param assets_bag $assets_bag Assets bag
* @param config $phpbb_config The phpBB configuration
* @param filesystem $filesystem
* @param path_helper $path_helper phpBB path helper
* @param string $cache_path The path to the cache directory
* @param \phpbb\extension\manager|null $extension_manager phpBB extension manager
* @param \Twig\Loader\LoaderInterface|null $loader Twig loader interface
* @param \phpbb\event\dispatcher_interface|null $phpbb_dispatcher Event dispatcher object
* @param manager|null $extension_manager phpBB extension manager
* @param LoaderInterface|null $loader Twig loader interface
* @param dispatcher_interface|null $phpbb_dispatcher Event dispatcher object
* @param array $options Array of options to pass to Twig
*/
public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig\Loader\LoaderInterface $loader = null, \phpbb\event\dispatcher_interface $phpbb_dispatcher = null, $options = array())
public function __construct(assets_bag $assets_bag, config $phpbb_config, filesystem $filesystem, path_helper $path_helper, $cache_path, manager $extension_manager = null, LoaderInterface $loader = null, dispatcher_interface $phpbb_dispatcher = null, $options = array())
{
$this->phpbb_config = $phpbb_config;
@@ -70,7 +77,7 @@ class environment extends \Twig\Environment
$this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path();
$this->assets_bag = new assets_bag();
$this->assets_bag = $assets_bag;
$options = array_merge(array(
'cache' => (defined('IN_INSTALL')) ? false : $cache_path,
@@ -97,7 +104,7 @@ class environment extends \Twig\Environment
/**
* Get phpBB config
*
* @return \phpbb\config\config
* @return config
*/
public function get_phpbb_config()
{
@@ -117,7 +124,7 @@ class environment extends \Twig\Environment
/**
* Get the filesystem object
*
* @return \phpbb\filesystem\filesystem
* @return filesystem
*/
public function get_filesystem()
{
@@ -137,7 +144,7 @@ class environment extends \Twig\Environment
/**
* Get the phpbb path helper object
*
* @return \phpbb\path_helper
* @return path_helper
*/
public function get_path_helper()
{
@@ -204,6 +211,7 @@ class environment extends \Twig\Environment
{
$context['definition']->set('SCRIPTS', '__SCRIPTS_' . $placeholder_salt . '__');
$context['definition']->set('STYLESHEETS', '__STYLESHEETS_' . $placeholder_salt . '__');
$context['definition']->set('ICONIFY_ICONS', '__ICONIFY_ICONS_' . $placeholder_salt . '__');
}
/**
@@ -251,6 +259,7 @@ class environment extends \Twig\Environment
{
$output = str_replace('__STYLESHEETS_' . $placeholder_salt . '__', $this->assets_bag->get_stylesheets_content(), $output);
$output = str_replace('__SCRIPTS_' . $placeholder_salt . '__', $this->assets_bag->get_scripts_content(), $output);
$output = $this->assets_bag->inject_iconify_icons($output, '__ICONIFY_ICONS_' . $placeholder_salt . '__', $this->phpbb_config['allow_cdn']);
return $output;
}

View File

@@ -89,6 +89,8 @@ class icon extends AbstractExtension
case 'iconify':
$source = explode(':', $icon);
$source = $source[0];
$environment->get_assets_bag()->add_iconify_icon($icon);
break;
case 'png':