lib/composer/TranslationBundle/src/VisualMedia/TranslationBundle/Twig/TwigChannel.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3. * Twig Channel
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@dappr.nl>
  6. */
  7. namespace VisualMedia\TranslationBundle\Twig;
  8. use Exception;
  9. use Twig\Extension\AbstractExtension;
  10. use Twig\TwigFilter;
  11. use Twig\TwigFunction;
  12. use VisualMedia\TranslationBundle\Entity\BaseChannel;
  13. use VisualMedia\TranslationBundle\Entity\BaseDomain;
  14. use VisualMedia\TranslationBundle\Service\ChannelProvider;
  15. /**
  16. * Twig Channel
  17. */
  18. class TwigChannel extends AbstractExtension
  19. {
  20. /**
  21. * @param ChannelProvider $channelProvider
  22. */
  23. public function __construct(ChannelProvider $channelProvider)
  24. {
  25. $this->channelProvider = $channelProvider;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getFunctions(): array
  31. {
  32. return array(
  33. new TwigFunction('current_channel', array($this, 'currentChannel')),
  34. new TwigFunction('current_domain', array($this, 'currentDomain')),
  35. );
  36. }
  37. /**
  38. * @return BaseChannel|null
  39. */
  40. public function currentChannel(): ?BaseChannel
  41. {
  42. return $this->channelProvider->getCurrentChannel();
  43. }
  44. /**
  45. * @return BaseDomain|null
  46. */
  47. public function currentDomain(): ?BaseDomain
  48. {
  49. if (null === $domain = $this->channelProvider->getCurrentDomain()) {
  50. return null;
  51. }
  52. if (empty($domain->identifier)) {
  53. throw new Exception(sprintf('Identifier has not been set for domain#%d \'%s\'. Please configure the identifier in the channel or directly in the database.', $domain->id, $domain->host));
  54. }
  55. return $domain;
  56. }
  57. }