Skip to content

[Icons] Rework cache warm #1608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Icons/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@
])
->tag('twig.runtime')

->set('.ux_icons.twig_icon_finder', IconFinder::class)
->set('.ux_icons.icon_finder', IconFinder::class)
->args([
service('twig'),
abstract_arg('icon_dir'),
])

->set('.ux_icons.cache_warmer', IconCacheWarmer::class)
->args([
service('.ux_icons.cache_icon_registry'),
service('.ux_icons.twig_icon_finder'),
service('.ux_icons.icon_finder'),
])

->set('.ux_icons.command.warm_cache', WarmCacheCommand::class)
Expand Down
4 changes: 4 additions & 0 deletions src/Icons/src/DependencyInjection/UXIconsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
])
;

$container->getDefinition('.ux_icons.icon_finder')
->setArgument(1, $mergedConfig['icon_dir'])
;

$container->getDefinition('.ux_icons.icon_renderer')
->setArgument(1, $mergedConfig['default_icon_attributes'])
;
Expand Down
48 changes: 28 additions & 20 deletions src/Icons/src/Twig/IconFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
*/
final class IconFinder
{
public function __construct(private Environment $twig)
{
public function __construct(
private Environment $twig,
private string $iconDirectory,
) {
}

/**
Expand All @@ -35,42 +37,48 @@ public function icons(): array
{
$found = [];

foreach ($this->files($this->twig->getLoader()) as $file) {
// https://regex101.com/r/WGa4iF/1
$token = '[a-z0-9]+(?:-[a-z0-9]+)*';
$pattern = "#(?:'$token:$token')|(?:\"$token:$token\")#i";

// Extract icon names from strings in app templates
foreach ($this->templateFiles($this->twig->getLoader()) as $file) {
$contents = file_get_contents($file);
if (preg_match_all($pattern, $contents, $matches)) {
$found[] = array_map(fn ($res) => trim($res, '"\''), $matches[0]);
}
}
$found = array_merge(...$found);

if (preg_match_all('#["\']([\w:-]+)["\']#', $contents, $matches)) {
$found[] = $matches[1];
// Extract prefix-less SVG files from the root of the icon directory
if (is_dir($this->iconDirectory)) {
$icons = (new Finder())->files()->in($this->iconDirectory)->depth(0)->name('*.svg');
foreach ($icons as $icon) {
$found[] = $icon->getBasename('.svg');
}
}

return array_unique(array_merge(...$found));
return array_unique($found);
}

/**
* @return string[]
*/
private function files(LoaderInterface $loader): iterable
private function templateFiles(LoaderInterface $loader): iterable
{
$files = [];

if ($loader instanceof FilesystemLoader) {
$paths = [];
foreach ($loader->getNamespaces() as $namespace) {
foreach ($loader->getPaths($namespace) as $path) {
foreach ((new Finder())->files()->in($path)->name('*.twig') as $file) {
$file = (string) $file;
if (!\in_array($file, $files, true)) {
yield $file;
}

$files[] = $file;
}
}
$paths = [...$paths, ...$loader->getPaths($namespace)];
}
foreach ((new Finder())->files()->in($paths)->name('*.twig') as $file) {
yield (string) $file;
}
}

if ($loader instanceof ChainLoader) {
foreach ($loader->getLoaders() as $subLoader) {
yield from $this->files($subLoader);
yield from $this->templateFiles($subLoader);
}
}
}
Expand Down