vendor/ibexa/admin-ui/src/lib/Resolver/IconPathResolver.php line 61

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\AdminUi\Resolver;
  8. use Ibexa\Contracts\AdminUi\Resolver\IconPathResolverInterface;
  9. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  10. use Ibexa\Core\MVC\Symfony\Event\ScopeChangeEvent;
  11. use Ibexa\Core\MVC\Symfony\MVCEvents;
  12. use Symfony\Component\Asset\Packages;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. /**
  15. * @internal
  16. */
  17. final class IconPathResolver implements IconPathResolverInterface, EventSubscriberInterface
  18. {
  19. private ConfigResolverInterface $configResolver;
  20. private Packages $packages;
  21. /** @var string[] */
  22. private array $iconCache;
  23. public function __construct(
  24. ConfigResolverInterface $configResolver,
  25. Packages $packages
  26. ) {
  27. $this->configResolver = $configResolver;
  28. $this->packages = $packages;
  29. $this->iconCache = [];
  30. }
  31. public function resolve(string $icon, ?string $set = null): string
  32. {
  33. if (isset($this->iconCache[$set][$icon])) {
  34. return $this->iconCache[$set][$icon];
  35. }
  36. $iconSetName = $set ?? $this->configResolver->getParameter('assets.default_icon_set');
  37. $iconSets = $this->configResolver->getParameter('assets.icon_sets');
  38. $this->iconCache[$set][$icon] = sprintf('%s#%s', $this->packages->getUrl($iconSets[$iconSetName]), $icon);
  39. return $this->iconCache[$set][$icon];
  40. }
  41. public static function getSubscribedEvents(): array
  42. {
  43. return [
  44. MVCEvents::CONFIG_SCOPE_CHANGE => ['onConfigScopeChange', 100],
  45. MVCEvents::CONFIG_SCOPE_RESTORE => ['onConfigScopeChange', 100],
  46. ];
  47. }
  48. public function onConfigScopeChange(ScopeChangeEvent $event): void
  49. {
  50. $this->iconCache = [];
  51. }
  52. }
  53. class_alias(IconPathResolver::class, 'Ibexa\Platform\Assets\Resolver\IconPathResolver');