Skip to content

Commit d5d8e69

Browse files
committed
Find all the prefix-less icons
1 parent 8ad7fb0 commit d5d8e69

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/Icons/config/services.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@
5050
])
5151
->tag('twig.runtime')
5252

53-
->set('.ux_icons.twig_icon_finder', IconFinder::class)
53+
->set('.ux_icons.icon_finder', IconFinder::class)
5454
->args([
5555
service('twig'),
56+
abstract_arg('icon_dir'),
5657
])
5758

5859
->set('.ux_icons.cache_warmer', IconCacheWarmer::class)
5960
->args([
6061
service('.ux_icons.cache_icon_registry'),
61-
service('.ux_icons.twig_icon_finder'),
62+
service('.ux_icons.icon_finder'),
6263
])
6364

6465
->set('.ux_icons.command.warm_cache', WarmCacheCommand::class)

src/Icons/src/DependencyInjection/UXIconsExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
8787
])
8888
;
8989

90+
$container->getDefinition('.ux_icons.icon_finder')
91+
->setArgument(1, $mergedConfig['icon_dir'])
92+
;
93+
9094
$container->getDefinition('.ux_icons.icon_renderer')
9195
->setArgument(1, $mergedConfig['default_icon_attributes'])
9296
;

src/Icons/src/Twig/IconFinder.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
*/
2525
final class IconFinder
2626
{
27-
public function __construct(private Environment $twig)
28-
{
27+
public function __construct(
28+
private Environment $twig,
29+
private string $iconDirectory,
30+
) {
2931
}
3032

3133
/**
@@ -39,20 +41,30 @@ public function icons(): array
3941
$token = '[a-z0-9]+(?:-[a-z0-9]+)*';
4042
$pattern = "#(?:'$token:$token')|(?:\"$token:$token\")#i";
4143

42-
foreach ($this->files($this->twig->getLoader()) as $file) {
44+
// Extract icon names from strings in app templates
45+
foreach ($this->templateFiles($this->twig->getLoader()) as $file) {
4346
$contents = file_get_contents($file);
4447
if (preg_match_all($pattern, $contents, $matches)) {
4548
$found[] = array_map(fn ($res) => trim($res, '"\''), $matches[0]);
4649
}
4750
}
51+
$found = array_merge(...$found);
52+
53+
// Extract prefix-less SVG files from the root of the icon directory
54+
if (is_dir($this->iconDirectory)) {
55+
$icons = (new Finder())->files()->in($this->iconDirectory)->depth(0)->name('*.svg');
56+
foreach ($icons as $icon) {
57+
$found[] = $icon->getBasename('.svg');
58+
}
59+
}
4860

49-
return array_unique(array_merge(...$found));
61+
return array_unique($found);
5062
}
5163

5264
/**
5365
* @return string[]
5466
*/
55-
private function files(LoaderInterface $loader): iterable
67+
private function templateFiles(LoaderInterface $loader): iterable
5668
{
5769
if ($loader instanceof FilesystemLoader) {
5870
$paths = [];
@@ -66,7 +78,7 @@ private function files(LoaderInterface $loader): iterable
6678

6779
if ($loader instanceof ChainLoader) {
6880
foreach ($loader->getLoaders() as $subLoader) {
69-
yield from $this->files($subLoader);
81+
yield from $this->templateFiles($subLoader);
7082
}
7183
}
7284
}

0 commit comments

Comments
 (0)