1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-01-17 14:18:24 +01:00

[ticket/17283] Remove remaining parts using iconify

PHPBB3-17283
This commit is contained in:
Marc Alexander 2024-01-04 20:35:56 +01:00
parent 9af61c1f1e
commit db56017d57
No known key found for this signature in database
GPG Key ID: 50E0D2423696F995
19 changed files with 11 additions and 531 deletions

View File

@ -67,26 +67,8 @@ function watch() {
gulp.watch(paths.styles.src, styles);
}
function copyIconifyIcons() {
return gulp.src([
'node_modules/@iconify/json/json/fa.json',
'node_modules/@iconify/json/json/ic.json',
'node_modules/@iconify/json/json/mdi.json',
])
.pipe(gulp.dest('phpBB/assets/iconify'));
}
function copyIconifyJs() {
return gulp.src([
'node_modules/@iconify/iconify/dist/iconify.without-api.min.js',
])
.pipe(rename('iconify.min.js'))
.pipe(gulp.dest('phpBB/assets/iconify'));
}
exports.style = styles;
exports.minify = minify;
exports.watch = watch;
exports.copyIconify = gulp.parallel(copyIconifyJs, copyIconifyIcons);
exports.default = gulp.series(styles, minify, watch);

View File

@ -2,12 +2,3 @@ services:
assets.bag:
class: phpbb\template\assets_bag
shared: false
arguments:
- '@assets.iconify_bundler'
assets.iconify_bundler:
class: phpbb\assets\iconify_bundler
shared: false
arguments:
- '@log'
- '%core.root_path%'

View File

@ -12,15 +12,6 @@ services:
assets.bag:
class: phpbb\template\assets_bag
shared: false
arguments:
- '@assets.iconify_bundler'
assets.iconify_bundler:
class: phpbb\assets\iconify_bundler
shared: false
arguments:
- ~
- '%core.root_path%'
cache.driver:
class: '%cache.driver.class%'

View File

@ -1,262 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\assets;
use Iconify\JSONTools\Collection;
use phpbb\log\log_interface;
class iconify_bundler
{
/** @var string Icons path */
protected string $icons_path;
/** @var string[] Icons list */
protected $icons_list = [];
/**
* Constructor for iconify bundler
*
* @param log_interface|null $log Logger
* @param string $root_path phpBB root path
*/
public function __construct(protected ?log_interface $log, string $root_path)
{
$this->icons_path = $root_path . 'assets/iconify/';
}
/**
* Run iconify bundler
*
* @return string Iconify bundle
*/
public function run()
{
// Sort icons first
sort($this->icons_list, SORT_NATURAL);
$organized_icons = $this->organize_icons_list();
$output = $this->load_icons_data($organized_icons);
if (!$output)
{
return '';
}
$output = '(function() {
function add(data) {
try {
if (typeof self.Iconify === \'object\' && self.Iconify.addCollection) {
self.Iconify.addCollection(data);
return;
}
if (typeof self.IconifyPreload === \'undefined\') {
self.IconifyPreload = [];
}
self.IconifyPreload.push(data);
} catch (err) {
}
}
' . $output . '
})();' . "\n";
return $output;
}
/**
* Add icon to icons list
*
* @param string $icon_name
* @return void
*/
protected function add_icon(string $icon_name): void
{
if (!in_array($icon_name, $this->icons_list))
{
$this->icons_list[] = $icon_name;
}
}
/**
* Add multiple icons to icons list
*
* @param array $icons
* @return void
*/
public function add_icons(array $icons): void
{
foreach ($icons as $icon)
{
$this->add_icon($icon);
}
}
/**
* Organize icons list by prefix
*
* Result is an object, where key is prefix, value is array of icon names
*
* @return array Organized icons list
*/
protected function organize_icons_list(): array
{
$results = [];
foreach ($this->icons_list as $icon_name)
{
// Split icon to prefix and name
$icon = $this->name_to_icon($icon_name);
if ($icon === null)
{
// Invalid name or icon name does not have provider
if ($this->log)
{
$this->log->add('critical', ANONYMOUS, '', 'LOG_ICON_INVALID', false, [$icon_name]);
}
continue;
}
$prefix = $icon['prefix'];
$name = $icon['name'];
// Add icon to results
if (!isset($results[$prefix]))
{
$results[$prefix] = [$name];
continue;
}
if (!in_array($name, $results[$prefix]))
{
$results[$prefix][] = $name;
}
}
return $results;
}
/**
* Convert icon name from string to object.
*
* Object properties:
* - provider (ignored in this example)
* - prefix
* - name
*
* This function was converted to PHP from @iconify/utils/src/icon/name.ts
* See https://github.com/iconify/iconify/blob/master/packages/utils/src/icon/name.ts
*
* @param string $icon_name Icon name
* @return array|null Icon data or null if icon is invalid
*/
protected function name_to_icon(string $icon_name): ?array
{
$provider = '';
$colonSeparated = explode(':', $icon_name);
// Check for provider with correct '@' at start
if (substr($icon_name, 0, 1) === '@')
{
// First part is provider
if (count($colonSeparated) < 2 || count($colonSeparated) > 3)
{
return null;
}
$provider = substr(array_shift($colonSeparated), 1);
}
// Check split by colon: "prefix:name", "provider:prefix:name"
if (!$colonSeparated || count($colonSeparated) > 3)
{
return null;
}
if (count($colonSeparated) > 1)
{
// "prefix:name"
$name = array_pop($colonSeparated);
$prefix = array_pop($colonSeparated);
return [
// Allow provider without '@': "provider:prefix:name"
'provider' => count($colonSeparated) > 0 ? $colonSeparated[0] : $provider,
'prefix' => $prefix,
'name' => $name,
];
}
// Attempt to split by dash: "prefix-name"
$dashSeparated = explode('-', $colonSeparated[0]);
if (count($dashSeparated) > 1)
{
return [
'provider' => $provider,
'prefix' => array_shift($dashSeparated),
'name' => implode('-', $dashSeparated),
];
}
return null;
}
/**
* Get collection path for prefix
*
* @param string $prefix Icon collection prefix
* @return string Icon collection path
*/
protected function get_collection_path(string $prefix): string
{
return $this->icons_path . $prefix . '.json';
}
/**
* Load icons date for supplied icons array
*
* @param array $icons
* @return string
*/
protected function load_icons_data(array $icons): string
{
// Load icons data
$output = '';
foreach ($icons as $prefix => $iconsList)
{
// Load icon set
$collection = new Collection($prefix);
$collection_file = $this->get_collection_path($prefix);
if (!file_exists($collection_file) || !$collection->loadFromFile($collection_file))
{
$this->log?->add('critical', ANONYMOUS, '', 'LOG_ICON_COLLECTION_INVALID', false, [$prefix]);
continue;
}
// Make sure all icons exist
foreach ($iconsList as $key => $name)
{
if (!$collection->iconExists($name))
{
$this->log?->add('critical', ANONYMOUS, '', 'LOG_ICON_INVALID', false, [$prefix . ':' . $name]);
unset($iconsList[$key]);
}
}
// Get data for all icons as string
$output .= $collection->scriptify([
'icons' => $iconsList,
'callback' => 'add',
'optimize' => true,
]);
}
return $output;
}
}

