Skip to content

Commit 5283e51

Browse files
committed
first commit
1 parent 9b1bdb1 commit 5283e51

File tree

8 files changed

+438
-2
lines changed

8 files changed

+438
-2
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/vendor/
2+
3+
composer.lock
4+
5+
.idea/
6+
*.iml
7+
8+
# coverage report
9+
/build

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: php
2+
matrix:
3+
fast_finish: true
4+
include:
5+
- php: 5.6
6+
- php: 7.0
7+
- php: 7.1
8+
- php: 7.2
9+
- php: 7.3
10+
- php: 7.4snapshot
11+
12+
install:
13+
- composer install --prefer-dist --dev --no-interaction
14+
15+
script:
16+
- mkdir -p build/logs
17+
- vendor/bin/phpunit
18+
19+
after_script:
20+
- travis_retry vendor/bin/php-coveralls -v

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,48 @@
1-
# psr3-bridge
2-
This library provides a PSR-3 compliant bridge for Casbin Logger.
1+
# PSR-3 Bridge
2+
3+
[![Build Status](https://travis-ci.org/php-casbin/psr3-bridge.svg?branch=master)](https://travis-ci.org/php-casbin/psr3-bridge)
4+
[![Coverage Status](https://coveralls.io/repos/github/php-casbin/psr3-bridge/badge.svg)](https://coveralls.io/github/php-casbin/psr3-bridge)
5+
[![Latest Stable Version](https://poser.pugx.org/casbin/psr3-bridge/v/stable)](https://packagist.org/packages/casbin/psr3-bridge)
6+
[![Total Downloads](https://poser.pugx.org/casbin/psr3-bridge/downloads)](https://packagist.org/packages/casbin/psr3-bridge)
7+
[![License](https://poser.pugx.org/casbin/psr3-bridge/license)](https://packagist.org/packages/casbin/psr3-bridge)
8+
9+
This library provides a PSR-3 compliant bridge for `PHP-Casbin` Logger.
10+
11+
[Casbin](https://github.com/php-casbin/php-casbin) is a powerful and efficient open-source access control library.
12+
13+
### Installation
14+
15+
Via [Composer](https://getcomposer.org/).
16+
17+
```
18+
composer require casbin/psr3-bridge
19+
```
20+
21+
### Usage
22+
23+
Here is an example of using `Monolog`, `Monolog` implements the PSR-3 interface.
24+
25+
You can use any other library that implements PSR-3 interface.
26+
27+
```php
28+
29+
use Casbin\Bridge\Logger\LoggerBridge;
30+
use Casbin\Log\Log;
31+
use Monolog\Logger;
32+
use Monolog\Handler\StreamHandler;
33+
34+
35+
$log = new Logger('name');
36+
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
37+
38+
39+
Log::setLogger(new LoggerBridge($log));
40+
```
41+
42+
### Getting Help
43+
44+
- [php-casbin](https://github.com/php-casbin/php-casbin)
45+
46+
### License
47+
48+
This project is licensed under the [Apache 2.0 license](LICENSE).

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "casbin/psr3-bridge",
3+
"keywords": [
4+
"casbin",
5+
"psr-3",
6+
"logger",
7+
"rbac",
8+
"permission",
9+
"acl"
10+
],
11+
"description": "This library provides a PSR-3 compliant bridge for Casbin Logger.",
12+
"authors": [
13+
{
14+
"name": "TechLee",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"license": "Apache-2.0",
19+
"require": {
20+
"casbin/casbin": "^1.0",
21+
"psr/log": "^1.1"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "~5.7|~6.0|~7.0",
25+
"mockery/mockery": "^1.2",
26+
"php-coveralls/php-coveralls": "^2.1"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Casbin\\Bridge\\Logger\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Casbin\\Bridge\\Logger\\Tests\\": "tests/"
36+
}
37+
}
38+
}

phpunit.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
<logging>
22+
<log type="coverage-clover" target="build/logs/clover.xml"/>
23+
<log type="coverage-html" target="build/html"/>
24+
</logging>
25+
<php>
26+
<env name="DB_DATABASE" value="casbin"/>
27+
</php>
28+
</phpunit>

src/LoggerBridge.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Casbin\Bridge\Logger;
4+
5+
use Casbin\Log\Logger as LoggerContract;
6+
use Psr\Log\LoggerInterface;
7+
8+
class LoggerBridge implements LoggerContract
9+
{
10+
public $enable = false;
11+
12+
/**
13+
* The log level.
14+
*
15+
* @var mixed
16+
*/
17+
protected $defaultLevel;
18+
19+
/**
20+
* The logger instance.
21+
*
22+
* @var LoggerInterface
23+
*/
24+
protected $logger;
25+
26+
/**
27+
* LoggerBridge constructor.
28+
*
29+
* @param LoggerInterface $logger
30+
* @param string $defaultLevel
31+
*/
32+
public function __construct(LoggerInterface $logger, $defaultLevel = 'info')
33+
{
34+
$this->logger = $logger;
35+
$this->defaultLevel = $defaultLevel;
36+
}
37+
38+
/**
39+
* controls whether print the message.
40+
*
41+
* @param bool $enable
42+
*/
43+
public function enableLog($enable)
44+
{
45+
$this->enable = $enable;
46+
}
47+
48+
/**
49+
* returns if logger is enabled.
50+
*
51+
* @return bool
52+
*/
53+
public function isEnabled()
54+
{
55+
return $this->enable;
56+
}
57+
58+
/**
59+
* formats using the default formats for its operands and logs the message.
60+
*
61+
* @param mixed ...$v
62+
*
63+
* @return mixed
64+
*/
65+
public function write(...$v)
66+
{
67+
if (!$this->enable) {
68+
return;
69+
}
70+
71+
$content = '';
72+
73+
foreach ($v as $value) {
74+
if (\is_array($value) || \is_object($value)) {
75+
$value = json_encode($value);
76+
}
77+
78+
$content .= $value;
79+
}
80+
81+
$this->logger->log($this->defaultLevel, $content);
82+
}
83+
84+
/**
85+
* formats according to a format specifier and logs the message.
86+
*
87+
* @param $format
88+
* @param mixed ...$v
89+
*
90+
* @return mixed
91+
*/
92+
public function writef($format, ...$v)
93+
{
94+
if (!$this->enable) {
95+
return;
96+
}
97+
98+
$this->logger->log($this->defaultLevel, sprintf($format, ...$v));
99+
}
100+
}

tests/LoggerBrdgeTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Casbin\Bridge\Logger\Tests;
4+
5+
use Mockery;
6+
use Casbin\Bridge\Logger\LoggerBridge;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class LoggerTest extends TestCase
10+
{
11+
public function testEnableLogger()
12+
{
13+
$testLog = Mockery::mock(TestLogger::class);
14+
15+
$defaultLevel = 'info';
16+
17+
$logger = new LoggerBridge($testLog, $defaultLevel);
18+
$this->assertFalse($logger->isEnabled());
19+
20+
$logger->enableLog(true);
21+
$this->assertTrue($logger->isEnabled());
22+
23+
$testLog->shouldReceive('log')->once()->with($defaultLevel, 'foo');
24+
$logger->write('foo');
25+
26+
$testLog->shouldReceive('log')->once()->with($defaultLevel, 'foo1foo2');
27+
$logger->write('foo1', 'foo2');
28+
29+
$testLog->shouldReceive('log')->once()->with($defaultLevel, json_encode(['foo1', 'foo2']));
30+
$logger->write(['foo1', 'foo2']);
31+
32+
$testLog->shouldReceive('log')->once()->with($defaultLevel, sprintf('There are %u million cars in %s.', 2, 'Shanghai'));
33+
$logger->writef('There are %u million cars in %s.', 2, 'Shanghai');
34+
35+
$testLog = Mockery::mock(TestLogger::class);
36+
37+
$logger = new LoggerBridge($testLog);
38+
39+
$logger->enableLog(false);
40+
41+
$testLog->shouldNotHaveReceived('log');
42+
$logger->write(['foo1', 'foo2']);
43+
44+
$testLog->shouldNotHaveReceived('log');
45+
$logger->writef('There are %u million cars in %s.', 2, 'Shanghai');
46+
}
47+
48+
public function testDisableLogger()
49+
{
50+
$testLog = Mockery::mock(TestLogger::class);
51+
52+
$defaultLevel = 'info';
53+
54+
$logger = new LoggerBridge($testLog, $defaultLevel);
55+
56+
$logger->enableLog(false);
57+
$this->assertFalse($logger->isEnabled());
58+
59+
$logger->enableLog(false);
60+
61+
$testLog->shouldNotHaveReceived('log');
62+
$logger->write(['foo1', 'foo2']);
63+
64+
$testLog->shouldNotHaveReceived('log');
65+
$logger->writef('There are %u million cars in %s.', 2, 'Shanghai');
66+
}
67+
}

0 commit comments

Comments
 (0)