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
|
<?php
interface IServiceLocator
{
public function getService($pServiceName);
public function setService($pServiceName, $pService);
}
class ServiceLocatorException extends Exception{}
class ServiceLocator implements IServiceLocator
{
private $_services = array();
public function getService ($pServiceName)
{
if (isset($this->_services[strtolower($pServiceName)])) {
return $this->_services[strtolower($pServiceName)];
}
throw new ServiceLocatorException("Service $pServiceName is not configured yet");
}
public function setService ($pServiceName, $pService)
{
$this->_services[strtolower($pServiceName)] = $pService;
return $this;
}
public function __call ($pName, $pParameters)
{
if (strpos(strtolower($pName), 'get') === 0) {
return $this->getService(substr($pName, 3));
}else{
echo $pName;
}
}
}
?> |
Partager