View File

@ -13,8 +13,6 @@
namespace phpbb\template;
use phpbb\assets\iconify_bundler;
class assets_bag
{
/** @var asset[] */
@ -23,17 +21,6 @@ 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
*
@ -54,30 +41,6 @@ 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
*
@ -129,22 +92,4 @@ 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

@ -211,7 +211,6 @@ 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 . '__');
}
/**
@ -259,7 +258,6 @@ 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

@ -57,7 +57,7 @@ class icon extends AbstractExtension
* Generate icon HTML for use in the template, depending on the mode.
*
* @param environment $environment Twig environment object
* @param string $type Icon type (font|iconify|png|svg)
* @param string $type Icon type (font|png|svg)
* @param array|string $icon Icon name (eg. "bold")
* @param string $title Icon title
* @param bool $hidden Hide the icon title from view
@ -86,13 +86,6 @@ class icon extends AbstractExtension
// Nothing to do here..
break;
case 'iconify':
$source = explode(':', $icon);
$source = $source[0];
$environment->get_assets_bag()->add_iconify_icon($icon);
break;
case 'png':
$filesystem = $environment->get_filesystem();
$root_path = $environment->get_web_root_path();
@ -137,7 +130,6 @@ class icon extends AbstractExtension
default:
return '';
break;
}
// If no PNG or SVG icon was found, display a default 404 SVG icon.

View File

@ -1,4 +0,0 @@
{% apply spaceless %}
<i class="iconify o-icon-src-{{ SOURCE }} o-icon{{ CLASSES ? ' ' ~ CLASSES }}"{% if S_HIDDEN %}{% if TITLE %} title="{{ TITLE }}"{% endif %} aria-hidden="true"{% endif %} data-icon="{{ ICON }}" data-inline="true"{{ ATTRIBUTES }}></i>
{% if TITLE %}<span{% if S_HIDDEN %} class="sr-only"{% endif %}>{{ TITLE }}</span>{% endif %}
{% endapply %}

View File

@ -1,143 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\tests\unit\assets;
class iconify_bundler_test extends \phpbb_test_case
{
/** @var array Log content */
protected $log_content = [];
/** @var \phpbb\assets\iconify_bundler */
protected $bundler;
public function setUp(): void
{
global $phpbb_root_path;
$log = $this->getMockBuilder('\phpbb\log\dummy')
->onlyMethods(['add'])
->getMock();
$log->method('add')
->willReturnCallback(function ($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array()) {
$this->log_content[] = $log_operation;
});
$this->bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
}
public function data_test_generate_bundle()
{
return [
[
['fa:address-card-o'],
['"prefix":"fa"', '"address-card-o"'],
],
[
['fa:address-card-o', 'ic:baseline-credit-card'],
['"prefix":"fa"', '"address-card-o"', '"prefix":"ic"', '"baseline-credit-card"'],
],
[
['fa:address-card-o', 'fa:foo-bar'],
['"prefix":"fa"', '"address-card-o"'],
['LOG_ICON_INVALID'],
],
[
['fa:address-card-o', 'ic:baseline-credit-card', 'ic:baseline-credit-card'],
['"prefix":"fa"', '"address-card-o"', '"prefix":"ic"', '"baseline-credit-card"'],
],
[
['fa:address-card-o', 'ic:baseline-credit-card', 'ic:baseline-add-ic-call'],
['"prefix":"fa"', '"address-card-o"', '"prefix":"ic"', '"baseline-credit-card"', '"baseline-add-ic-call"'],
],
[
['fa:address-card-o', 'fa:bell', 'ic:baseline-credit-card', 'ic:baseline-add-ic-call'],
['"prefix":"fa"', '"address-card-o"', '"bell"', '"prefix":"ic"', '"baseline-credit-card"', '"baseline-add-ic-call"'],
],
[
['@test'],
[],
['LOG_ICON_INVALID'],
],
[
['fa:address-foo-o'],
['"prefix":"fa"', '"icons":[]'],
['LOG_ICON_INVALID'],
],
[
['foo:bar'],
[],
['LOG_ICON_COLLECTION_INVALID']
],
[
['@iconify:fa:address-card-o'],
['"prefix":"fa"', '"address-card-o"'],
],
[
['@iconify:someother:fa:address-card-o'],
[],
['LOG_ICON_INVALID'],
],
[
['iconify:fa:address-card-o'],
['"prefix":"fa"', '"address-card-o"'],
],
[
['iconify:fa:fa:address-card-o'],
[],
['LOG_ICON_INVALID'],
],
[
['test'],
[],
['LOG_ICON_INVALID'],
],
[
[''],
[],
['LOG_ICON_INVALID'],
],
[
['fa-address-card-o'],
['"prefix":"fa"', '"address-card-o"'],
],
];
}
/**
* @dataProvider data_test_generate_bundle
*/
public function test_generate_bundle($icons, $expected, $log_content = [])
{
$this->bundler->add_icons($icons);
$bundle = $this->bundler->run();
foreach ($expected as $expected_part)
{
$this->assertStringContainsString($expected_part, $bundle, 'Failed asserting that generated bundle contains ' . $expected_part);
}
if (!count($expected))
{
$this->assertEquals($bundle, '', 'Failed asserting that generated bundle is empty');
}
if (count($log_content))
{
$this->assertEquals($this->log_content, $log_content, 'Failed asserting that log content is correct');
}
else
{
$this->assertEmpty($this->log_content, 'Failed asserting that log content is empty');
}
}
}

