[ticket/16944] Handle bundling of iconify icons in ACP

PHPBB3-16944
This commit is contained in:
Marc Alexander 2023-08-13 18:17:31 +02:00
parent a531d6071b
commit a1b41f25ef
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
7 changed files with 67 additions and 15 deletions

View file

@ -4,4 +4,5 @@ services:
arguments:
- '@dbal.conn'
- '@ext.manager'
- '@log'
- '%core.root_path%'

View file

@ -34,9 +34,7 @@ $iconify_bundler->find_icons([
$phpbb_root_path . 'styles/',
$phpbb_root_path . 'adm/style/',
]);
$output = $iconify_bundler->with_extensions()
->with_styles()
->run();
$output = $iconify_bundler->get_bundle();
// Save to file
file_put_contents($target, $output);

View file

@ -270,6 +270,10 @@ class acp_extensions
), array($this->phpbb_root_path . 'adm/style'));
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_ENABLE', time(), array($ext_name));
// Force rebuild of iconify bundle
$iconify_bundler = new \phpbb\assets\iconify_bundler($this->db, $this->ext_manager, $this->log, $this->phpbb_root_path);
$iconify_bundler->get_bundle(true);
}
catch (\phpbb\db\migration\exception $e)
{
@ -316,6 +320,10 @@ class acp_extensions
}
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_DISABLE', time(), array($ext_name));
// Force rebuild of iconify bundle
$iconify_bundler = new \phpbb\assets\iconify_bundler($this->db, $this->ext_manager, $this->log, $this->phpbb_root_path);
$iconify_bundler->get_bundle(true);
$this->tpl_name = 'acp_ext_disable';
$this->template->assign_vars([

View file

@ -65,6 +65,9 @@ class acp_styles
/** @var \phpbb\event\dispatcher_interface */
protected $dispatcher;
/** @var \phpbb\assets\iconify_bundler */
protected $iconify_bundler;
public function main($id, $mode)
{
global $db, $phpbb_admin_path, $phpbb_root_path, $phpEx, $template, $request, $cache, $auth, $config, $phpbb_dispatcher, $phpbb_container;
@ -80,6 +83,7 @@ class acp_styles
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $phpEx;
$this->dispatcher = $phpbb_dispatcher;
$this->iconify_bundler = $phpbb_container->get('assets.iconify_bundler');
$this->default_style = $config['default_style'];
$this->styles_path = $this->phpbb_root_path . $this->styles_path_absolute . '/';
@ -240,6 +244,9 @@ class acp_styles
$this->text_formatter_cache->invalidate();
}
// Force rebuild of iconify bundle
$this->iconify_bundler->get_bundle(true);
// Show message
if (!count($messages))
{
@ -331,7 +338,7 @@ class acp_styles
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
// Uinstall each style
// Uninstall each style
$uninstalled = array();
foreach ($rows as $style)
{
@ -367,6 +374,9 @@ class acp_styles
// Clear cache
$this->cache->purge();
// Force rebuild of iconify bundle
$this->iconify_bundler->get_bundle(true);
// Show message
trigger_error(implode('<br />', $messages) . adm_back_link($this->u_action), E_USER_NOTICE);
}

View file

@ -671,6 +671,8 @@ $lang = array_merge($lang, array(
'LOG_USERS_APPROVED' => '<strong>Users approved in usergroup</strong> %1$s<br />» %2$s',
'LOG_USERS_PENDING' => '<strong>Users requested to join group “%1$s” and need to be approved</strong><br />» %2$s',
'LOG_ICON_INVALID' => '<strong>Invalid icon supplied:</strong> Icon <i>%1$s:%2$s</i> does not seem to exist.',
'LOG_ICON_COLLECTION_INVALID' => '<strong>Invalid icon collection supplied:</strong> Icon collection with prefix <i>%1$s</i> does not seem to exist.',
'LOG_IMAGE_GENERATION_ERROR' => '<strong>Error while creating image</strong><br />» Error in %1$s on line %2$s: %3$s',
'LOG_INACTIVE_ACTIVATE' => '<strong>Activated inactive users</strong><br />» %s',

View file

@ -19,21 +19,35 @@ use Symfony\Component\Finder\Finder;
class iconify_bundler
{
protected const BUNDLE_PATH = 'assets/iconify/iconify_bundle.js';
protected $db;
protected $ext_manager;
/** @var \phpbb\log\log_interface */
protected $log;
/** @var \phpbb\filesystem\filesystem_interface */
protected $filesystem;
protected $root_path = '';
protected $bundle_path = '';
protected $icons_list = [];
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\extension\manager $ext_manager, string $root_path)
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\extension\manager $ext_manager, \phpbb\log\log_interface $log, string $root_path)
{
$this->db = $db;
$this->ext_manager = $ext_manager;
$this->filesystem = new \phpbb\filesystem\filesystem();
$this->log = $log;
$this->root_path = $root_path;
$this->bundle_path = $root_path . self::BUNDLE_PATH;
}
public function run()
protected function run()
{
// Sort icons first
sort($this->icons_list, SORT_NATURAL);
@ -62,6 +76,22 @@ class iconify_bundler
return $output;
}
public function get_bundle(bool $force_rebuild = false): string
{
if (!$force_rebuild && $this->is_dumped())
{
return file_get_contents($this->bundle_path);
}
$iconify_bundle = $this->with_extensions()
->with_styles()
->run();
$this->filesystem->dump_file($this->bundle_path, $iconify_bundle);
return $iconify_bundle;
}
/**
* @param array $paths Icon paths
*
@ -157,6 +187,11 @@ class iconify_bundler
return $this;
}
protected function is_dumped(): bool
{
return $this->filesystem->exists($this->bundle_path) && $this->filesystem->is_readable($this->bundle_path);
}
protected function add_icon(string $icon_name): void
{
if (!in_array($icon_name, $this->icons_list))
@ -276,18 +311,16 @@ class iconify_bundler
{
// Load icon set
$collection = new Collection($prefix);
if (!$collection->loadIconifyCollection($prefix)) {
throw new \Error(
'Icons with prefix "' . $prefix . '" do not exist in Iconify. Update iconify/json?'
);
if (!$collection->loadIconifyCollection($prefix))
{
$this->log->add('critical', ANONYMOUS, '', 'LOG_ICON_COLLECTION_INVALID', false, [$prefix]);
}
// Make sure all icons exist
foreach ($iconsList as $name) {
if (!$collection->iconExists($name)) {
// Uncomment next line to throw error if an icon does not exist
// throw new Error('Could not find icon: "' . $prefix . ':' . $name . '"');
echo 'Could not find icon: "', $prefix, ':', $name, "\"\n";
if (!$collection->iconExists($name))
{
$this->log->add('critical', ANONYMOUS, '', 'LOG_ICON_INVALID', false, [$prefix, $name]);
}
}

View file

@ -76,7 +76,7 @@
<script src="{T_ASSETS_PATH}/javascript/core.js?assets_version={T_ASSETS_VERSION}"></script>
<!-- INCLUDEJS forum_fn.js -->
<!-- INCLUDEJS ajax.js -->
<script src="{T_ASSETS_PATH}/iconify/iconify-bundle.js?assets_version={T_ASSETS_VERSION}"></script>
<script src="{{ T_ASSETS_PATH ~ '/iconify/iconify-bundle.js?assets_version=' ~ T_ASSETS_VERSION }}"></script>
<script src="{T_ASSETS_PATH}/iconify/iconify.min.js?assets_version={T_ASSETS_VERSION}"></script>
<!-- IF S_COOKIE_NOTICE -->