Have a look at my implementation, using a YAML configuration instead of a database: https://github.com/fourlabsldn/HostsBundle. You might be able to get some inspiration.
<?php
namespace FourLabs\HostsBundle\Service;
use Symfony\Component\HttpFoundation\RequestStack;
use FourLabs\HostsBundle\Model\DomainRepository;
use FourLabs\HostsBundle\Exception\NotConfiguredException;
abstract class AbstractProvider
{
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var DomainRepository
*/
protected $domainRepository;
/**
* @var boolean
*/
protected $requestActive;
public function __construct(RequestStack $requestStack, DomainRepository $domainRepository, $requestActive)
{
$this->requestStack = $requestStack;
$this->domainRepository = $domainRepository;
$this->requestActive = $requestActive;
}
protected function getDomainConfig()
{
$request = $this->requestStack->getCurrentRequest();
if(is_null($request) || !$this->requestActive) {
return;
}
$host = parse_url($request->getUri())['host'];
if(!($domain = $this->domainRepository->findByHost($host))) {
throw new NotConfiguredException('Domain configuration for '.$host.' missing');
}
return $domain;
}
}
and the listener
<?php
namespace FourLabs\HostsBundle\EventListener;
use FourLabs\HostsBundle\Service\LocaleProvider;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class LocaleListener
{
/**
* @var LocaleProvider
*/
private $localeProvider;
public function __construct(LocaleProvider $localeProvider) {
$this->localeProvider = $localeProvider;
}
public function onKernelRequest(GetResponseEvent $event) {
if(HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$event->getRequest()->setLocale($this->localeProvider->getLocale());
}
}