Skip to content

Commit 044bbf7

Browse files
committed
Add console output
1 parent 12cd06f commit 044bbf7

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

config/soar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
'output' => [
4141
\Guanguans\LaravelSoar\Outputs\BarOutput::class,
42+
\Guanguans\LaravelSoar\Outputs\ConsoleOutput::class,
4243
\Guanguans\LaravelSoar\Outputs\DebugBarOutput::class,
4344
\Guanguans\LaravelSoar\Outputs\JsonOutput::class,
4445
\Guanguans\LaravelSoar\Outputs\LogOutput::class,

src/Outputs/Concerns/OutputCondition.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Foundation\Http\Events\RequestHandled;
1515
use Illuminate\Http\JsonResponse;
1616
use Illuminate\Http\Response;
17+
use Illuminate\Support\Str;
1718

1819
trait OutputCondition
1920
{
@@ -58,8 +59,20 @@ protected function shouldOutputInResponse($operator): bool
5859
return $operator instanceof Response;
5960
}
6061

61-
protected function shouldOutputInDebugBar(): bool
62+
/**
63+
* @param mixed $operator
64+
*/
65+
protected function shouldOutputInHtmlResponse($operator): bool
66+
{
67+
return $operator instanceof Response
68+
&& ! $operator instanceof JsonResponse
69+
&& Str::contains($operator->headers->get('Content-Type'), 'text/html');
70+
}
71+
72+
protected function shouldOutputInDebugBar($operator): bool
6273
{
63-
return class_exists('Barryvdh\Debugbar\Facade');
74+
return class_exists('Barryvdh\Debugbar\Facade')
75+
&& \Barryvdh\Debugbar\Facade::isEnabled()
76+
&& $operator instanceof Response;
6477
}
6578
}

src/Outputs/ConsoleOutput.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the guanguans/laravel-soar.
5+
*
6+
* (c) guanguans <ityaozm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Guanguans\LaravelSoar\Outputs;
12+
13+
use Illuminate\Support\Collection;
14+
15+
class ConsoleOutput extends Output
16+
{
17+
public function output(Collection $scores, $operator)
18+
{
19+
if (! $this->shouldOutputInHtmlResponse($operator)) {
20+
return;
21+
}
22+
23+
$content = $operator->getContent();
24+
$outputContent = $this->getOutputContent($scores);
25+
26+
$content = false === ($pos = strripos($content, '</body>'))
27+
? $content.$outputContent
28+
: substr($content, 0, $pos).$outputContent.substr($content, $pos);
29+
30+
// Update the new content and reset the content length
31+
$operator->setContent($content);
32+
$operator->headers->remove('Content-Length');
33+
}
34+
35+
protected function getOutputContent(Collection $scores)
36+
{
37+
$output = '<script type="text/javascript">';
38+
foreach ($scores as $score) {
39+
$output .= sprintf("console.warn(JSON.parse('%s')); ", addslashes(json_encode($score, JSON_UNESCAPED_UNICODE)));
40+
}
41+
$output .= '</script>';
42+
43+
return $output;
44+
}
45+
}

src/Outputs/DebugBarOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DebugBarOutput extends Output
2828
*/
2929
public function output(Collection $scores, $response)
3030
{
31-
if (! $this->shouldOutputInDebugBar()) {
31+
if (! $this->shouldOutputInDebugBar($response)) {
3232
return;
3333
}
3434

0 commit comments

Comments
 (0)