<?php
/**
* Domain Provider
*
* @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
*/
namespace VisualMedia\TranslationBundle\Service;
use Symfony\Component\HttpFoundation\RequestStack;
use VisualMedia\LisaBundle\Helper\Helper;
use VisualMedia\LisaBundle\Component\ManagerData;
use VisualMedia\LisaBundle\Component\Interfaces\ServiceInterface;
use VisualMedia\TranslationBundle\Manager\BaseDomainManager;
use VisualMedia\TranslationBundle\Entity\BaseDomain;
/**
* Domain Provider
*/
class DomainProvider implements ServiceInterface
{
public const SERVICE_ID = 'visualmedia.lisa.provider.domain';
public const DEFAULT = 'default';
public const SESSION_KEY_DOMAIN = 'domain';
public const DEFAULT_LANGUAGE = 'nl-NL';
public const MODE_GLOBAL = 'global';
public const MODE_DEFAULT = 'default domain';
public const MODE_CONSTRUCTED = 'constructed';
public const MODE_QUERY_SESSION = 'query / session';
/**
* Request
* @var Request
*/
protected $request;
/**
* Manager
* @var BaseDomainManager
*/
protected $manager;
/**
* Domains
* @var array
*/
protected $domains;
/**
* Current Domain
* @var BaseDomain
*/
protected $currentDomain;
/**
* Default Domain
* @var BaseDomain
*/
protected $defaultDomain;
/**
* Mode
* @var string
*/
protected $mode;
/**
* Constructor
*
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack, BaseDomainManager $manager)
{
$this->request = $requestStack->getMasterRequest();
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public static function getServiceId(): string
{
return static::SERVICE_ID;
}
/**
* Get Mapped Domains
*
* @return array
*/
protected function getMappedDomains(): array
{
return [];
if ($this->domains === null) {
$this->domains = array();
foreach ($this->manager->getIndex() as $domain) {
$this->domains[(string)$domain->getId()] = $domain;
$this->domains[(string)$domain->getHost()] = $domain;
if ($domain->getDefault()) {
$this->domains[static::DEFAULT] = $domain;
}
}
}
return $this->domains;
}
/**
* Get Domains
*
* @return array
*/
public function getDomains(): array
{
return [];
$domains = array();
foreach ($this->getMappedDomains() as $key=>$domain) {
if (is_numeric($key)) {
$domains[(int)$key] = $domain;
}
}
return $domains;
}
/**
* Get Domain
*
* @param mixed $key
*
* @return BaseDomain
*/
public function getDomain($key): ?BaseDomain
{
return null;
return $this->getMappedDomains()[$key] ?? null;
}
/**
* Get Default Domain
*
* @return BaseDomain
*/
public function getDefaultDomain(): ?BaseDomain
{
return null;
$this->mode = static::MODE_DEFAULT;
return $this->getMappedDomains()[static::DEFAULT] ?? null;
}
/**
* Get Current Domain
*
* @return BaseDomain
*/
public function getCurrentDomain(): ?BaseDomain
{
return null;
// Cached.
// @TODO Caching current domain causes issue in edit mode
// if ($this->currentDomain !== null) {
// return $this->currentDomain;
// }
// Return default Domain if the request doesn't exist.
if ($this->request === null) {
return $this->currentDomain = $this->getDefaultDomain();
}
// treat as frontend
if($this->request->attributes->get('_route') === 'manage_login') {
return $this->getCurrentFrontendDomain();
}
// Check frontend / backend.
if ($this->request->attributes->get('_firewall_context') === 'security.firewall.map.context.manage') {
return $this->getCurrentBackendDomain();
}
return $this->getCurrentFrontendDomain();
}
/**
* Get Current Backend Domain
*
* @return BaseDomain
*/
protected function getCurrentBackendDomain(): ?BaseDomain
{
return null;
// Get Domain by query / session.
if (null !== $id = $this->request->query->get(static::SESSION_KEY_DOMAIN, $this->request->getSession()->get(static::SESSION_KEY_DOMAIN))) {
$this->currentDomain = $this->getDomain($id);
$this->mode = static::MODE_QUERY_SESSION;
// Fallback, try to get default Domain if it's not global.
if ($this->currentDomain === null && (int)$id !== 0) {
$this->currentDomain = $this->getDefaultDomain();
}
}
// Update session.
if ($this->currentDomain !== null) {
$this->request->getSession()->set(static::SESSION_KEY_DOMAIN, $this->currentDomain->getId());
}
else {
$host = $this->request->getHost();
if(strpos($host, 'www') !== false) {
$host = str_replace('www.', '', $host);
}
// Check and switch global domain
if ($this->currentDomain === null && $id !== null && (int)$id === 0) {
$host = 0;
}
// fallback, set current host
$this->request->getSession()->set(static::SESSION_KEY_DOMAIN, $host);
}
// Return.
return $this->currentDomain;
}
/**
* Get Current Frontend Domain
*
* @return BaseDomain
*/
protected function getCurrentFrontendDomain(): ?BaseDomain
{
return null;
// Get Domain by host.
if (null !== $domain = $this->getDomain($this->request->getHost())) {
$this->currentDomain = $domain;
}
// Fallback, try to get default Domain.
if ($this->currentDomain === null) {
$this->currentDomain = $this->getDefaultDomain();
}
// Return.
return $this->currentDomain;
}
/**
* Get Mode
*
* @return string
*/
public function getMode(): ?string
{
return null;
return $this->mode;
}
/**
* Get Subdomain
*
* @return string|null
*/
public function getSubDomain()
{
return;
$schemaHttpHost = $this->request->getSchemeAndHttpHost();
$parseUrl = parse_url($schemaHttpHost);
$array['host'] = explode('.', $parseUrl['host']) ?? array();
if(!empty($array)) {
if(false !== $key = array_search('www', $array['host'])) {
unset($array['host'][$key]);
}
// reset keys
$array['host'] = array_values($array['host']);
return $array['host'][0];
}
return null;
}
}