1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
<?php
namespace [MonChemin]\Listener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Yaml\Parser;
use Doctrine\Common\Cache\ArrayCache;
class SubdomainListener
{
protected $currentDomain;
protected $pathToAppKernel;
public $Cache;
protected $parameters;
public function __construct(ArrayCache $Cache,$KernelDirectory) {
$this->pathToAppKernel = $KernelDirectory;
}
public function onDomainParse(Event $event)
{
$this->currentDomain=$event->getRequest()->getHost();
}
public function getHostParameter($paramName,$default=null){
if(null===$this->parameters){
$this->loadParameter();
}
return isset($this->parameters['parameters'][$paramName])?$this->parameters['parameters'][$paramName]:$default;
}
public function GetDomain(){
return $this->currentDomain;
}
protected function loadParameter(){
if ($this->Cache->contains($this->currentDomain.'-params')) {
$this->parameters = $this->Cache->fetch($this->currentDomain.'-params');
}else{
//Read Configuration File
$fileName = (file_exists($this->pathToAppKernel.'/config/domain/'.$this->currentDomain.'.yml'))
? $this->pathToAppKernel.'/config/domain/'.$this->currentDomain.'.yml'
: $this->pathToAppKernel.'/config/domain/default.yml';
//Parse yaml Configuration File
$yaml = new Parser();
$this->parameters = $yaml->parse(file_get_contents($fileName));
$this->Cache->save($this->currentDomain.'-params', $this->parameters,3600);
}
}
}
?> |
Partager