lib/composer/LisaBundle/src/VisualMedia/LisaBundle/Component/TwigFilesystemLoader.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3. * Twig Filesystem Loader
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\LisaBundle\Component;
  8. use Twig\Loader\FilesystemLoader;
  9. use Twig_Error_Loader;
  10. use Twig_Loader_Filesystem;
  11. use VisualMedia\TranslationBundle\Service\DomainProvider;
  12. /**
  13. * Twig Filesystem Loader
  14. */
  15. class TwigFilesystemLoader extends FilesystemLoader
  16. {
  17. public const PLACEHOLDER = '{domain}';
  18. /**
  19. * Initialized
  20. *
  21. * @var bool
  22. */
  23. private $initialized = false;
  24. /**
  25. * @var DomainProvider
  26. */
  27. private $domainProvider;
  28. public function __construct($paths = array(), $rootPath = null, DomainProvider $domainProvider = null)
  29. {
  30. parent::__construct($paths, $rootPath);
  31. $this->domainProvider = $domainProvider;
  32. }
  33. protected function findTemplate($name, $throw = true)
  34. {
  35. if (!$this->initialized) {
  36. $this->initialized = true;
  37. if ($this->domainProvider !== null && null !== $currentDomain = $this->domainProvider->getCurrentDomain()) {
  38. foreach($this->paths as $namespace => $paths) {
  39. foreach($paths as $index => $path) {
  40. $this->paths[$namespace][$index] = str_replace(static::PLACEHOLDER, $currentDomain->getKey(), $path);
  41. }
  42. }
  43. }
  44. }
  45. return parent::findTemplate($name, $throw);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function addPath($path, $namespace = self::MAIN_NAMESPACE)
  51. {
  52. $this->paths[$namespace][] = rtrim($path, '/\\');
  53. }
  54. }