vendor/symfony/ux-live-component/src/Twig/TemplateMap.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\UX\LiveComponent\Twig;
  11. use Symfony\Component\Cache\Adapter\NullAdapter;
  12. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  13. /**
  14. * @author Bart Vanderstukken <bart.vanderstukken@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class TemplateMap
  19. {
  20. /**
  21. * @var array<string, string> Map of <obscured name> => <template name>
  22. */
  23. private readonly array $map;
  24. public function __construct(string $cacheFile)
  25. {
  26. $this->map = PhpArrayAdapter::create($cacheFile, new NullAdapter())->getItem('map')->get();
  27. }
  28. public function resolve(string $obscuredName): string
  29. {
  30. return $this->map[$obscuredName] ?? throw new \RuntimeException(\sprintf('Cannot find a template matching "%s". Cache may be corrupt.', $obscuredName));
  31. }
  32. public function obscuredName(string $templateName): string
  33. {
  34. if (false === $obscuredName = array_search($templateName, $this->map, true)) {
  35. throw new \RuntimeException(\sprintf('Cannot find a match for template "%s". Cache may be corrupt.', $templateName));
  36. }
  37. return $obscuredName;
  38. }
  39. }