View File

@ -117,8 +117,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_database_test_
$container->setParameter('core.environment', PHPBB_ENVIRONMENT);
$cache_path = $phpbb_root_path . 'cache/twig';
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$twig = new \phpbb\template\twig\environment(

View File

@ -67,8 +67,7 @@ class phpbb_email_parsing_test extends phpbb_test_case
$phpbb_container->set('ext.manager', $extension_manager);
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$phpbb_container->set('assets.bag', $assets_bag);
$context = new \phpbb\template\context();

View File

@ -67,8 +67,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
$this->phpEx
);
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $this->phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$this->config,

View File

@ -96,8 +96,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader([]);
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$config,

View File

@ -59,8 +59,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$config,

View File

@ -153,8 +153,7 @@ Zeta test event in all',
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$config,

View File

@ -45,8 +45,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$config,

View File

@ -96,8 +96,7 @@ class phpbb_template_template_test_case extends phpbb_test_case
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$config,

View File

@ -46,8 +46,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$config,

View File

@ -64,8 +64,7 @@ class twig_test extends \phpbb_test_case
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$log = new \phpbb\log\dummy();
$iconify_bundler = new \phpbb\assets\iconify_bundler($log, $phpbb_root_path);
$assets_bag = new \phpbb\template\assets_bag($iconify_bundler);
$assets_bag = new \phpbb\template\assets_bag();
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$config,