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

Merge pull request #6581 from marc1706/ticket/17283

[ticket/17283] Remove iconify support
This commit is contained in:
Marc Alexander
2024-02-22 19:49:18 +01:00
committed by GitHub
105 changed files with 450 additions and 110736 deletions

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

@@ -0,0 +1,34 @@
<?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\db\migration\data\v400;
use phpbb\db\migration\migration;
class font_awesome_6_upgrade extends migration
{
public static function depends_on(): array
{
return [
'\phpbb\db\migration\data\v400\dev',
'\phpbb\db\migration\data\v33x\font_awesome_5_rollback',
];
}
public function update_data(): array
{
return [
['config.update', ['load_font_awesome_url', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css']],
];
}
}

View File

@@ -26,7 +26,11 @@ use phpbb\filesystem\helper as filesystem_helper;
*/
class core extends Extension
{
const TWIG_OPTIONS_POSITION = 7;
/**
* Index of array in service template.twig.environment inside services_twig.yml
* @var int
*/
const TWIG_OPTIONS_POSITION = 8;
/**
* Config path
@@ -86,7 +90,7 @@ class core extends Extension
$twig_environment_options['auto_reload'] = true;
}
// Replace the 7th argument, the options passed to the environment
// Replace the 8th argument, the options passed to the environment
$definition->replaceArgument(static::TWIG_OPTIONS_POSITION, $twig_environment_options);
if ($config['twig']['enable_debug_extension'])

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.