From b6ed2b51c350c0b886b6190e163e2a7f700095a4 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 24 Sep 2019 16:06:29 +0800 Subject: [PATCH] use psr3-bridge. --- composer.json | 3 +- config/lauthz.php | 4 +-- src/EnforcerManager.php | 7 +++- src/Logger.php | 79 ----------------------------------------- tests/LoggerTest.php | 39 -------------------- 5 files changed, 10 insertions(+), 122 deletions(-) delete mode 100644 src/Logger.php delete mode 100644 tests/LoggerTest.php diff --git a/composer.json b/composer.json index d913147..857798b 100755 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "license": "Apache-2.0", "require": { "laravel/framework": "~5.1|~6.0", - "casbin/casbin": "~1.0" + "casbin/casbin": "~1.0", + "casbin/psr3-bridge": "^1.0" }, "require-dev": { "phpunit/phpunit": "~5.7|~6.0|~7.0|~8.0", diff --git a/config/lauthz.php b/config/lauthz.php index b97bd7b..81e5982 100644 --- a/config/lauthz.php +++ b/config/lauthz.php @@ -39,8 +39,8 @@ return [ // changes whether Lauthz will log messages to the Logger. 'enabled' => false, - // Casbin Logger - 'logger' => Lauthz\Logger::class, + // Casbin Logger, Supported: \Psr\Log\LoggerInterface|string + 'logger' => 'log', ], 'cache' => [ diff --git a/src/EnforcerManager.php b/src/EnforcerManager.php index 0f13e54..aafa98b 100755 --- a/src/EnforcerManager.php +++ b/src/EnforcerManager.php @@ -2,6 +2,7 @@ namespace Lauthz; +use Casbin\Bridge\Logger\LoggerBridge; use Casbin\Enforcer; use Casbin\Model\Model; use Casbin\Log\Log; @@ -77,7 +78,11 @@ class EnforcerManager implements Factory } if ($logger = Arr::get($config, 'log.logger')) { - Log::setLogger(new $logger($this->app['log'])); + if (is_string($logger)) { + $logger = $this->app->make($logger); + } + + Log::setLogger(new LoggerBridge($logger)); } $model = new Model(); diff --git a/src/Logger.php b/src/Logger.php deleted file mode 100644 index e7d4cce..0000000 --- a/src/Logger.php +++ /dev/null @@ -1,79 +0,0 @@ -logger = $logger; - } - - /** - * controls whether print the message. - * - * @param bool $enable - */ - public function enableLog($enable) - { - $this->enable = $enable; - } - - /** - * returns if logger is enabled. - * - * @return bool - */ - public function isEnabled() - { - return $this->enable; - } - - /** - * formats using the default formats for its operands and logs the message. - * - * @param mixed ...$v - * - * @return mixed - */ - public function write(...$v) - { - if (!$this->enable) { - return; - } - $content = ''; - foreach ($v as $value) { - if (\is_array($value) || \is_object($value)) { - $value = json_encode($value); - } - $content .= $value; - } - $this->logger->info($content); - } - - /** - * formats according to a format specifier and logs the message. - * - * @param $format - * @param mixed ...$v - * - * @return mixed - */ - public function writef($format, ...$v) - { - if (!$this->enable) { - return; - } - $this->logger->info(sprintf($format, ...$v)); - } -} diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php deleted file mode 100644 index 531f952..0000000 --- a/tests/LoggerTest.php +++ /dev/null @@ -1,39 +0,0 @@ -enableLog(false); - $this->assertFalse($logger->isEnabled()); - - $logger->enableLog(true); - $this->assertTrue($logger->isEnabled()); - - $monolog->shouldReceive('info')->once()->with('foo', []); - $logger->write('foo'); - - $monolog->shouldReceive('info')->once()->with('foo1foo2', []); - $logger->write('foo1', 'foo2'); - - $monolog->shouldReceive('info')->once()->with(json_encode(['foo1', 'foo2']), []); - $logger->write(['foo1', 'foo2']); - - $monolog->shouldReceive('info')->once()->with(sprintf('There are %u million cars in %s.', 2, 'Shanghai'), []); - $logger->writef('There are %u million cars in %s.', 2, 'Shanghai'); - } -}