Skip to content

Commit 9911e14

Browse files
committed
Add attribute for outputs
1 parent 067e66c commit 9911e14

13 files changed

+76
-82
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@
350350
PhpCsFixerCustomFixers\Fixer\PhpUnitDedicatedAssertFixer::name() => true,
351351
PhpCsFixerCustomFixers\Fixer\PhpUnitNoUselessReturnFixer::name() => true,
352352
PhpCsFixerCustomFixers\Fixer\PhpdocArrayStyleFixer::name() => true,
353-
PhpCsFixerCustomFixers\Fixer\PhpdocNoIncorrectVarAnnotationFixer::name() => true,
353+
// PhpCsFixerCustomFixers\Fixer\PhpdocNoIncorrectVarAnnotationFixer::name() => true,
354354
PhpCsFixerCustomFixers\Fixer\PhpdocNoSuperfluousParamFixer::name() => true,
355355
// PhpCsFixerCustomFixers\Fixer\PhpdocOnlyAllowedAnnotationsFixer::name() => [
356356
// 'elements' => [

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
"mark-start": "printf '\\n!\\n!\\t\\033[0;36m%s\\033[0m\\n!\\n\\n' \"Started\"",
165165
"md-fix": "@md-lint --fix",
166166
"md-lint": "lint-md --config .lintmdrc ./*.md ./.github/ ./docs/",
167-
"pest": "@php ./vendor/bin/pest -v",
167+
"pest": "@php ./vendor/bin/pest",
168168
"pest-coverage": "@pest --coverage-html=./build/phpunit/ --coverage-clover=./clover.xml --coverage",
169169
"phpstan": "@php ./vendor/bin/phpstan analyse --ansi -v",
170170
"phpstan-baseline": "@phpstan --generate-baseline --allow-empty-baseline",

config/soar.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
// soar 评分输出器
3737
'outputs' => [
3838
// Guanguans\LaravelSoar\Outputs\ClockworkOutput::class,
39-
// Guanguans\LaravelSoar\Outputs\ConsoleOutput::class,
39+
// Guanguans\LaravelSoar\Outputs\ConsoleOutput::class => ['method' => 'warn'],
40+
// Guanguans\LaravelSoar\Outputs\DebugBarOutput::class => ['name' => 'Soar Scores', 'label' => 'warning'],
4041
// Guanguans\LaravelSoar\Outputs\DumpOutput::class => ['exit' => false],
4142
// Guanguans\LaravelSoar\Outputs\ErrorLogOutput::class => ['messageType' => 0, 'destination' => '', 'extraHeaders' => ''],
4243
// Guanguans\LaravelSoar\Outputs\RayOutput::class => ['label' => 'Soar Scores'],
43-
// Guanguans\LaravelSoar\Outputs\SyslogOutput::class,
44+
// Guanguans\LaravelSoar\Outputs\SyslogOutput::class => ['priority' => LOG_WARNING],
4445
Guanguans\LaravelSoar\Outputs\JsonOutput::class => ['key' => 'soar_scores'],
45-
Guanguans\LaravelSoar\Outputs\LogOutput::class => ['channel' => 'daily'],
46-
Guanguans\LaravelSoar\Outputs\DebugBarOutput::class,
47-
Guanguans\LaravelSoar\Outputs\SoarBarOutput::class,
46+
Guanguans\LaravelSoar\Outputs\LogOutput::class => ['channel' => 'daily', 'level' => 'warning'],
47+
Guanguans\LaravelSoar\Outputs\SoarBarOutput::class => ['name' => 'Scores', 'label' => 'warning'],
4848
],
4949

5050
// +----------------------------------------------------------------------+//

psalm-baseline.xml

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,7 @@
6161
<code>$uris</code>
6262
</InvalidReturnStatement>
6363
</file>
64-
<file src="src/Outputs/ClockworkOutput.php">
65-
<UndefinedFunction>
66-
<code>clock()</code>
67-
</UndefinedFunction>
68-
</file>
6964
<file src="src/Outputs/Concerns/OutputCondition.php">
70-
<PossiblyUnusedMethod>
71-
<code>isCommandFinished</code>
72-
</PossiblyUnusedMethod>
7365
<UndefinedPropertyFetch>
7466
<code><![CDATA[$dispatcher->headers]]></code>
7567
</UndefinedPropertyFetch>
@@ -82,15 +74,7 @@
8274
<code><![CDATA[$dispatcher->headers]]></code>
8375
</UndefinedPropertyFetch>
8476
</file>
85-
<file src="src/Outputs/DumpOutput.php">
86-
<PossiblyUnusedMethod>
87-
<code>__construct</code>
88-
</PossiblyUnusedMethod>
89-
</file>
9077
<file src="src/Outputs/JsonOutput.php">
91-
<PossiblyUnusedMethod>
92-
<code>__construct</code>
93-
</PossiblyUnusedMethod>
9478
<UndefinedMethod>
9579
<code>getData</code>
9680
<code>setData</code>
@@ -99,24 +83,11 @@
9983
<code><![CDATA[$dispatcher->headers]]></code>
10084
</UndefinedPropertyFetch>
10185
</file>
102-
<file src="src/Outputs/LogOutput.php">
103-
<PossiblyUnusedMethod>
104-
<code>__construct</code>
105-
</PossiblyUnusedMethod>
106-
</file>
10786
<file src="src/Outputs/RayOutput.php">
10887
<InvalidArgument>
10988
<code>$scores</code>
11089
</InvalidArgument>
11190
</file>
112-
<file src="src/Outputs/SoarBarOutput.php">
113-
<PossiblyUnusedMethod>
114-
<code>__construct</code>
115-
</PossiblyUnusedMethod>
116-
<UndefinedPropertyFetch>
117-
<code><![CDATA[$dispatcher->headers]]></code>
118-
</UndefinedPropertyFetch>
119-
</file>
12091
<file src="src/SoarBar.php">
12192
<PossiblyUnusedMethod>
12293
<code>__construct</code>

