Skip to content

Commit

Permalink
Fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Apr 18, 2022
1 parent c7eddf2 commit 2e4cdf7
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 139 deletions.
6 changes: 2 additions & 4 deletions src/Contracts/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
interface Output
{
/**
* @param \Symfony\Component\HttpFoundation\Response $requestHandled
* @param \Illuminate\Console\Events\CommandFinished $requestHandled
* @param \Illuminate\Foundation\Http\Events\RequestHandled $requestHandled
* @param \Illuminate\Console\Events\CommandFinished|\Symfony\Component\HttpFoundation\Response $dispatcher
*
* @return mixed
*/
public function output(Collection $scores, $requestHandled);
public function output(Collection $scores, $dispatcher);
}
12 changes: 4 additions & 8 deletions src/OutputManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@ public function offsetSet($offset, $value): void
}

/**
* @param \Symfony\Component\HttpFoundation\Response $requestHandled
* @param \Illuminate\Console\Events\CommandFinished $requestHandled
* @param \Illuminate\Foundation\Http\Events\RequestHandled $requestHandled
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $requestHandled)
public function output(Collection $scores, $dispatcher)
{
/* @var Output $output */
foreach ($this->attributes as $output) {
event(new OutputtingEvent($output, $scores, $requestHandled));
$result = $output->output($scores, $requestHandled);
event(new OutputtingEvent($output, $scores, $dispatcher));
$result = $output->output($scores, $dispatcher);
event(new OutputtedEvent($output, $scores, $result));
}
}
Expand Down
14 changes: 3 additions & 11 deletions src/Outputs/ClockworkOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
class ClockworkOutput extends Output
{
/**
* @param \Illuminate\Console\Events\CommandFinished $event
* @param \Illuminate\Foundation\Http\Events\RequestHandled $event
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $event)
public function output(Collection $scores, $dispatcher)
{
if (! $this->shouldOutput($event)) {
if (! function_exists('clock')) {
return;
}

Expand All @@ -31,9 +28,4 @@ public function output(Collection $scores, $event)
clock()->warning($score['Summary'], $score);
});
}

protected function shouldOutput($event): bool
{
return function_exists('clock');
}
}
62 changes: 17 additions & 45 deletions src/Outputs/Concerns/OutputCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,47 @@
namespace Guanguans\LaravelSoar\Outputs\Concerns;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Str;

trait OutputCondition
{
/**
* @param \Symfony\Component\HttpFoundation\Response $event
* @param \Illuminate\Console\Events\CommandFinished $event
* @param \Illuminate\Foundation\Http\Events\RequestHandled $event
* @param \Illuminate\Console\Events\CommandFinished|\Symfony\Component\HttpFoundation\Response $dispatcher
*/
protected function isEvent($event): bool
protected function isCommandFinished($dispatcher): bool
{
return $event instanceof CommandFinished ||
$event instanceof RequestHandled;
return $dispatcher instanceof CommandFinished;
}

/**
* @param \Symfony\Component\HttpFoundation\Response $event
* @param \Illuminate\Console\Events\CommandFinished $event
* @param \Illuminate\Foundation\Http\Events\RequestHandled $event
* @param \Illuminate\Console\Events\CommandFinished|\Symfony\Component\HttpFoundation\Response $dispatcher
*/
protected function isCommandFinishedEvent($event): bool
protected function isResponse($dispatcher): bool
{
return $event instanceof CommandFinished;
return $dispatcher instanceof Response;
}

/**
* @param \Symfony\Component\HttpFoundation\Response $event
* @param \Illuminate\Console\Events\CommandFinished $event
* @param \Illuminate\Foundation\Http\Events\RequestHandled $event
* @param \Illuminate\Console\Events\CommandFinished|\Symfony\Component\HttpFoundation\Response $dispatcher
*/
protected function isRequestHandledEvent($event): bool
protected function isHtmlResponse($dispatcher): bool
{
return $event instanceof RequestHandled;
return $dispatcher instanceof Response &&
Str::contains($dispatcher->headers->get('Content-Type'), 'text/html') &&
! $this->isJsonResponse($dispatcher);
}

/**
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Illuminate\Console\Events\CommandFinished $response
* @param \Illuminate\Foundation\Http\Events\RequestHandled $response
* @param \Illuminate\Console\Events\CommandFinished|\Symfony\Component\HttpFoundation\Response $dispatcher
*/
protected function isResponse($response): bool
protected function isJsonResponse($dispatcher): bool
{
return $response instanceof Response;
}

/**
* @param \Illuminate\Http\Response $response
* @param \Illuminate\Http\JsonResponse $response
*/
protected function isHtmlResponse($response): bool
{
return $response instanceof Response &&
Str::contains($response->headers->get('Content-Type'), 'text/html') &&
! $this->isJsonResponse($response);
}

/**
* @param \Illuminate\Http\Response $response
* @param \Illuminate\Http\JsonResponse $response
*/
protected function isJsonResponse($response): bool
{
return $response instanceof JsonResponse &&
Str::contains($response->headers->get('Content-Type'), 'application/json') &&
transform($response, function (JsonResponse $response) {
if ('' === ($content = $response->getContent())) {
return $dispatcher instanceof JsonResponse &&
Str::contains($dispatcher->headers->get('Content-Type'), 'application/json') &&
transform($dispatcher, function (JsonResponse $dispatcher) {
if ('' === ($content = $dispatcher->getContent())) {
return false;
}

Expand Down
19 changes: 6 additions & 13 deletions src/Outputs/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
class ConsoleOutput extends Output
{
/**
* @param \Illuminate\Foundation\Http\Events\RequestHandled $requestHandled
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $requestHandled)
public function output(Collection $scores, $dispatcher)
{
if (! $this->shouldOutput($requestHandled)) {
if (! $this->isHtmlResponse($dispatcher)) {
return;
}

$js = $this->transformToJs($scores);
$content = $requestHandled->getContent();
$content = $dispatcher->getContent();

// Try to put the widget at the end, directly before the </body>
$pos = strripos($content, '</body>');
Expand All @@ -37,13 +35,8 @@ public function output(Collection $scores, $requestHandled)
}

// Update the new content and reset the content length
$requestHandled->setContent($content);
$requestHandled->headers->remove('Content-Length');
}

protected function shouldOutput($requestHandled): bool
{
return $this->isHtmlResponse($requestHandled);
$dispatcher->setContent($content);
$dispatcher->headers->remove('Content-Length');
}

protected function transformToJs(Collection $scores): string
Expand Down
12 changes: 5 additions & 7 deletions src/Outputs/DebugBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ class DebugBarOutput extends Output
private static $outputted = false;

/**
* @param \Symfony\Component\HttpFoundation\Response $response
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $response)
public function output(Collection $scores, $dispatcher)
{
if (! $this->shouldOutput($response)) {
if (! $this->shouldOutput($dispatcher)) {
return;
}

Expand All @@ -52,9 +50,9 @@ public static function isOutputted(): bool
return self::$outputted;
}

protected function shouldOutput($response): bool
protected function shouldOutput($dispatcher): bool
{
return $this->isResponse($response) &&
return $this->isHtmlResponse($dispatcher) &&
class_exists('Barryvdh\Debugbar\Facade') &&
\Barryvdh\Debugbar\Facade::isEnabled();
}
Expand Down
16 changes: 2 additions & 14 deletions src/Outputs/DumpOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,13 @@
class DumpOutput extends Output
{
/**
* @param \Illuminate\Console\Events\CommandFinished $event
* @param \Illuminate\Foundation\Http\Events\RequestHandled $event
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $event)
public function output(Collection $scores, $dispatcher)
{
if (! $this->shouldOutput($event)) {
return;
}

$scores->each(function (array $score) {
unset($score['Basic']);
dump($score);
});
}

protected function shouldOutput($event): bool
{
return true;
}
}
19 changes: 6 additions & 13 deletions src/Outputs/JsonOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
class JsonOutput extends Output
{
/**
* @param \Illuminate\Foundation\Http\Events\RequestHandled $requestHandled
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $requestHandled)
public function output(Collection $scores, $dispatcher)
{
if (! $this->shouldOutput($requestHandled)) {
if (! $this->isJsonResponse($dispatcher)) {
return;
}

Expand All @@ -32,14 +30,9 @@ public function output(Collection $scores, $requestHandled)
return $score;
});

$data = Arr::wrap($requestHandled->getData(true)) and $data['soar_scores'] = $scores;
$data = Arr::wrap($dispatcher->getData(true)) and $data['soar_scores'] = $scores;
// Update the new content and reset the content length
$requestHandled->setData($data);
$requestHandled->headers->remove('Content-Length');
}

protected function shouldOutput($requestHandled): bool
{
return $this->isJsonResponse($requestHandled);
$dispatcher->setData($data);
$dispatcher->headers->remove('Content-Length');
}
}
14 changes: 1 addition & 13 deletions src/Outputs/LogOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,13 @@ public function __construct($channel = 'daily')
}

/**
* @param \Illuminate\Console\Events\CommandFinished $event
* @param \Illuminate\Foundation\Http\Events\RequestHandled $event
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $event)
{
if (! $this->shouldOutput($event)) {
return;
}

$scores->each(function (array $score) {
unset($score['Basic']);
Log::channel($this->channel)->warning($score['Summary'].PHP_EOL.to_pretty_json($score));
});
}

protected function shouldOutput($event): bool
{
return true;
}
}
20 changes: 9 additions & 11 deletions src/Outputs/SoarBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ public function __construct(SoarBar $debugBar)
}

/**
* @param \Illuminate\Foundation\Http\Events\RequestHandled $requestHandled
*
* @return mixed
* {@inheritdoc}
*/
public function output(Collection $scores, $requestHandled)
public function output(Collection $scores, $dispatcher)
{
if (! $this->shouldOutput($requestHandled)) {
if (! $this->shouldOutput($dispatcher)) {
return;
}

Expand All @@ -48,7 +46,7 @@ public function output(Collection $scores, $requestHandled)
$this->debugBar['scores']->addMessage($score['Summary'].PHP_EOL.to_pretty_json($score), 'warning', false);
});

$content = $requestHandled->getContent();
$content = $dispatcher->getContent();
$head = $this->renderer->renderHead();
$widget = $this->renderer->render();

Expand All @@ -70,13 +68,13 @@ public function output(Collection $scores, $requestHandled)
}

// Update the new content and reset the content length
$requestHandled->setContent($content);
$requestHandled->headers->remove('Content-Length');
$dispatcher->setContent($content);
$dispatcher->headers->remove('Content-Length');
}

protected function shouldOutput($requestHandled): bool
protected function shouldOutput($dispatcher): bool
{
return ! DebugBarOutput::isOutputted()
&& $this->isHtmlResponse($requestHandled);
return ! DebugBarOutput::isOutputted() &&
$this->isHtmlResponse($dispatcher);
}
}

0 comments on commit 2e4cdf7

Please sign in to comment.