* @package generis */ class common_log_Dispatcher implements common_log_Appender { /** * Identifer of the configuration that stores the log configuration * * @var string */ const CONFIG_ID = 'log'; // --- ATTRIBUTES --- /** * Short description of attribute appenders * * @access private * @var array */ private $appenders = []; /** * Short description of attribute minLevel * * @access private * @var int */ private $minLevel = null; /** * Short description of attribute instance * * @access private * @var Dispatcher */ private static $instance = null; // --- OPERATIONS --- /** * Short description of method log * * @access public * @author Joel Bout, * @param Item item * @return mixed */ public function log(common_log_Item $item) { foreach ($this->appenders as $appender) { $appender->log($item); } } /** * Short description of method getLogThreshold * * @access public * @author Joel Bout, * @return int */ public function getLogThreshold() { $returnValue = (int) 0; $returnValue = $this->minLevel; return (int) $returnValue; } /** * Short description of method init * * @access public * @author Joel Bout, * @param array configuration * @return boolean */ public function init($configuration) { $returnValue = (bool) false; $this->appenders = []; $this->minLevel = null; foreach ($configuration as $appenderConfig) { if (isset($appenderConfig['class'])) { $classname = $appenderConfig['class']; if (!class_exists($classname)) { $classname = 'common_log_' . $classname; } if (class_exists($classname) && is_subclass_of($classname, 'common_log_Appender')) { $appender = new $classname(); if (!is_null($appender) && $appender->init($appenderConfig)) { $this->addAppender($appender); } } } } $returnValue = (count($this->appenders) > 0); return (bool) $returnValue; } /** * Short description of method singleton * * @access public * @author Joel Bout, * @return common_log_Dispatcher */ public static function singleton() { $returnValue = null; if (is_null(self::$instance)) { self::$instance = new common_log_Dispatcher(); } $returnValue = self::$instance; return $returnValue; } /** * Short description of method __construct * * @access private * @author Joel Bout, * @return mixed */ public function __construct($config = null) { if (is_null($config) || !is_array($config)) { $config = []; } $this->init($config); } /** * Short description of method addAppender * * @access public * @author Joel Bout, * @param Appender appender * @return mixed */ public function addAppender(common_log_Appender $appender) { $this->appenders[] = $appender; if (is_null($this->minLevel) || $this->minLevel > $appender->getLogThreshold()) { $this->minLevel = $appender->getLogThreshold(); } } }