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

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 $domain, BaseLocale $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 $channelManager, BaseDomainManager $domainManager, BaseLocaleManager $localeManager, RequestStack $requestStack, ParameterBagInterface $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()) > 2 ? $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. $channels = $this->channelManager->getIndex();
  257. $domains = $this->domainManager->getIndex();
  258. $locales = $this->localeManager->getIndex();
  259. if (!$locale = reset($locales)) {
  260. $locales[] = $locale = new BaseLocale('nl_NL');
  261. $this->localeManager->persist($locale);
  262. }
  263. if (!$channel = reset($channels)) {
  264. $channels[] = $channel = new BaseChannel($request->getHost());
  265. $channel->main = true;
  266. $channel->defaultLocale = $locale;
  267. $this->channelManager->persist($channel);
  268. }
  269. if (!$domain = reset($domains)) {
  270. $domains[] = $domain = new BaseDomain($request->getHost());
  271. $domain->channel = $channel;
  272. $domain->identifier = 'main';
  273. $domain->locales->add($locale);
  274. $this->domainManager->persist($domain);
  275. }
  276. $this->currentChannel = $channel;
  277. $this->currentDomain = $domain;
  278. $this->currentLocale = $locale;
  279. $this->channelManager->flush();
  280. }
  281. }