Skip to content

Commit 5026d32

Browse files
committed
Resolved #5, Add PSR-3 Adapter
1 parent 59816fd commit 5026d32

10 files changed

Lines changed: 258 additions & 16 deletions

File tree

README.md

Whitespace-only changes.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
},
3030
"require": {
3131
"php": ">=7.0",
32-
"ext-json": "*"
32+
"ext-json": "*",
33+
"psr/log": "^1.0"
3334
},
3435
"require-dev": {
3536
"dframe/database": "^1.4"
@@ -40,4 +41,4 @@
4041
],
4142
"phpunit": "php vendor/bin/phpunit"
4243
}
43-
}
44+
}

composer.lock

Lines changed: 54 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/app/Drivers/PSR3FileLog.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Dframe\ActivityLog\Demo\Drivers;
4+
5+
define('APP_DIR', __DIR__ . '/../');
6+
7+
/**
8+
* Dframe/ActivityLog
9+
* Copyright (c) Sławomir Kaleta
10+
*
11+
* @license https://github.com/dusta/activityLog/blob/master/LICENCE
12+
*/
13+
class PSR3FileLog extends \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface, \Dframe\ActivityLog\Driver
14+
{
15+
const FILE_DB = APP_DIR . 'Logs' . DIRECTORY_SEPARATOR . 'db.txt';
16+
17+
public function push($loggedId, $on, $entity, $log)
18+
{
19+
$data = [
20+
'level' => $entity['data']['level'],
21+
'data' => $entity['data']['message'],
22+
'context' => $entity['data']['context']
23+
];
24+
25+
$fp = fopen(self::FILE_DB, 'rw+');
26+
if (!flock($fp, LOCK_EX)) {
27+
return false;
28+
}
29+
30+
if (filesize(self::FILE_DB) > 0) {
31+
$contents = null;
32+
while (!feof($fp)) {
33+
$contents .= fread($fp, 8192);
34+
}
35+
36+
$jsondecode = json_decode($contents, true);
37+
$jsondecode[] = $data;
38+
39+
flock($fp, LOCK_UN);
40+
fclose($fp);
41+
42+
$fp = fopen(self::FILE_DB, 'w+');
43+
$result = fwrite($fp, json_encode($jsondecode));
44+
} else {
45+
$result = fwrite($fp, json_encode($data));
46+
}
47+
48+
flock($fp, LOCK_UN);
49+
fclose($fp);
50+
51+
52+
return $result !== false;
53+
}
54+
55+
/**
56+
* Logs with an arbitrary level.
57+
*
58+
* @param mixed $level
59+
* @param string $message
60+
* @param array $context
61+
*
62+
* @return void
63+
*/
64+
public function log($level, $message, array $context = [])
65+
{
66+
$this->push(null, null, null, $message);
67+
}
68+
69+
/**
70+
* @param $start
71+
* @param $limit
72+
* @param $whereArray
73+
* @param string $order
74+
* @param string $sort
75+
*
76+
* @return mixed
77+
*/
78+
public function logs($start, $limit, $where, $order, $sort)
79+
{
80+
if (!file_exists(self::FILE_DB)) {
81+
return false;
82+
}
83+
84+
$fp = fopen(self::FILE_DB, 'r');
85+
86+
$contents = null;
87+
while (!feof($fp)) {
88+
$contents .= fread($fp, 8192);
89+
}
90+
91+
fclose($fp);
92+
93+
$data = json_decode($contents, true);
94+
return ['return' => true, 'data' => $data];
95+
}
96+
}

demo/app/Entity/Action.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,4 @@ public function __construct()
1717
{
1818
return $this;
1919
}
20-
21-
public function build($message)
22-
{
23-
return ['message' => $message];
24-
}
2520
}

demo/app/Logs/db.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
{"loggedId":"1","on":"","entity":{"entity":"Dframe\\ActivityLog\\Demo\\Entity\\Action","data":{"message":"Hello Word!"}},"log":"system","0":{"loggedId":"1","on":"","entity":{"entity":"Dframe\\ActivityLog\\Demo\\Entity\\Action","data":{"message":"Hello Word!"}},"log":"system"},"1":{"loggedId":"1","on":"","entity":{"entity":"Dframe\\ActivityLog\\Demo\\Entity\\Action","data":{"message":"Hello Word!"}},"log":"system"},"2":{"loggedId":"1","on":"","entity":{"entity":"Dframe\\ActivityLog\\Demo\\Entity\\Action","data":{"message":"Hello Word!"}},"log":"system"}}

