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: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177:
<?php
namespace SAREhub\Client;
use Pimple\Container;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use SAREhub\Commons\Logger\LoggerFactory;
use SAREhub\Commons\Logger\NullLoggerFactory;
use SAREhub\Commons\Misc\TimeProvider;
use SAREhub\Commons\Service\Service;
class BasicClientContext extends Container implements ClientContext
{
private $services = [];
private $logger;
private $timeProvider;
private $loggerFactory;
private $started = false;
public function __construct()
{
parent::__construct();
$this->timeProvider = new TimeProvider();
$this->loggerFactory = new NullLoggerFactory();
$this->logger = $this->loggerFactory->create('ClientContext');
}
public static function newInstance()
{
return new self();
}
public function getProperty($name)
{
return $this[$name];
}
public function hasProperty($name)
{
return $this->offsetExists($name);
}
public function setProperty($name, $value)
{
$this[$name] = $value;
return $this;
}
public function addFactory($name, callable $factory)
{
$this->setProperty($name, $this->factory($factory));
return $this;
}
public function addService($name, Service $service)
{
$this->setProperty($name, $service);
$this->services[$name] = $service;
if ($service instanceof ClientContextAware) {
$service->setClientContext($this);
}
$this->injectLogger($name, $service);
$service->start();
return $this;
}
public function now()
{
return $this->getTimeProvider()->now();
}
public function start()
{
$this->started = true;
}
public function tick()
{
foreach ($this->services as $service) {
$service->tick();
}
}
public function stop()
{
foreach ($this->services as $service) {
$service->stop();
}
}
public function isStarted()
{
return $this->started;
}
public function isStopped()
{
return !$this->isStarted();
}
public function isRunning()
{
return $this->isStarted();
}
public function getLogger()
{
return $this->logger;
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function getLoggerFactory()
{
return $this->loggerFactory;
}
public function setLoggerFactory(LoggerFactory $factory)
{
$this->loggerFactory = $factory;
return $this;
}
public function getTimeProvider()
{
return $this->timeProvider;
}
public function setTimeProvider(TimeProvider $provider)
{
$this->timeProvider = $provider;
return $this;
}
public function injectLogger($name, LoggerAwareInterface $aware)
{
$logger = $this->getLoggerFactory()->create($name);
$aware->setLogger($logger);
}
public function registerContextProvider(ContextProvider $provider)
{
$provider->register($this);
return $this;
}
}