Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit f2a4fd1

Browse files
committed
Реализована запись в лог-файл, добавлен runtime cache
1 parent aedf90d commit f2a4fd1

4 files changed

Lines changed: 75 additions & 11 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace GGrach\BitrixDebugger\Cache;
4+
5+
/**
6+
* Description of RuntimeCache
7+
*
8+
* @author ggrachdev
9+
*/
10+
class RuntimeCache {
11+
12+
private static $cacheRegister;
13+
14+
public static function get($key) {
15+
if (self::has($key))
16+
{
17+
return self::getRegister()[$key];
18+
}
19+
20+
return null;
21+
}
22+
23+
public static function has($key) {
24+
return \array_key_exists($key, self::getRegister());
25+
}
26+
27+
public static function set($key, $value) {
28+
self::$cacheRegister[$key] = $value;
29+
}
30+
31+
public static function getRegister() {
32+
return self::$cacheRegister;
33+
}
34+
35+
36+
public static function remove($key) {
37+
if(self::has($key))
38+
{
39+
unset(self::$cacheRegister[$key]);
40+
}
41+
}
42+
43+
public static function removeAll()
44+
{
45+
self::$cacheRegister = [];
46+
}
47+
}

src/BitrixDebugger/Debugger/Debugger.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use GGrach\BitrixDebugger\Contract\ShowModableContract;
88
use GGrach\BitrixDebugger\Validator\ShowModeDebuggerValidator;
99
use GGrach\BitrixDebugger\Debugger\DebuggerShowModable;
10+
use GGrach\BitrixDebugger\Cache\RuntimeCache;
11+
use GGrach\Writer\FileWriter;
1012

1113
/**
1214
* Description of Debugger
@@ -93,11 +95,28 @@ protected function noticeRaw(string $type, $items) {
9395

9496
}
9597

98+
if (empty($items)) {
99+
return;
100+
}
101+
96102
if (ShowModeDebuggerValidator::needWriteInLog($this)) {
97-
$logFile = $this->configuratorDebugger->getLogPath($type);
98-
99-
if ($logFile) {
100-
103+
$pathLogFile = $this->configuratorDebugger->getLogPath($type);
104+
105+
if ($pathLogFile) {
106+
$keyCache = $type . '_log_file_descriptor';
107+
108+
$fileLogResource = null;
109+
110+
if (RuntimeCache::has($keyCache)) {
111+
$fileLogResource = RuntimeCache::get($keyCache);
112+
} else {
113+
$fileLogResource = \fopen($pathLogFile, 'w');
114+
RuntimeCache::set($keyCache, $fileLogResource);
115+
116+
foreach ($items as $item) {
117+
FileWriter::write($item, $fileLogResource);
118+
}
119+
}
101120
}
102121
}
103122
}

src/Writer/Contract/WritableContract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ interface WritableContract {
1212
/**
1313
* Запись данных в ресурс
1414
*
15-
* @param type $data
15+
* @param string $text
1616
* @param resource $resource
1717
* @param string $delimeter
1818
*/
19-
public static function write($data, resource $resource, string $delimeter): bool;
19+
public static function write(string $text, resource $resource, string $delimeter): bool;
2020

2121
/**
2222
* Очистка данных

src/Writer/FileWriter.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ class FileWriter implements WritableContract {
1414
/**
1515
* Запись данных в ресурс
1616
*
17-
* @param type $data
17+
* @param type $text
1818
* @param resource $resource
1919
* @param string $delimeter
2020
*/
21-
public static function write($data, resource $resource, string $delimeter): bool {
22-
$result = false;
23-
24-
return $result;
21+
public static function write(string $text, resource $resource, string $delimeter): bool {
22+
return fwrite($resource, $text.$delimeter) !== false;
2523
}
2624

2725
/**

0 commit comments

Comments
 (0)