Skip to content

Commit

Permalink
Add attribute for outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jun 16, 2023
1 parent 067e66c commit 9911e14
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@
PhpCsFixerCustomFixers\Fixer\PhpUnitDedicatedAssertFixer::name() => true,
PhpCsFixerCustomFixers\Fixer\PhpUnitNoUselessReturnFixer::name() => true,
PhpCsFixerCustomFixers\Fixer\PhpdocArrayStyleFixer::name() => true,
PhpCsFixerCustomFixers\Fixer\PhpdocNoIncorrectVarAnnotationFixer::name() => true,
// PhpCsFixerCustomFixers\Fixer\PhpdocNoIncorrectVarAnnotationFixer::name() => true,
PhpCsFixerCustomFixers\Fixer\PhpdocNoSuperfluousParamFixer::name() => true,
// PhpCsFixerCustomFixers\Fixer\PhpdocOnlyAllowedAnnotationsFixer::name() => [
// 'elements' => [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"mark-start": "printf '\\n!\\n!\\t\\033[0;36m%s\\033[0m\\n!\\n\\n' \"Started\"",
"md-fix": "@md-lint --fix",
"md-lint": "lint-md --config .lintmdrc ./*.md ./.github/ ./docs/",
"pest": "@php ./vendor/bin/pest -v",
"pest": "@php ./vendor/bin/pest",
"pest-coverage": "@pest --coverage-html=./build/phpunit/ --coverage-clover=./clover.xml --coverage",
"phpstan": "@php ./vendor/bin/phpstan analyse --ansi -v",
"phpstan-baseline": "@phpstan --generate-baseline --allow-empty-baseline",
Expand Down
10 changes: 5 additions & 5 deletions config/soar.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
// soar 评分输出器
'outputs' => [
// Guanguans\LaravelSoar\Outputs\ClockworkOutput::class,
// Guanguans\LaravelSoar\Outputs\ConsoleOutput::class,
// Guanguans\LaravelSoar\Outputs\ConsoleOutput::class => ['method' => 'warn'],
// Guanguans\LaravelSoar\Outputs\DebugBarOutput::class => ['name' => 'Soar Scores', 'label' => 'warning'],
// Guanguans\LaravelSoar\Outputs\DumpOutput::class => ['exit' => false],
// Guanguans\LaravelSoar\Outputs\ErrorLogOutput::class => ['messageType' => 0, 'destination' => '', 'extraHeaders' => ''],
// Guanguans\LaravelSoar\Outputs\RayOutput::class => ['label' => 'Soar Scores'],
// Guanguans\LaravelSoar\Outputs\SyslogOutput::class,
// Guanguans\LaravelSoar\Outputs\SyslogOutput::class => ['priority' => LOG_WARNING],
Guanguans\LaravelSoar\Outputs\JsonOutput::class => ['key' => 'soar_scores'],
Guanguans\LaravelSoar\Outputs\LogOutput::class => ['channel' => 'daily'],
Guanguans\LaravelSoar\Outputs\DebugBarOutput::class,
Guanguans\LaravelSoar\Outputs\SoarBarOutput::class,
Guanguans\LaravelSoar\Outputs\LogOutput::class => ['channel' => 'daily', 'level' => 'warning'],
Guanguans\LaravelSoar\Outputs\SoarBarOutput::class => ['name' => 'Scores', 'label' => 'warning'],
],

