From 9b01ca275f891c245c2e840ed93c87f715c04e70 Mon Sep 17 00:00:00 2001 From: Dmitry Dorogin Date: Sat, 2 Sep 2017 00:49:53 +0300 Subject: [PATCH] Fixes #14273: yii\log\Target::$enabled now supports callable value (#14539) * Fixes #14273: yii\log\Target:: now supports callable value * added empty line in changelog [skip ci] * getter and setter * Update Target.php --- framework/CHANGELOG.md | 1 + framework/log/Target.php | 31 ++++++++++++++++++++++++++----- tests/framework/log/TargetTest.php | 17 +++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 19b169a..c2edd9c 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.13 under development ------------------------ +- Enh #14273: `yii\log\Target::$enabled` now supports callable value (dmirogin) - Bug #14723: Fixed serialization of `yii\db\Connection` instance closes database connection (klimov-paul) - Bug #14697: Fixed `console\widgets\Table` rendering when there's no data supplied (bscheshirwork) - Enh #13853: Added `yii\db\Migration::$compact` as well as `yii\console\controllers\BaseMigrateController::$compact` to allow making the migration console output more compact (francislavoie) diff --git a/framework/log/Target.php b/framework/log/Target.php index e83e86f..1488c4d 100644 --- a/framework/log/Target.php +++ b/framework/log/Target.php @@ -28,6 +28,7 @@ use yii\web\Request; * @property int $levels The message levels that this target is interested in. This is a bitmap of level * values. Defaults to 0, meaning all available levels. Note that the type of this property differs in getter * and setter. See [[getLevels()]] and [[setLevels()]] for details. + * @property bool $enabled Whether to enable this log target. Defaults to true. * * For more details and usage information on Target, see the [guide article on logging & targets](guide:runtime-logging). * @@ -37,10 +38,6 @@ use yii\web\Request; abstract class Target extends Component { /** - * @var bool whether to enable this log target. Defaults to true. - */ - public $enabled = true; - /** * @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories. * You can use an asterisk at the end of a category so that the category may be used to * match those categories sharing the same common prefix. For example, 'yii\db\*' will match @@ -94,7 +91,7 @@ abstract class Target extends Component public $messages = []; private $_levels = 0; - + private $_enabled = true; /** * Exports log [[messages]] to a specific destination. @@ -311,4 +308,28 @@ abstract class Target extends Component return "[$ip][$userID][$sessionID]"; } + + /** + * Sets a value indicating whether this log target is enabled. + * @param bool|callable $value a boolean value or a callable to obtain the value from. + * The callable value is available since version 2.0.13. + */ + public function setEnabled($value) + { + $this->_enabled = $value; + } + + /** + * Check whether the log target is enabled. + * @property Indicates whether this log target is enabled. Defaults to true. + * @return bool A value indicating whether this log target is enabled. + */ + public function getEnabled() + { + if (is_callable($this->_enabled)) { + return call_user_func($this->_enabled, $this); + } + + return $this->_enabled; + } } diff --git a/tests/framework/log/TargetTest.php b/tests/framework/log/TargetTest.php index ff7b258..55ccd78 100644 --- a/tests/framework/log/TargetTest.php +++ b/tests/framework/log/TargetTest.php @@ -165,6 +165,23 @@ class TargetTest extends TestCase $this->expectExceptionMessage('Incorrect 128 value'); $target->setLevels(128); } + + public function testGetEnabled() + { + /** @var Target $target */ + $target = $this->getMockForAbstractClass('yii\\log\\Target'); + + $target->enabled = true; + $this->assertTrue($target->enabled); + + $target->enabled = false; + $this->assertFalse($target->enabled); + + $target->enabled = function ($target) { + return empty($target->messages); + }; + $this->assertTrue($target->enabled); + } } class TestTarget extends Target