[ticket/16944] Start changing bundler to be used outside of develop scripts

PHPBB3-16944
This commit is contained in:
Marc Alexander 2023-07-29 14:44:13 +02:00
parent 7f80772eda
commit a531d6071b
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
4 changed files with 89 additions and 16 deletions

View file

@ -1,4 +1,5 @@
imports: imports:
- { resource: services_assets.yml }
- { resource: services_attachment.yml } - { resource: services_attachment.yml }
- { resource: services_auth.yml } - { resource: services_auth.yml }
- { resource: services_avatar.yml } - { resource: services_avatar.yml }

View file

@ -0,0 +1,7 @@
services:
assets.iconify_bundler:
class: phpbb\assets\iconify_bundler
arguments:
- '@dbal.conn'
- '@ext.manager'
- '%core.root_path%'

View file

@ -21,18 +21,22 @@ define('IN_PHPBB', true);
$phpbb_root_path = dirname(__FILE__) . '/../'; $phpbb_root_path = dirname(__FILE__) . '/../';
$phpEx = substr(strrchr(__FILE__, '.'), 1); $phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'vendor/autoload.php'); include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx); /** @var \phpbb\assets\iconify_bundler $iconify_bundler */
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx); $iconify_bundler = $phpbb_container->get('assets.iconify_bundler');
$phpbb_class_loader->register();
// JS file to save bundle to // JS file to save bundle to
$target = $phpbb_root_path . 'assets/iconify/iconify-bundle.js'; $target = $phpbb_root_path . 'assets/iconify/iconify-bundle.js';
// Icons to bundle, the list of iconify icons used in phpBB // Icons to bundle, the list of iconify icons used in phpBB
$iconify_bundler = new \phpbb\assets\iconify_bundler($phpbb_root_path); $iconify_bundler->find_icons([
$output = $iconify_bundler->run(); $phpbb_root_path . 'styles/',
$phpbb_root_path . 'adm/style/',
]);
$output = $iconify_bundler->with_extensions()
->with_styles()
->run();
// Save to file // Save to file
file_put_contents($target, $output); file_put_contents($target, $output);

View file

@ -19,17 +19,24 @@ use Symfony\Component\Finder\Finder;
class iconify_bundler class iconify_bundler
{ {
protected $db;
protected $ext_manager;
protected $root_path = ''; protected $root_path = '';
protected $icons_list = []; protected $icons_list = [];
public function __construct(string $root_path) public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\extension\manager $ext_manager, string $root_path)
{ {
$this->db = $db;
$this->ext_manager = $ext_manager;
$this->root_path = $root_path; $this->root_path = $root_path;
} }
public function run() public function run()
{ {
$this->find_icons(); // Sort icons first
sort($this->icons_list, SORT_NATURAL);
$organized_icons = $this->organize_icons_list(); $organized_icons = $this->organize_icons_list();
@ -55,12 +62,27 @@ class iconify_bundler
return $output; return $output;
} }
protected function find_icons() /**
* @param array $paths Icon paths
*
* @return void
*/
public function find_icons(array $paths): void
{ {
if (!count($paths))
{
return;
}
$finder = new Finder(); $finder = new Finder();
$finder->files()->in($this->root_path . '/styles/') $finder->files();
->in($this->root_path . '/adm/style/')
->name('*.html') foreach ($paths as $cur_path)
{
$finder->in($cur_path);
}
$finder->name('*.html')
->name('*.twig') ->name('*.twig')
->contains("Icon('iconify',"); ->contains("Icon('iconify',");
@ -95,8 +117,44 @@ class iconify_bundler
} }
} }
} }
}
sort($this->icons_list, SORT_NATURAL); public function with_extensions(): iconify_bundler
{
$extensions = $this->ext_manager->all_enabled();
$search_paths = [];
foreach ($extensions as $path)
{
if (file_exists($path))
{
$search_paths[] = $path;
}
}
$this->find_icons($search_paths);
return $this;
}
public function with_styles(): iconify_bundler
{
$sql = 'SELECT *
FROM ' . STYLES_TABLE;
$result = $this->db->sql_query($sql);
$style_paths = [];
while ($row = $this->db->sql_fetchrow($result))
{
$style_paths[] = $this->root_path . 'styles/' . $row['style_path'];
}
$this->db->sql_freeresult($result);
$this->find_icons($style_paths);
return $this;
} }
protected function add_icon(string $icon_name): void protected function add_icon(string $icon_name): void
@ -111,6 +169,8 @@ class iconify_bundler
* Organize icons list by prefix * Organize icons list by prefix
* *
* Result is an object, where key is prefix, value is array of icon names * 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 protected function organize_icons_list(): array
{ {
@ -156,7 +216,8 @@ class iconify_bundler
* This function was converted to PHP from @iconify/utils/src/icon/name.ts * 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 * See https://github.com/iconify/iconify/blob/master/packages/utils/src/icon/name.ts
* *
* @param string $icon_name * @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 protected function name_to_icon(string $icon_name): ?array
{ {
@ -216,7 +277,7 @@ class iconify_bundler
// Load icon set // Load icon set
$collection = new Collection($prefix); $collection = new Collection($prefix);
if (!$collection->loadIconifyCollection($prefix)) { if (!$collection->loadIconifyCollection($prefix)) {
throw new Error( throw new \Error(
'Icons with prefix "' . $prefix . '" do not exist in Iconify. Update iconify/json?' 'Icons with prefix "' . $prefix . '" do not exist in Iconify. Update iconify/json?'
); );
} }