lib/composer/TranslationBundle/src/VisualMedia/TranslationBundle/Service/DomainProvider.php line 78

Open in your IDE?
  1. <?php
  2. /**
  3. * Domain Provider
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\TranslationBundle\Service;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use VisualMedia\LisaBundle\Helper\Helper;
  10. use VisualMedia\LisaBundle\Component\ManagerData;
  11. use VisualMedia\LisaBundle\Component\Interfaces\ServiceInterface;
  12. use VisualMedia\TranslationBundle\Manager\BaseDomainManager;
  13. use VisualMedia\TranslationBundle\Entity\BaseDomain;
  14. /**
  15. * Domain Provider
  16. */
  17. class DomainProvider implements ServiceInterface
  18. {
  19. public const SERVICE_ID = 'visualmedia.lisa.provider.domain';
  20. public const DEFAULT = 'default';
  21. public const SESSION_KEY_DOMAIN = 'domain';
  22. public const DEFAULT_LANGUAGE = 'nl-NL';
  23. public const MODE_GLOBAL = 'global';
  24. public const MODE_DEFAULT = 'default domain';
  25. public const MODE_CONSTRUCTED = 'constructed';
  26. public const MODE_QUERY_SESSION = 'query / session';
  27. /**
  28. * Request
  29. * @var Request
  30. */
  31. protected $request;
  32. /**
  33. * Manager
  34. * @var BaseDomainManager
  35. */
  36. protected $manager;
  37. /**
  38. * Domains
  39. * @var array
  40. */
  41. protected $domains;
  42. /**
  43. * Current Domain
  44. * @var BaseDomain
  45. */
  46. protected $currentDomain;
  47. /**
  48. * Default Domain
  49. * @var BaseDomain
  50. */
  51. protected $defaultDomain;
  52. /**
  53. * Mode
  54. * @var string
  55. */
  56. protected $mode;
  57. /**
  58. * Constructor
  59. *
  60. * @param RequestStack $requestStack
  61. */
  62. public function __construct(RequestStack $requestStack, BaseDomainManager $manager)
  63. {
  64. $this->request = $requestStack->getMasterRequest();
  65. $this->manager = $manager;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public static function getServiceId(): string
  71. {
  72. return static::SERVICE_ID;
  73. }
  74. /**
  75. * Get Mapped Domains
  76. *
  77. * @return array
  78. */
  79. protected function getMappedDomains(): array
  80. {
  81. return [];
  82. if ($this->domains === null) {
  83. $this->domains = array();
  84. foreach ($this->manager->getIndex() as $domain) {
  85. $this->domains[(string)$domain->getId()] = $domain;
  86. $this->domains[(string)$domain->getHost()] = $domain;
  87. if ($domain->getDefault()) {
  88. $this->domains[static::DEFAULT] = $domain;
  89. }
  90. }
  91. }
  92. return $this->domains;
  93. }
  94. /**
  95. * Get Domains
  96. *
  97. * @return array
  98. */
  99. public function getDomains(): array
  100. {
  101. return [];
  102. $domains = array();
  103. foreach ($this->getMappedDomains() as $key=>$domain) {
  104. if (is_numeric($key)) {
  105. $domains[(int)$key] = $domain;
  106. }
  107. }
  108. return $domains;
  109. }
  110. /**
  111. * Get Domain
  112. *
  113. * @param mixed $key
  114. *
  115. * @return BaseDomain
  116. */
  117. public function getDomain($key): ?BaseDomain
  118. {
  119. return null;
  120. return $this->getMappedDomains()[$key] ?? null;
  121. }
  122. /**
  123. * Get Default Domain
  124. *
  125. * @return BaseDomain
  126. */
  127. public function getDefaultDomain(): ?BaseDomain
  128. {
  129. return null;
  130. $this->mode = static::MODE_DEFAULT;
  131. return $this->getMappedDomains()[static::DEFAULT] ?? null;
  132. }
  133. /**
  134. * Get Current Domain
  135. *
  136. * @return BaseDomain
  137. */
  138. public function getCurrentDomain(): ?BaseDomain
  139. {
  140. return null;
  141. // Cached.
  142. // @TODO Caching current domain causes issue in edit mode
  143. // if ($this->currentDomain !== null) {
  144. // return $this->currentDomain;
  145. // }
  146. // Return default Domain if the request doesn't exist.
  147. if ($this->request === null) {
  148. return $this->currentDomain = $this->getDefaultDomain();
  149. }
  150. // treat as frontend
  151. if($this->request->attributes->get('_route') === 'manage_login') {
  152. return $this->getCurrentFrontendDomain();
  153. }
  154. // Check frontend / backend.
  155. if ($this->request->attributes->get('_firewall_context') === 'security.firewall.map.context.manage') {
  156. return $this->getCurrentBackendDomain();
  157. }
  158. return $this->getCurrentFrontendDomain();
  159. }
  160. /**
  161. * Get Current Backend Domain
  162. *
  163. * @return BaseDomain
  164. */
  165. protected function getCurrentBackendDomain(): ?BaseDomain
  166. {
  167. return null;
  168. // Get Domain by query / session.
  169. if (null !== $id = $this->request->query->get(static::SESSION_KEY_DOMAIN, $this->request->getSession()->get(static::SESSION_KEY_DOMAIN))) {
  170. $this->currentDomain = $this->getDomain($id);
  171. $this->mode = static::MODE_QUERY_SESSION;
  172. // Fallback, try to get default Domain if it's not global.
  173. if ($this->currentDomain === null && (int)$id !== 0) {
  174. $this->currentDomain = $this->getDefaultDomain();
  175. }
  176. }
  177. // Update session.
  178. if ($this->currentDomain !== null) {
  179. $this->request->getSession()->set(static::SESSION_KEY_DOMAIN, $this->currentDomain->getId());
  180. }
  181. else {
  182. $host = $this->request->getHost();
  183. if(strpos($host, 'www') !== false) {
  184. $host = str_replace('www.', '', $host);
  185. }
  186. // Check and switch global domain
  187. if ($this->currentDomain === null && $id !== null && (int)$id === 0) {
  188. $host = 0;
  189. }
  190. // fallback, set current host
  191. $this->request->getSession()->set(static::SESSION_KEY_DOMAIN, $host);
  192. }
  193. // Return.
  194. return $this->currentDomain;
  195. }
  196. /**
  197. * Get Current Frontend Domain
  198. *
  199. * @return BaseDomain
  200. */
  201. protected function getCurrentFrontendDomain(): ?BaseDomain
  202. {
  203. return null;
  204. // Get Domain by host.
  205. if (null !== $domain = $this->getDomain($this->request->getHost())) {
  206. $this->currentDomain = $domain;
  207. }
  208. // Fallback, try to get default Domain.
  209. if ($this->currentDomain === null) {
  210. $this->currentDomain = $this->getDefaultDomain();
  211. }
  212. // Return.
  213. return $this->currentDomain;
  214. }
  215. /**
  216. * Get Mode
  217. *
  218. * @return string
  219. */
  220. public function getMode(): ?string
  221. {
  222. return null;
  223. return $this->mode;
  224. }
  225. /**
  226. * Get Subdomain
  227. *
  228. * @return string|null
  229. */
  230. public function getSubDomain()
  231. {
  232. return;
  233. $schemaHttpHost = $this->request->getSchemeAndHttpHost();
  234. $parseUrl = parse_url($schemaHttpHost);
  235. $array['host'] = explode('.', $parseUrl['host']) ?? array();
  236. if(!empty($array)) {
  237. if(false !== $key = array_search('www', $array['host'])) {
  238. unset($array['host'][$key]);
  239. }
  240. // reset keys
  241. $array['host'] = array_values($array['host']);
  242. return $array['host'][0];
  243. }
  244. return null;
  245. }
  246. }