lib/composer/TranslationBundle/src/VisualMedia/TranslationBundle/Service/ChannelProvider.php line 191

Open in your IDE?
  1. <?php
  2. /**
  3.  * Channel Provider
  4.  *
  5.  * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6.  */
  7. namespace VisualMedia\TranslationBundle\Service;
  8. use Exception;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use VisualMedia\LisaBundle\Helper\Helper;
  13. use VisualMedia\LisaBundle\Component\Interfaces\EntityInterface;
  14. use VisualMedia\LisaBundle\Component\ManagerData;
  15. use VisualMedia\TranslationBundle\Manager\BaseChannelManager;
  16. use VisualMedia\TranslationBundle\Manager\BaseDomainManager;
  17. use VisualMedia\TranslationBundle\Manager\BaseLocaleManager;
  18. use VisualMedia\TranslationBundle\Entity\BaseChannel;
  19. use VisualMedia\TranslationBundle\Entity\BaseDomain;
  20. use VisualMedia\TranslationBundle\Entity\BaseLocale;
  21. use VisualMedia\TranslationBundle\Form\Subscriber\SimpleContentsSubscriber;
  22. use VisualMedia\TranslationBundle\Form\Subscriber\LocaleContentsSubscriber;
  23. use VisualMedia\TranslationBundle\Form\Subscriber\ChannelLocaleContentsSubscriber;
  24. /**
  25.  * Switch Link
  26.  */
  27. class SwitchLink
  28. {
  29.     public $domain;
  30.     public $locale;
  31.     /**
  32.      * Constructor
  33.      *
  34.      * @param BaseDomain $domain
  35.      * @param BaseLocale $locale
  36.      */
  37.     public function __construct(BaseDomain $domainBaseLocale $locale)
  38.     {
  39.         $this->domain $domain;
  40.         $this->locale $locale;
  41.     }
  42. }
  43. /**
  44.  * Channel Provider
  45.  */
  46. class ChannelProvider
  47. {
  48.     public const MODE_SIMPLE 'simple';
  49.     public const MODE_LOCALE 'locale';
  50.     public const MODE_CHANNEL_LOCALE 'channel_locale';
  51.     /**
  52.      * @var BaseChannelManager
  53.      */
  54.     protected $channelManager;
  55.     /**
  56.      * @var BaseDomainManager
  57.      */
  58.     protected $domainManager;
  59.     /**
  60.      * @var BaseLocaleManager
  61.      */
  62.     protected $localeManager;
  63.     /**
  64.      * @var RequestStack
  65.      */
  66.     protected $requestStack;
  67.     /**
  68.      * @var ParameterBagInterface
  69.      */
  70.     protected $params;
  71.     /**
  72.      * @var array
  73.      */
  74.     protected $channels;
  75.     /**
  76.      * @var BaseChannel
  77.      */
  78.     protected $currentChannel;
  79.     /**
  80.      * @var BaseDomain
  81.      */
  82.     protected $currentDomain;
  83.     /**
  84.      * @var BaseLocale
  85.      */
  86.     protected $currentLocale;
  87.     /**
  88.      * @var string
  89.      */
  90.     protected $mode;
  91.     /**
  92.      * @param BaseChannelManager $channelManager
  93.      * @param BaseDomainManager $domainManager
  94.      * @param BaseLocaleManager $localeManager
  95.      * @param RequestStack $requestStack
  96.      * @param ParameterBagInterface $params
  97.      */
  98.     public function __construct(BaseChannelManager $channelManagerBaseDomainManager $domainManagerBaseLocaleManager $localeManagerRequestStack $requestStackParameterBagInterface $params)
  99.     {
  100.         $this->channelManager $channelManager;
  101.         $this->domainManager $domainManager;
  102.         $this->localeManager $localeManager;
  103.         $this->requestStack $requestStack;
  104.         $this->params $params;
  105.     }
  106.     /**
  107.      * @param string $mode
  108.      * @return void
  109.      */
  110.     public function initialize(string $mode): void
  111.     {
  112.         $this->mode $mode;
  113.     }
  114.     /**
  115.      * @return string
  116.      */
  117.     public function getMode(): string
  118.     {
  119.         return $this->mode;
  120.     }
  121.     /**
  122.      * @return bool
  123.      */
  124.     public function hasMultiple(): bool
  125.     {
  126.         return count($this->getChannels()) > 1;
  127.     }
  128.     /**
  129.      * @return BaseLocale|null
  130.      */
  131.     public function getCurrentLocale(): ?BaseLocale
  132.     {
  133.         if ($this->currentLocale) {
  134.             return $this->currentLocale;
  135.         }
  136.         // no request, fallback default locale from config as param.
  137.         $locale $this->params->get('locale') ?? 'nl_NL';
  138.         if (null !== $request $this->requestStack->getMasterRequest()) {
  139.             $locale strlen($request->getLocale()) > $request->getLocale() : $request->getDefaultLocale();
  140.         }
  141.         if (null === $this->currentLocale $this->currentLocale ?: $this->localeManager->getByLocale($locale)) {
  142.             $this->validate();
  143.             return $this->getCurrentLocale();
  144.             // if (!$request->hasSession()) {
  145.             //     return new BaseLocale($locale);
  146.             // }
  147.             // throw new Exception(sprintf('Locale \'%s\' is not yet configured in the database.', $locale));
  148.         }
  149.         return $this->currentLocale;
  150.     }
  151.     /**
  152.      * @return BaseDomain|null
  153.      */
  154.     public function getCurrentDomain(): ?BaseDomain
  155.     {
  156.         if ($this->currentDomain) {
  157.             return $this->currentDomain;
  158.         }
  159.         if (null === $request $this->requestStack->getMasterRequest()) {
  160.             return null;
  161.         }
  162.         if (null === $this->currentDomain $this->currentDomain ?: $this->getDomainByHost($request->getHost())) {
  163.             $this->validate();
  164.             return $this->getCurrentDomain();
  165.             // throw new Exception(sprintf('Domain with host \'%s\' is not yet configured in the database.', $request->getHost()));
  166.         }
  167.         return $this->currentDomain;
  168.     }
  169.     /**
  170.      * @return BaseChannel|null
  171.      */
  172.     public function getCurrentChannel(): ?BaseChannel
  173.     {
  174.         if ($this->currentChannel) {
  175.             return $this->currentChannel;
  176.         }
  177.         if (null === $request $this->requestStack->getMasterRequest()) {
  178.             return null;
  179.         }
  180.         $domain $this->getCurrentDomain();
  181.         if (null === $this->currentChannel $this->currentChannel ?: $domain->channel ?? null) {
  182.             $this->validate();
  183.             return $this->getCurrentChannel();
  184.         }
  185.         return $this->currentChannel;
  186.     }
  187.     /**
  188.      * @param string $host
  189.      * @return BaseDomain
  190.      */
  191.     public function getDomainByHost(string $host): ?BaseDomain
  192.     {
  193.         return $this->domainManager->getByHost($host);
  194.     }
  195.     /**
  196.      * @return array
  197.      */
  198.     public function getChannels(): array
  199.     {
  200.         return $this->channels $this->channels ?: $this->channelManager->getIndex();
  201.     }
  202.     /**
  203.      * @param BaseChannel $channel
  204.      * @return array
  205.      */
  206.     public function getSwitch(BaseChannel $channel): array
  207.     {
  208.         $switch = [];
  209.         foreach ($channel->domains as $domain) {
  210.             if (count($domain->locales) > 1) {
  211.                 foreach ($domain->locales as $locale) {
  212.                     $switch[] = $new = new SwitchLink($domain$locale);
  213.                 }
  214.             }
  215.             elseif ($channel->defaultLocale) {
  216.                 $switch[] = $new = new SwitchLink($domain$channel->defaultLocale);
  217.             }
  218.         }
  219.         return $switch;
  220.     }
  221.     /**
  222.      * @return array
  223.      */
  224.     public function getLocalesForEntity(EntityInterface $entity): array
  225.     {
  226.         $locales = [];
  227.         foreach ($entity->channels as $channel) {
  228.             foreach ($channel->domains as $domain) {
  229.                 if ($channel->defaultLocale) {
  230.                     $locales[$channel->defaultLocale->code] = $channel->defaultLocale;
  231.                 }
  232.                 foreach ($domain->locales as $locale) {
  233.                     $locales[$locale->code] = $locale;
  234.                 }
  235.             }
  236.         }
  237.         return $locales;
  238.     }
  239.     /**
  240.      * @return void
  241.      */
  242.     public function addContentsSubscriber(FormBuilderInterface $builder, array $options): void
  243.     {
  244.         switch ($this->getMode()) {
  245.             case static::MODE_SIMPLE$builder->addEventSubscriber(new SimpleContentsSubscriber($this)); break;
  246.             case static::MODE_LOCALE$builder->addEventSubscriber(new LocaleContentsSubscriber($this)); break;
  247.             case static::MODE_CHANNEL_LOCALE$builder->addEventSubscriber(new ChannelLocaleContentsSubscriber($this)); break;
  248.         }
  249.     }
  250.     /**
  251.      * @return void
  252.      */
  253.     public function validate(): void
  254.     {
  255.         $request $this->requestStack->getMasterRequest();
  256.         
  257.         $channels $this->channelManager->getIndex();
  258.         $domains $this->domainManager->getIndex();
  259.         $locales $this->localeManager->getIndex();
  260.         if (!$locale reset($locales)) {
  261.             $locales[] = $locale = new BaseLocale('nl_NL');
  262.             $this->localeManager->persist($locale);
  263.         }
  264.         if (!$channel reset($channels)) {
  265.             $channels[] = $channel = new BaseChannel($request->getHost());
  266.             $channel->main true;
  267.             $channel->defaultLocale $locale;
  268.             $this->channelManager->persist($channel);
  269.         }
  270.         if (!$domain reset($domains)) {
  271.             $domains[] = $domain = new BaseDomain($request->getHost());
  272.             $domain->channel $channel;
  273.             $domain->identifier 'main';
  274.             
  275.             $domain->locales->add($locale);
  276.             $this->domainManager->persist($domain);
  277.         }
  278.         $this->currentChannel $channel;
  279.         $this->currentDomain $domain;
  280.         $this->currentLocale $locale;
  281.         $this->channelManager->flush();
  282.     }
  283. }