From c5c192191411e734fbbb5a4327455b99e29798eb Mon Sep 17 00:00:00 2001 From: Arunas Mazeika Date: Wed, 21 Mar 2018 11:13:43 +0100 Subject: [PATCH 1/2] #15 Add enabled state Allows disabling and enabling loggers for a given controller implementing the loggable --- controller/behavior/loggable.php | 53 ++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/controller/behavior/loggable.php b/controller/behavior/loggable.php index 6e25a6f..30ba477 100644 --- a/controller/behavior/loggable.php +++ b/controller/behavior/loggable.php @@ -17,6 +17,13 @@ */ class ComActivitiesControllerBehaviorLoggable extends KControllerBehaviorAbstract { + /** + * Behavior enabled state + * + * @var bool + */ + protected $_enabled; + /** * Logger queue. * @@ -36,6 +43,8 @@ public function __construct(KObjectConfig $config) // Create the logger queue $this->__queue = $this->getObject('lib:object.queue'); + $this->_enabled = $config->enabled; + // Attach the loggers $loggers = KObjectConfig::unbox($config->loggers); @@ -59,6 +68,7 @@ public function __construct(KObjectConfig $config) protected function _initialize(KObjectConfig $config) { $config->append(array( + 'enabled' => true, 'priority' => self::PRIORITY_LOWEST, 'loggers' => array(), )); @@ -80,23 +90,48 @@ protected function _initialize(KObjectConfig $config) */ final public function execute(KCommandInterface $command, KCommandChainInterface $chain) { - $action = $command->getName(); - - foreach($this->__queue as $logger) + if ($this->_enabled) { - if (in_array($action, $logger->getActions())) - { - $object = $logger->getActivityObject($command); + $action = $command->getName(); - if ($object instanceof KModelEntityInterface) + foreach($this->__queue as $logger) + { + if (in_array($action, $logger->getActions())) { - $subject = $logger->getActivitySubject($command); - $logger->log($action, $object, $subject); + $object = $logger->getActivityObject($command); + + if ($object instanceof KModelEntityInterface) + { + $subject = $logger->getActivitySubject($command); + $logger->log($action, $object, $subject); + } } } } } + /** + * Enables loggers + * + * @return ComActivitiesControllerBehaviorLoggable + */ + public function enableLoggers() + { + $this->_enabled = true; + return $this; + } + + /** + * Disables loggers + * + * @return ComActivitiesControllerBehaviorLoggable + */ + public function disableLoggers() + { + $this->_enabled = false; + return $this; + } + /** * Attach a logger. * From c9bdcb43f25828f32ea4c98cacb0a44dcc3d768f Mon Sep 17 00:00:00 2001 From: Arunas Mazeika Date: Wed, 21 Mar 2018 12:01:52 +0100 Subject: [PATCH 2/2] #15 Change method names --- controller/behavior/loggable.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/controller/behavior/loggable.php b/controller/behavior/loggable.php index 30ba477..8a1e402 100644 --- a/controller/behavior/loggable.php +++ b/controller/behavior/loggable.php @@ -111,22 +111,22 @@ final public function execute(KCommandInterface $command, KCommandChainInterface } /** - * Enables loggers + * Enables logging * * @return ComActivitiesControllerBehaviorLoggable */ - public function enableLoggers() + public function enableLogging() { $this->_enabled = true; return $this; } /** - * Disables loggers + * Disables logging * * @return ComActivitiesControllerBehaviorLoggable */ - public function disableLoggers() + public function disableLogging() { $this->_enabled = false; return $this;