Skip to content

Commit

Permalink
refactor(output): implement shouldOutput method
Browse files Browse the repository at this point in the history
  - Add `shouldOutput` method to the `Output` interface
  - Implement `shouldOutput` method in all output classes
  • Loading branch information
guanguans committed Jan 4, 2024
1 parent 0fa89f6 commit 247dfb0
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 39 deletions.
5 changes: 5 additions & 0 deletions src/Contracts/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

interface Output
{
/**
* @param \Illuminate\Console\Events\CommandFinished|\Symfony\Component\HttpFoundation\Response $dispatcher
*/
public function shouldOutput($dispatcher): bool;

/**
* @param \Illuminate\Console\Events\CommandFinished|\Symfony\Component\HttpFoundation\Response $dispatcher
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Exceptions/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/laravel-soar.
*
* (c) guanguans <ityaozm@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelSoar\Exceptions;

use Guanguans\LaravelSoar\Contracts\Throwable;

class BadMethodCallException extends \BadMethodCallException implements Throwable {}
11 changes: 9 additions & 2 deletions src/OutputManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Guanguans\LaravelSoar\Contracts\Sanitizer;
use Guanguans\LaravelSoar\Events\OutputtedEvent;
use Guanguans\LaravelSoar\Events\OutputtingEvent;
use Guanguans\LaravelSoar\Exceptions\BadMethodCallException;
use Guanguans\LaravelSoar\Exceptions\InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
Expand Down Expand Up @@ -55,13 +56,19 @@ public function output(Collection $scores, $dispatcher): void
{
/** @var \Guanguans\LaravelSoar\Contracts\Output $output */
foreach ($this->attributes as $output) {
if ($output instanceof Sanitizer) {
$scores = $output->sanitize($scores);
if (! $output->shouldOutput($dispatcher)) {
continue;
}

$output instanceof Sanitizer and $scores = $output->sanitize($scores);
event(new OutputtingEvent($output, $scores, $dispatcher));
$result = $output->output($scores, $dispatcher);
event(new OutputtedEvent($output, $scores, $result));
}
}

public function shouldOutput($dispatcher): bool
{
throw new BadMethodCallException(sprintf('The method [%s] is not implemented.', __METHOD__));
}
}
9 changes: 5 additions & 4 deletions src/Outputs/ClockworkOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

class ClockworkOutput extends Output
{
public function output(Collection $scores, $dispatcher): void
public function shouldOutput($dispatcher): bool
{
if (! \function_exists('clock')) {
return; // @codeCoverageIgnore
}
return \function_exists('clock');
}

public function output(Collection $scores, $dispatcher): void
{
clock(...$scores);
}
}
9 changes: 5 additions & 4 deletions src/Outputs/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public function __construct(string $method = 'warn')
$this->method = $method;
}

public function output(Collection $scores, $dispatcher): void
public function shouldOutput($dispatcher): bool
{
if (! $this->isHtmlResponse($dispatcher)) {
return;
}
return $this->isHtmlResponse($dispatcher);
}

public function output(Collection $scores, $dispatcher): void
{
$js = $this->toJavascript($scores);
$content = $dispatcher->getContent();

Expand Down
20 changes: 8 additions & 12 deletions src/Outputs/DebugBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@ public function __construct(string $name = 'Soar Scores', string $label = 'warni
$this->label = $label;
}

public function shouldOutput($dispatcher): bool
{
// app(LaravelDebugbar::class)->isEnabled()
return class_exists(LaravelDebugbar::class)
&& app()->has(LaravelDebugbar::class)
&& $this->isHtmlResponse($dispatcher);
}

/**
* @param mixed $dispatcher
*
* @throws \JsonException
*/
public function output(Collection $scores, $dispatcher): void
{
if (! $this->shouldOutput($dispatcher)) {
return;
}

$laravelDebugbar = app(LaravelDebugbar::class);
if (! $laravelDebugbar->hasCollector($this->name)) {
$laravelDebugbar->addCollector(new MessagesCollector($this->name));
Expand All @@ -56,12 +60,4 @@ public static function isOutputted(): bool
{
return self::$outputted;
}

protected function shouldOutput($dispatcher): bool
{
// app(LaravelDebugbar::class)->isEnabled()
return class_exists(LaravelDebugbar::class)
&& app()->has(LaravelDebugbar::class)
&& $this->isHtmlResponse($dispatcher);
}
}
9 changes: 5 additions & 4 deletions src/Outputs/JsonOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public function __construct(string $key = 'soar_scores')
$this->key = $key;
}

public function output(Collection $scores, $dispatcher): void
public function shouldOutput($dispatcher): bool
{
if (! $this->isJsonResponse($dispatcher)) {
return;
}
return $this->isJsonResponse($dispatcher);
}

public function output(Collection $scores, $dispatcher): void
{
/** @var \Symfony\Component\HttpFoundation\JsonResponse $dispatcher */
$data = Arr::wrap($dispatcher->getData(true));
Arr::set($data, $this->key, $scores);
Expand Down
5 changes: 5 additions & 0 deletions src/Outputs/NullOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

class NullOutput implements Output
{
public function shouldOutput($dispatcher): bool
{
return true;
}

public function output(Collection $scores, $dispatcher): void
{
// noop
Expand Down
5 changes: 5 additions & 0 deletions src/Outputs/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ abstract class Output implements \Guanguans\LaravelSoar\Contracts\Output, Saniti
use OutputConditions;
use ScoresHydrator;
use ScoresSanitizer;

public function shouldOutput($dispatcher): bool
{
return true;
}
}
9 changes: 5 additions & 4 deletions src/Outputs/RayOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ public function __construct(string $label = 'Soar Scores')
$this->label = $label;
}

public function shouldOutput($dispatcher): bool
{
return \function_exists('ray');
}

/**
* @psalm-suppress UndefinedDocblockClass
*
* @param mixed $dispatcher
*/
public function output(Collection $scores, $dispatcher): void
{
if (! \function_exists('ray')) {
return; // @codeCoverageIgnore
}

ray(...$scores)->label($this->label);
}
}
14 changes: 5 additions & 9 deletions src/Outputs/SoarBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ public function __construct(string $name = 'Scores', string $label = 'warning')
$this->label = $label;
}

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

/**
* @param mixed $dispatcher
*
* @throws \JsonException
*/
public function output(Collection $scores, $dispatcher): void
{
if (! $this->shouldOutput($dispatcher)) {
return;
}

$soarBar = app(SoarBar::class);
if (! $soarBar->hasCollector($this->name)) {
$soarBar->addCollector(new MessagesCollector($this->name));
Expand Down Expand Up @@ -68,9 +69,4 @@ public function output(Collection $scores, $dispatcher): void
$dispatcher->setContent($content);
$dispatcher->headers->remove('Content-Length');
}

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

0 comments on commit 247dfb0

Please sign in to comment.