// +----------------------------------------------------------------------+//
Expand Down
29 changes: 0 additions & 29 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,7 @@
<code>$uris</code>
</InvalidReturnStatement>
</file>
<file src="src/Outputs/ClockworkOutput.php">
<UndefinedFunction>
<code>clock()</code>
</UndefinedFunction>
</file>
<file src="src/Outputs/Concerns/OutputCondition.php">
<PossiblyUnusedMethod>
<code>isCommandFinished</code>
</PossiblyUnusedMethod>
<UndefinedPropertyFetch>
<code><![CDATA[$dispatcher->headers]]></code>
</UndefinedPropertyFetch>
Expand All @@ -82,15 +74,7 @@
<code><![CDATA[$dispatcher->headers]]></code>
</UndefinedPropertyFetch>
</file>
<file src="src/Outputs/DumpOutput.php">
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Outputs/JsonOutput.php">
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
<UndefinedMethod>
<code>getData</code>
<code>setData</code>
Expand All @@ -99,24 +83,11 @@
<code><![CDATA[$dispatcher->headers]]></code>
</UndefinedPropertyFetch>
</file>
<file src="src/Outputs/LogOutput.php">
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Outputs/RayOutput.php">
<InvalidArgument>
<code>$scores</code>
</InvalidArgument>
</file>
<file src="src/Outputs/SoarBarOutput.php">
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
<UndefinedPropertyFetch>
<code><![CDATA[$dispatcher->headers]]></code>
</UndefinedPropertyFetch>
</file>
<file src="src/SoarBar.php">
<PossiblyUnusedMethod>
<code>__construct</code>
Expand Down
6 changes: 6 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<directory name="src/Outputs"/>
</errorLevel>
</UnusedClass>
<PossiblyUnusedMethod>
<errorLevel type="suppress">
<directory name="src/Macros"/>
<directory name="src/Outputs"/>
</errorLevel>
</PossiblyUnusedMethod>
</issueHandlers>
<plugins>
<!--<pluginClass class="Psalm\LaravelPlugin\Plugin"/>-->
Expand Down
2 changes: 1 addition & 1 deletion src/Outputs/ClockworkOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function output(Collection $scores, $dispatcher): void
return; // @codeCoverageIgnore
}

$scores->each(static fn (array $score) => clock()->warning($score));
clock(...$scores);
}
}
11 changes: 9 additions & 2 deletions src/Outputs/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

