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

Commit ab8df51

Browse files
committed
Добавлен фильтратор, методы для фильтрации поступающих данных для дебага, для инициализации дебаг-бара как модуля и через подключение файлов теперь одна точка подключения всех данных
1 parent a157ad2 commit ab8df51

File tree

14 files changed

+299
-112
lines changed

14 files changed

+299
-112
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"autoload": {
1111
"psr-4": {
1212
"GGrach\\BitrixDebugger\\": "src/BitrixDebugger",
13-
"GGrach\\Writer\\": "src/Writer"
13+
"GGrach\\Writer\\": "src/Writer",
14+
"GGrach\\Filtrator\\": "src/Filtrator"
1415
}
1516
},
1617
"require": {}

initializer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* https://github.com/ggrachdev/BitrixDebugger
55
6-
* @version 0.0.4 beta
6+
* @version 0.0.5 beta
77
*
88
* Пример дебага:
99
*
@@ -22,6 +22,9 @@
2222
* include 'BitrixDebugger/initializer.php';
2323
*
2424
*/
25+
26+
const GGRACH_DEBUG_BAR_TYPE_INCLUDE = 'initializer';
27+
2528
if (\php_sapi_name() === 'cli') {
2629
include 'initializers/cli.php';
2730
} else {

initializers/server.php

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,2 @@
11
<?
2-
3-
use Bitrix\Main\Page\Asset;
4-
5-
$ggrachDebuggerRootPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', __DIR__ . '/..');
6-
$ggrachPathLogFolder = \realpath('.' . $ggrachDebuggerRootPath . '/logs');
7-
8-
\Bitrix\Main\Loader::registerAutoLoadClasses(null, [
9-
"\GGrach\BitrixDebugger\Debugger\Debugger" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/Debugger.php",
10-
"\GGrach\BitrixDebugger\Debugger\NoticeDebugger" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/NoticeDebugger.php",
11-
"\GGrach\BitrixDebugger\Debugger\LogFileDebugger" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/LogFileDebugger.php",
12-
"\GGrach\BitrixDebugger\Debugger\ConfigurationDebugger" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/ConfigurationDebugger.php",
13-
"\GGrach\BitrixDebugger\Contract\ShowModableContract" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Contract/ShowModableContract.php",
14-
"\GGrach\BitrixDebugger\Configurator\DebuggerConfigurator" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Configurator/DebuggerConfigurator.php",
15-
"\GGrach\BitrixDebugger\Configurator\DebugBarConfigurator" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Configurator/DebugBarConfigurator.php",
16-
"\GGrach\BitrixDebugger\Cache\RuntimeCache" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Cache/RuntimeCache.php",
17-
"\GGrach\BitrixDebugger\Validator\ShowModeDebuggerValidator" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Validator/ShowModeDebuggerValidator.php",
18-
"\GGrach\BitrixDebugger\Representer\DebugBarRepresenter" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Representer/DebugBarRepresenter.php",
19-
"\GGrach\Writer\FileWriter" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/Writer/FileWriter.php",
20-
"\GGrach\Writer\Contract\WritableContract" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/Writer/Contract/WritableContract.php",
21-
"\GGrach\BitrixDebugger\Events\OnEndBufferContent" => $ggrachDebuggerRootPath . "/module/ggrachdev.debugbar/classes/general/BitrixDebugger/Events/OnEndBufferContent.php"
22-
]);
23-
24-
$ggrachDebuggerConfigurator = new \GGrach\BitrixDebugger\Configurator\DebuggerConfigurator();
25-
$ggrachDebugBarConfigurator = new \GGrach\BitrixDebugger\Configurator\DebugBarConfigurator();
26-
27-
$ggrachDebuggerConfigurator->setLogPath('error', $ggrachPathLogFolder . '/error.log')
28-
->setLogPath('warning', $ggrachPathLogFolder . '/warning.log')
29-
->setLogPath('success', $ggrachPathLogFolder . '/success.log')
30-
->setLogPath('notice', $ggrachPathLogFolder . '/notice.log');
31-
32-
$GLOBALS["DD"] = new \GGrach\BitrixDebugger\Debugger\Debugger($ggrachDebuggerConfigurator, $ggrachDebugBarConfigurator);
33-
34-
/*
35-
* code - отображать дебаг-данные в коде
36-
* debug_bar - отображать дебаг-данные в debug_bar
37-
*/
38-
$GLOBALS["DD"]->getConfiguratorDebugger()->setShowModes(['debug_bar']);
39-
40-
function DD(...$data) {
41-
if (!empty($data)) {
42-
foreach ($data as $item) {
43-
$GLOBALS["DD"]->notice($item);
44-
}
45-
}
46-
47-
return $GLOBALS["DD"];
48-
}
49-
50-
if (\GGrach\BitrixDebugger\Validator\ShowModeDebuggerValidator::needShowInDebugBar(DD()->getConfiguratorDebugger())) {
51-
52-
Asset::getInstance()->addJs(
53-
$ggrachDebuggerRootPath . '/module/ggrachdev.debugbar/install/js/initializer.js'
54-
);
55-
Asset::getInstance()->addCss(
56-
$ggrachDebuggerRootPath . '/module/ggrachdev.debugbar/install/css/general.css'
57-
);
58-
Asset::getInstance()->addCss(
59-
$ggrachDebuggerRootPath . '/module/ggrachdev.debugbar/install/css/' . $ggrachDebugBarConfigurator->getColorTheme() . '/theme.css'
60-
);
61-
62-
include __DIR__ . '/../module/ggrachdev.debugbar/functions.php';
63-
include __DIR__ . '/../module/ggrachdev.debugbar/events.php';
64-
}
2+
include __DIR__ . '/../module/ggrachdev.debugbar/include.php';

module/ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/Debugger.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use GGrach\BitrixDebugger\Configurator\DebuggerConfigurator;
66
use GGrach\BitrixDebugger\Configurator\DebugBarConfigurator;
7+
use GGrach\Filtrator\FiltratorContract;
8+
use GGrach\Filtrator\Filtrator;
79

810
/**
911
* Ответственность: создание полноценного объекта, который позволит осуществлять все возможные операции через текучий интерфейс
@@ -12,7 +14,7 @@
1214
*/
1315
class Debugger extends LogFileDebugger {
1416

15-
public function __construct($debuggerConfigurator = null, $debugBarConfigurator = null) {
17+
public function __construct($debuggerConfigurator = null, $debugBarConfigurator = null, $filtrator = null) {
1618
if ($debuggerConfigurator === null) {
1719
$this->setConfiguratorDebugger(new DebuggerConfigurator());
1820
} elseif ($debuggerConfigurator instanceof DebuggerConfigurator) {
@@ -24,6 +26,12 @@ public function __construct($debuggerConfigurator = null, $debugBarConfigurator
2426
} elseif ($debugBarConfigurator instanceof DebugBarConfigurator) {
2527
$this->setConfiguratorDebugBar($debugBarConfigurator);
2628
}
29+
30+
if ($filtrator === null) {
31+
$this->setFiltrator(new Filtrator());
32+
} elseif ($filtrator instanceof FiltratorContract) {
33+
$this->setFiltrator($filtrator);
34+
}
2735
}
2836

2937
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace GGrach\BitrixDebugger\Debugger;
4+
5+
use GGrach\Filtrator\FiltratorContract;
6+
7+
/**
8+
* Ответственность: фильтрация входящих данных для дебага
9+
*
10+
* @author ggrachdev
11+
*/
12+
class FilterDebugger extends ConfigurationDebugger {
13+
14+
protected FiltratorContract $filtrator;
15+
16+
public function getFiltrator(): FiltratorContract {
17+
return $this->filtrator;
18+
}
19+
20+
public function setFiltrator(FiltratorContract $filtrator): self {
21+
$this->filtrator = $filtrator;
22+
return $this;
23+
}
24+
25+
public function resetFilter(): self {
26+
$this->getFiltrator()->clearFilters();
27+
return $this;
28+
}
29+
30+
public function filtrateItem(array $itemData): array {
31+
return $this->getFiltrator()->filtrate($itemData);
32+
}
33+
34+
public function first(): self {
35+
$this->getFiltrator()->addFilter('first');
36+
return $this;
37+
}
38+
39+
public function last(): self {
40+
$this->getFiltrator()->addFilter('last');
41+
return $this;
42+
}
43+
44+
public function limit(int $limit = 10): self {
45+
$this->getFiltrator()->addFilter('limit', [
46+
'count' => $limit
47+
]);
48+
return $this;
49+
}
50+
51+
}

module/ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/LogFileDebugger.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,27 @@ public function logRaw($type, ...$item) {
4848
}
4949

5050
public function noticeLog(...$item) {
51-
return $this->logRaw('notice', $item);
51+
$this->logRaw('notice', $item);
52+
$this->resetFilter();
53+
return $this;
5254
}
5355

5456
public function errorLog(...$item) {
55-
return $this->logRaw('error', $item);
57+
$this->logRaw('error', $item);
58+
$this->resetFilter();
59+
return $this;
5660
}
5761

5862
public function warningLog(...$item) {
59-
return $this->logRaw('warning', $item);
63+
$this->logRaw('warning', $item);
64+
$this->resetFilter();
65+
return $this;
6066
}
6167

6268
public function successLog(...$item) {
63-
return $this->logRaw('success', $item);
69+
$this->logRaw('success', $item);
70+
$this->resetFilter();
71+
return $this;
6472
}
6573

6674
}

module/ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/NoticeDebugger.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,32 @@
99
*
1010
* @author ggrachdev
1111
*/
12-
class NoticeDebugger extends ConfigurationDebugger {
12+
class NoticeDebugger extends FilterDebugger {
1313

1414
protected $log = [];
1515

1616
public function notice(...$item) {
17-
return $this->noticeRaw('notice', $item);
17+
$this->noticeRaw('notice', $item);
18+
$this->resetFilter();
19+
return $this;
1820
}
1921

2022
public function error(...$item) {
21-
return $this->noticeRaw('error', $item);
23+
$this->noticeRaw('error', $item);
24+
$this->resetFilter();
25+
return $this;
2226
}
2327

2428
public function warning(...$item) {
25-
return $this->noticeRaw('warning', $item);
29+
$this->noticeRaw('warning', $item);
30+
$this->resetFilter();
31+
return $this;
2632
}
2733

2834
public function success(...$item) {
29-
return $this->noticeRaw('success', $item);
35+
$this->noticeRaw('success', $item);
36+
$this->resetFilter();
37+
return $this;
3038
}
3139

3240
/**
@@ -115,14 +123,22 @@ public function getLog(bool $needAddSystemData = false): array {
115123
/**
116124
* Кастомизированное уведомление
117125
*
118-
* @param type $typeNotice
126+
* @param string $typeNotice
119127
* @param type $item
120128
*/
121-
public function debug($typeNotice, ...$item) {
122-
return $this->noticeRaw($typeNotice, $item);
129+
public function debug(string $typeNotice, ...$item) {
130+
$this->noticeRaw($typeNotice, $item);
131+
$this->resetFilter();
132+
return $this;
123133
}
124134

125-
protected function noticeRaw(string $type, $arLogItems) {
135+
protected function noticeRaw(string $type, array $arLogItems) {
136+
137+
if(!empty($arLogItems)) {
138+
foreach ($arLogItems as &$item) {
139+
$item = $this->filtrateItem($item);
140+
}
141+
}
126142

127143
if (ShowModeDebuggerValidator::needShowInDebugBar($this->getConfiguratorDebugger())) {
128144

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace GGrach\Filtrator;
4+
5+
use \GGrach\Filtrator\FiltratorContract;
6+
7+
/**
8+
* Фильтратор данных
9+
*
10+
* @author ggrachdev
11+
*/
12+
class Filtrator implements FiltratorContract {
13+
14+
public const FILTERS_NAME = [
15+
'limit', 'first', 'last'
16+
];
17+
18+
protected array $filters;
19+
20+
public function addFilter(string $filterType, array $filterParams = []): void {
21+
if ($this->hasFilter($filterType)) {
22+
$this->filters[] = [
23+
'type' => $filterType,
24+
'params' => $filterParams
25+
];
26+
}
27+
}
28+
29+
protected function filtrateItem(string $filterType, array $filterParams, array $data): array {
30+
switch ($filterType) {
31+
case 'limit':
32+
if (empty($filterParams['count'])) {
33+
$filterParams['count'] = 10;
34+
}
35+
$data = array_slice($data, 0, $filterParams['count'], true);
36+
break;
37+
case 'first':
38+
if (!empty($data[0])) {
39+
$data = $data[0];
40+
}
41+
break;
42+
case 'last':
43+
if (!empty($data)) {
44+
$data = $data[sizeof($data) - 1];
45+
}
46+
break;
47+
}
48+
49+
return $data;
50+
}
51+
52+
public function clearFilters(): void {
53+
$this->filters = [];
54+
}
55+
56+
public function filtrate(array $data): array {
57+
if (!empty($this->filters)) {
58+
foreach ($this->filters as $arFilter) {
59+
$data = $this->filtrateItem($arFilter['type'], $arFilter['params'], $data);
60+
}
61+
}
62+
63+
return $data;
64+
}
65+
66+
public function hasFilter(string $filterType): bool {
67+
return \in_array($filterType, self::FILTERS_NAME);
68+
}
69+
70+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace GGrach\Filtrator;
4+
5+
/**
6+
*
7+
* @author ggrachdev
8+
*/
9+
interface FiltratorContract {
10+
public function filtrate(array $data): array;
11+
12+
public function filtrateItem(string $filterType,array $data): array;
13+
14+
public function addFilter(string $filterType, array $filterParams): void;
15+
16+
public function hasFilter(string $filterType): bool;
17+
18+
public function clearFilters(): void;
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@startuml
2+
skinparam classAttributeIconSize 0
3+
4+
interface FiltratorContract {
5+
+ array filtrate(array $data)
6+
# array filtrateItem(string $filterType, array $filterParams, array $data)
7+
8+
+ void addFilter(string $filterType, array $filterParams = [])
9+
+ bool hasFilter(string $filterType)
10+
+ void clearFilters()
11+
}
12+
13+
class Filtrator {
14+
# const array FILTERS_NAME
15+
# array $filters
16+
17+
+ array filtrate(array $data)
18+
# array filtrateItem(string $filterType, array $filterParams, array $data)
19+
20+
+ void addFilter(string $filterType, array $filterParams = [])
21+
+ bool hasFilter(string $filterType)
22+
+ void clearFilters()
23+
}
24+
25+
FiltratorContract <|-- Filtrator
26+
@enduml

0 commit comments

Comments
 (0)