demo/web/index.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
*/
99

1010
use Dframe\ActivityLog\Activity;
11+
use Dframe\ActivityLog\Demo\Drivers\FileLog;
1112

1213
require_once __DIR__ . '/../../vendor/autoload.php';
1314

14-
$log = (new Activity(new \Dframe\ActivityLog\Demo\Drivers\FileLog()));
15-
$log->log('system')->entity(\Dframe\ActivityLog\Demo\Entity\Action::class, ['Hello Word!'])->push();
15+
$log = (new Activity(new FileLog()));
16+
$log->log('Hello Word!')->entity(\Dframe\ActivityLog\Demo\Entity\Action::class)->push();
1617

1718
echo '<pre>';
18-
var_dump($log->logs(null, null, null, null, null));
19+
var_dump($log->logs());

src/Activity.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ public function loggedId($loggedId)
6363
public function entity($entity, $arg = null)
6464
{
6565
$class = new \ReflectionClass($entity);
66-
$this->entity = call_user_func_array([new $entity, "build"], $arg);
66+
if (!is_null($arg)) {
67+
$this->entity = call_user_func_array([new $entity, "build"], $arg);
68+
} else {
69+
$this->entity = new $entity;
70+
}
6771
$this->entityType = $class->getName();
6872
return $this;
6973
}
@@ -133,8 +137,9 @@ public function logs($where = [], $order = 'id', $sort = 'DESC', $limit = 30, $s
133137
foreach ($logs['data'] as $key => $value) {
134138
if (isset($value['log_type'])) {
135139
$explode = explode(".", $value['log_type']);
136-
$table = $explode[0];
140+
137141
$entity = new $value['log_entity'];
142+
$table = $explode[0];
138143
$columns = $entity->interpreter($explode[0]);
139144

140145
$logs['data'][$key]['table'] = $this->driver->readTypes($table, $columns, [$explode['1'] => $value['changed_id']])['data'];

src/Entity/PSR3.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Dframe\ActivityLog\Entity;
4+
5+
/**
6+
* Dframe/ActivityLog
7+
* Copyright (c) Sławomir Kaleta
8+
*
9+
* @license https://github.com/dusta/activityLog/blob/master/LICENCE
10+
*/
11+
class PSR3
12+
{
13+
/**
14+
* Action constructor.
15+
*/
16+
public function __construct()
17+
{
18+
return $this;
19+
}
20+
21+
public function build($level, $message, array $context = [])
22+
{
23+
return [
24+
'level' => $level,
25+
'message' => $this->interpolate($message, $context),
26+
'context' => $context
27+
];
28+
29+
}
30+
31+
public function interpolate($message, array $context = [])
32+
{
33+
// build a replacement array with braces around the context keys
34+
$replace = [];
35+
foreach ($context as $key => $val) {
36+
// check that the value can be casted to string
37+
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
38+
$replace['{' . $key . '}'] = $val;
39+
}
40+
}
41+
42+
// interpolate replacement values into the message and return
43+
return strtr($message, $replace);
44+
}
45+
}

src/Helper/Psr3Adapter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Dframe\ActivityLog\Helper;
4+
5+
/**
6+
* Class Psr16Adapter
7+
*
8+
* @package phpFastCache\Helper
9+
*/
10+
class Psr3Adapter extends \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface
11+
{
12+
13+
/**
14+
* Psr3Adapter constructor.
15+
*
16+
* @param \Dframe\ActivityLog\Activity $log
17+
* @param $communique
18+
* @param $entity
19+
*/
20+
public function __construct(\Dframe\ActivityLog\Activity $log, $communique, $entity)
21+
{
22+
$this->log = $log;
23+
$this->communique = $communique;
24+
$this->entity = $entity;
25+
26+
}
27+
28+
/**
29+
* Logs with an arbitrary level.
30+
*
31+
* @param mixed $level
32+
* @param string $message
33+
* @param array $context
34+
*
35+
* @return void
36+
*/
37+
function log($level, $message, array $context = [])
38+
{
39+
40+
$this->log->log($this->communique)->entity($this->entity, [
41+
'level' => $level,
42+
'message' => $message,
43+
'context' => $context
44+
])->push();
45+
46+
}
47+
48+
49+
}

0 commit comments

Comments
 (0)