psalm.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<directory name="src/Outputs"/>
4040
</errorLevel>
4141
</UnusedClass>
42+
<PossiblyUnusedMethod>
43+
<errorLevel type="suppress">
44+
<directory name="src/Macros"/>
45+
<directory name="src/Outputs"/>
46+
</errorLevel>
47+
</PossiblyUnusedMethod>
4248
</issueHandlers>
4349
<plugins>
4450
<!--<pluginClass class="Psalm\LaravelPlugin\Plugin"/>-->

src/Outputs/ClockworkOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public function output(Collection $scores, $dispatcher): void
2222
return; // @codeCoverageIgnore
2323
}
2424

25-
$scores->each(static fn (array $score) => clock()->warning($score));
25+
clock(...$scores);
2626
}
2727
}

src/Outputs/ConsoleOutput.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
class ConsoleOutput extends Output
1818
{
19+
protected string $method;
20+
21+
public function __construct(string $method = 'warn')
22+
{
23+
$this->method = $method;
24+
}
25+
1926
public function output(Collection $scores, $dispatcher): void
2027
{
2128
if (! $this->isHtmlResponse($dispatcher)) {
@@ -37,8 +44,8 @@ public function output(Collection $scores, $dispatcher): void
3744
protected function toJs(Collection $scores): string
3845
{
3946
$js = $scores
40-
->map(static fn ($score): string => sprintf(
41-
'console.warn(`%s`);',
47+
->map(fn ($score): string => sprintf(
48+
"console.{$this->method}(`%s`);",
4249
str_replace('`', '\`', to_pretty_json($score))
4350
))
4451
->join(PHP_EOL);

src/Outputs/DebugBarOutput.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818

1919
class DebugBarOutput extends Output
2020
{
21-
protected static ?MessagesCollector $messagesCollector = null;
21+
protected string $name;
22+
protected string $label;
2223

2324
private static bool $outputted = false;
2425

26+
public function __construct(string $name = 'Soar Scores', string $label = 'warning')
27+
{
28+
$this->name = $name;
29+
$this->label = $label;
30+
}
31+
2532
/**
2633
* {@inheritDoc}
2734
*
@@ -33,10 +40,15 @@ public function output(Collection $scores, $dispatcher): void
3340
return;
3441
}
3542

43+
$laravelDebugbar = app(LaravelDebugbar::class);
44+
if (! $laravelDebugbar->hasCollector($this->name)) {
45+
$laravelDebugbar->addCollector(new MessagesCollector($this->name));
46+
}
47+
3648
$scores
37-
->each(fn (array $score) => $this->getMessagesCollector()->addMessage(
49+
->each(fn (array $score) => $laravelDebugbar[$this->name]->addMessage(
3850
$score['Summary'].PHP_EOL.to_pretty_json($score),
39-
'warning',
51+
$this->label,
4052
false
4153
))
4254
->tap(static fn (): bool => self::$outputted = true);
@@ -50,20 +62,8 @@ public static function isOutputted(): bool
5062
protected function shouldOutput($dispatcher): bool
5163
{
5264
// app(LaravelDebugbar::class)->isEnabled()
53-
return $this->isHtmlResponse($dispatcher) && class_exists(LaravelDebugbar::class);
54-
}
55-
56-
protected function getMessagesCollector(): MessagesCollector
57-
{
58-
if (self::$messagesCollector instanceof MessagesCollector) {
59-
return self::$messagesCollector;
60-
}
61-
62-
$messagesCollector = new MessagesCollector('Soar Scores');
63-
if (! app(LaravelDebugbar::class)->hasCollector($messagesCollector->getName())) {
64-
app(LaravelDebugbar::class)->addCollector($messagesCollector);
65-
}
66-
67-
return self::$messagesCollector = $messagesCollector;
65+
return app()->has(LaravelDebugbar::class)
66+
&& class_exists(LaravelDebugbar::class)
67+
&& $this->isHtmlResponse($dispatcher);
6868
}
6969
}

src/Outputs/DumpOutput.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public function __construct(bool $exit = false)
3333
public function output(Collection $scores, $dispatcher): void
3434
{
3535
$scores
36-
->each(static function (array $score): void {
37-
dump($score);
38-
})
36+
->each(static fn (array $score) => dump($score))
3937
->tap(function (): void {
4038
if ($this->exit) {
4139
exit(1); // @codeCoverageIgnore

src/Outputs/LogOutput.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
class LogOutput extends Output
1919
{
2020
protected string $channel;
21+
protected string $level;
2122

22-
public function __construct(string $channel = 'daily')
23+
public function __construct(string $channel = 'daily', string $level = 'warning')
2324
{
2425
$this->channel = $channel;
26+
$this->level = $level;
2527
}
2628

2729
/**
@@ -31,7 +33,8 @@ public function __construct(string $channel = 'daily')
3133
*/
3234
public function output(Collection $scores, $dispatcher): void
3335
{
34-
$scores->each(fn (array $score) => Log::channel($this->channel)->warning(
36+
$scores->each(fn (array $score) => Log::channel($this->channel)->log(
37+
$this->level,
3538
$score['Summary'].PHP_EOL.to_pretty_json($score)
3639
));
3740
}

src/Outputs/SoarBarOutput.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212

1313
namespace Guanguans\LaravelSoar\Outputs;
1414

15-
use DebugBar\JavascriptRenderer;
15+
use DebugBar\DataCollector\MessagesCollector;
1616
use Guanguans\LaravelSoar\SoarBar;
1717
use Illuminate\Support\Collection;
1818

1919
class SoarBarOutput extends Output
2020
{
21-
private SoarBar $soarBar;
21+
protected string $name;
22+
protected string $label;
2223

23-
private JavascriptRenderer $javascriptRenderer;
24-
25-
public function __construct(SoarBar $soarBar)
24+
public function __construct(string $name = 'Scores', string $label = 'warning')
2625
{
27-
$this->soarBar = $soarBar;
28-
$this->javascriptRenderer = $soarBar->getJavascriptRenderer();
26+
$this->name = $name;
27+
$this->label = $label;
2928
}
3029

3130
/**
@@ -39,24 +38,27 @@ public function output(Collection $scores, $dispatcher): void
3938
return;
4039
}
4140

42-
$scores->each(fn (array $score) => $this->soarBar['scores']->addMessage(
41+
$soarBar = app(SoarBar::class);
42+
if (! $soarBar->hasCollector($this->name)) {
43+
$soarBar->addCollector(new MessagesCollector($this->name));
44+
}
45+
46+
$scores->each(fn (array $score) => $soarBar[$this->name]->addMessage(
4347
$score['Summary'].PHP_EOL.to_pretty_json($score),
44-
'warning',
48+
$this->label,
4549
false
4650
));
4751

52+
/** @var \Symfony\Component\HttpFoundation\Response $dispatcher */
4853
$content = $dispatcher->getContent();
49-
$head = $this->javascriptRenderer->renderHead();
50-
$widget = $this->javascriptRenderer->render();
54+
$head = $soarBar->getJavascriptRenderer()->renderHead();
55+
$widget = $soarBar->getJavascriptRenderer()->render();
5156

5257
// Try to put the js/css directly before the </head>
5358
$pos = strripos($content, '</head>');
54-
if (false !== $pos) {
55-
$content = substr($content, 0, $pos).$head.substr($content, $pos); // @codeCoverageIgnore
56-
} else {
57-
// Append the head before the widget
58-
$widget = $head.$widget;
59-
}
59+
false !== $pos
60+
? $content = substr($content, 0, $pos).$head.substr($content, $pos) // @codeCoverageIgnore
61+
: $widget = $head.$widget; // Append the head before the widget
6062

6163
// Try to put the widget at the end, directly before the </body>
6264
$pos = strripos($content, '</body>');

src/Outputs/SyslogOutput.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616

1717
class SyslogOutput extends Output
1818
{
19+
protected int $priority;
20+
21+
public function __construct(int $priority = LOG_WARNING)
22+
{
23+
$this->priority = $priority;
24+
}
25+
1926
/**
2027
* {@inheritDoc}
2128
*
2229
* @throws \JsonException
2330
*/
2431
public function output(Collection $scores, $dispatcher): void
2532
{
26-
$scores->each(fn (array $score) => syslog(LOG_WARNING, $score['Summary'].PHP_EOL.to_pretty_json($score)));
33+
$scores->each(fn (array $score) => syslog($this->priority, $score['Summary'].PHP_EOL.to_pretty_json($score)));
2734
}
2835
}

src/SoarBar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class SoarBar extends DebugBar
2424
*/
2525
public function __construct()
2626
{
27-
$this->addCollector(new MessagesCollector('scores'))
28-
->addCollector(new MemoryCollector())
27+
$this->addCollector(new MemoryCollector())
2928
->addCollector(new PhpInfoCollector())
29+
// ->addCollector(new MessagesCollector('scores'))
3030
->jsRenderer = new JavascriptRenderer($this);
3131
}
3232
}

0 commit comments

Comments
 (0)