class ConsoleOutput extends Output
{
protected string $method;

public function __construct(string $method = 'warn')
{
$this->method = $method;
}

public function output(Collection $scores, $dispatcher): void
{
if (! $this->isHtmlResponse($dispatcher)) {
Expand All @@ -37,8 +44,8 @@ public function output(Collection $scores, $dispatcher): void
protected function toJs(Collection $scores): string
{
$js = $scores
->map(static fn ($score): string => sprintf(
'console.warn(`%s`);',
->map(fn ($score): string => sprintf(
"console.{$this->method}(`%s`);",
str_replace('`', '\`', to_pretty_json($score))
))
->join(PHP_EOL);
Expand Down
36 changes: 18 additions & 18 deletions src/Outputs/DebugBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@

class DebugBarOutput extends Output
{
protected static ?MessagesCollector $messagesCollector = null;
protected string $name;
protected string $label;

private static bool $outputted = false;

public function __construct(string $name = 'Soar Scores', string $label = 'warning')
{
$this->name = $name;
$this->label = $label;
}

/**
* {@inheritDoc}
*
Expand All @@ -33,10 +40,15 @@ public function output(Collection $scores, $dispatcher): void
return;
}

$laravelDebugbar = app(LaravelDebugbar::class);
if (! $laravelDebugbar->hasCollector($this->name)) {
$laravelDebugbar->addCollector(new MessagesCollector($this->name));
}

$scores
->each(fn (array $score) => $this->getMessagesCollector()->addMessage(
->each(fn (array $score) => $laravelDebugbar[$this->name]->addMessage(
$score['Summary'].PHP_EOL.to_pretty_json($score),
'warning',
$this->label,
false
))
->tap(static fn (): bool => self::$outputted = true);
Expand All @@ -50,20 +62,8 @@ public static function isOutputted(): bool
protected function shouldOutput($dispatcher): bool
{
// app(LaravelDebugbar::class)->isEnabled()
return $this->isHtmlResponse($dispatcher) && class_exists(LaravelDebugbar::class);
}

protected function getMessagesCollector(): MessagesCollector
{
if (self::$messagesCollector instanceof MessagesCollector) {
return self::$messagesCollector;
}

$messagesCollector = new MessagesCollector('Soar Scores');
if (! app(LaravelDebugbar::class)->hasCollector($messagesCollector->getName())) {
app(LaravelDebugbar::class)->addCollector($messagesCollector);
}

return self::$messagesCollector = $messagesCollector;
return app()->has(LaravelDebugbar::class)
&& class_exists(LaravelDebugbar::class)
&& $this->isHtmlResponse($dispatcher);
}
}
4 changes: 1 addition & 3 deletions src/Outputs/DumpOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function __construct(bool $exit = false)
public function output(Collection $scores, $dispatcher): void
{
$scores
->each(static function (array $score): void {
dump($score);
})
->each(static fn (array $score) => dump($score))
->tap(function (): void {
if ($this->exit) {
exit(1); // @codeCoverageIgnore
Expand Down
7 changes: 5 additions & 2 deletions src/Outputs/LogOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
class LogOutput extends Output
{
protected string $channel;
protected string $level;

public function __construct(string $channel = 'daily')
public function __construct(string $channel = 'daily', string $level = 'warning')
{
$this->channel = $channel;
$this->level = $level;
}

/**
Expand All @@ -31,7 +33,8 @@ public function __construct(string $channel = 'daily')
*/
public function output(Collection $scores, $dispatcher): void
{
$scores->each(fn (array $score) => Log::channel($this->channel)->warning(
$scores->each(fn (array $score) => Log::channel($this->channel)->log(
$this->level,
$score['Summary'].PHP_EOL.to_pretty_json($score)
));
}
Expand Down
36 changes: 19 additions & 17 deletions src/Outputs/SoarBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@

namespace Guanguans\LaravelSoar\Outputs;

use DebugBar\JavascriptRenderer;
use DebugBar\DataCollector\MessagesCollector;
use Guanguans\LaravelSoar\SoarBar;
use Illuminate\Support\Collection;

class SoarBarOutput extends Output
{
private SoarBar $soarBar;
protected string $name;
protected string $label;

private JavascriptRenderer $javascriptRenderer;

public function __construct(SoarBar $soarBar)
public function __construct(string $name = 'Scores', string $label = 'warning')
{
$this->soarBar = $soarBar;
$this->javascriptRenderer = $soarBar->getJavascriptRenderer();
$this->name = $name;
$this->label = $label;
}

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

$scores->each(fn (array $score) => $this->soarBar['scores']->addMessage(
$soarBar = app(SoarBar::class);
if (! $soarBar->hasCollector($this->name)) {
$soarBar->addCollector(new MessagesCollector($this->name));
}

$scores->each(fn (array $score) => $soarBar[$this->name]->addMessage(
$score['Summary'].PHP_EOL.to_pretty_json($score),
'warning',
$this->label,
false
));

/** @var \Symfony\Component\HttpFoundation\Response $dispatcher */
$content = $dispatcher->getContent();
$head = $this->javascriptRenderer->renderHead();
$widget = $this->javascriptRenderer->render();
$head = $soarBar->getJavascriptRenderer()->renderHead();
$widget = $soarBar->getJavascriptRenderer()->render();

// Try to put the js/css directly before the </head>
$pos = strripos($content, '</head>');
if (false !== $pos) {
$content = substr($content, 0, $pos).$head.substr($content, $pos); // @codeCoverageIgnore
} else {
// Append the head before the widget
$widget = $head.$widget;
}
false !== $pos
? $content = substr($content, 0, $pos).$head.substr($content, $pos) // @codeCoverageIgnore
: $widget = $head.$widget; // Append the head before the widget

// Try to put the widget at the end, directly before the </body>
$pos = strripos($content, '</body>');
Expand Down
9 changes: 8 additions & 1 deletion src/Outputs/SyslogOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

class SyslogOutput extends Output
{
protected int $priority;

public function __construct(int $priority = LOG_WARNING)
{
$this->priority = $priority;
}

/**
* {@inheritDoc}
*
* @throws \JsonException
*/
public function output(Collection $scores, $dispatcher): void
{
$scores->each(fn (array $score) => syslog(LOG_WARNING, $score['Summary'].PHP_EOL.to_pretty_json($score)));
$scores->each(fn (array $score) => syslog($this->priority, $score['Summary'].PHP_EOL.to_pretty_json($score)));
}
}
4 changes: 2 additions & 2 deletions src/SoarBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class SoarBar extends DebugBar
*/
public function __construct()
{
$this->addCollector(new MessagesCollector('scores'))
->addCollector(new MemoryCollector())
$this->addCollector(new MemoryCollector())
->addCollector(new PhpInfoCollector())
// ->addCollector(new MessagesCollector('scores'))
->jsRenderer = new JavascriptRenderer($this);
}
}

0 comments on commit 9911e14

Please sign in to comment.