Skip to content

Commit 501fdb2

Browse files
committed
refactor(DebugBarOutput): Improve class handling for Laravel Debugbar
- Replace direct class references with a method to determine the class. - Simplify the output method's return type to object. - Comment out unnecessary assertions for better flexibility. - Update test case to use the new method for class resolution.
1 parent 6c13c6b commit 501fdb2

4 files changed

Lines changed: 45 additions & 15 deletions

File tree

composer-dependency-analyser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
],
4242
[ErrorType::SHADOW_DEPENDENCY]
4343
)
44-
->ignoreErrorsOnPackageAndPath(
45-
'barryvdh/laravel-debugbar',
46-
__DIR__.'/src/Outputs/DebugBarOutput.php',
47-
[ErrorType::DEV_DEPENDENCY_IN_PROD]
48-
)
44+
// ->ignoreErrorsOnPackageAndPath(
45+
// 'barryvdh/laravel-debugbar',
46+
// __DIR__.'/src/Outputs/DebugBarOutput.php',
47+
// [ErrorType::DEV_DEPENDENCY_IN_PROD]
48+
// )
4949
->ignoreErrorsOnPackageAndPath(
5050
'php-debugbar/php-debugbar',
5151
__DIR__.'/src/Outputs/DebugBarOutput.php',

src/Outputs/DebugBarOutput.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Guanguans\LaravelSoar\Outputs;
1515

16-
use Barryvdh\Debugbar\LaravelDebugbar;
1716
use DebugBar\DataCollector\MessagesCollector;
1817
use Illuminate\Console\Events\CommandFinished;
1918
use Illuminate\Support\Collection;
@@ -31,23 +30,22 @@ public function __construct(
3130
*/
3231
public function shouldOutput(CommandFinished|Response $outputter): bool
3332
{
34-
return class_exists(LaravelDebugbar::class)
35-
&& app()->has(LaravelDebugbar::class)
36-
// && resolve(LaravelDebugbar::class)->isEnabled()
33+
return class_exists(self::laravelDebugbarClass())
34+
&& app()->has(self::laravelDebugbarClass())
35+
// && resolve($this->laravelDebugbarClass())->isEnabled()
3736
&& $this->isHtmlResponse($outputter);
3837
}
3938

4039
/**
4140
* @throws \DebugBar\DebugBarException
4241
* @throws \JsonException
4342
*
44-
* @noinspection PhpPossiblePolymorphicInvocationInspection
43+
* @return \Barryvdh\Debugbar\LaravelDebugbar|\Fruitcake\LaravelDebugbar\LaravelDebugbar
4544
*/
46-
public function output(Collection $scores, CommandFinished|Response $outputter): LaravelDebugbar
45+
public function output(Collection $scores, CommandFinished|Response $outputter): object
4746
{
48-
$debugBar = resolve(LaravelDebugbar::class);
49-
50-
\assert($debugBar instanceof LaravelDebugbar);
47+
$debugBar = resolve(self::laravelDebugbarClass());
48+
// \assert($debugBar instanceof LaravelDebugbar);
5149

5250
if (!$debugBar->hasCollector($this->name)) {
5351
$debugBar->addCollector(new MessagesCollector($this->name));
@@ -61,4 +59,26 @@ public function output(Collection $scores, CommandFinished|Response $outputter):
6159

6260
return $debugBar;
6361
}
62+
63+
/**
64+
* @api
65+
*
66+
* @return class-string<\Barryvdh\Debugbar\ServiceProvider|\Fruitcake\LaravelDebugbar\ServiceProvider>
67+
*/
68+
public static function laravelDebugbarServiceProviderClass(): string
69+
{
70+
return class_exists($class = 'Fruitcake\LaravelDebugbar\ServiceProvider')
71+
? $class
72+
: 'Barryvdh\Debugbar\ServiceProvider';
73+
}
74+
75+
/**
76+
* @return class-string<\Barryvdh\Debugbar\LaravelDebugbar|\Fruitcake\LaravelDebugbar\LaravelDebugbar>
77+
*/
78+
private static function laravelDebugbarClass(): string
79+
{
80+
return class_exists($class = 'Fruitcake\LaravelDebugbar\LaravelDebugbar')
81+
? $class
82+
: 'Barryvdh\Debugbar\LaravelDebugbar';
83+
}
6484
}

testbench.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ laravel: '@testbench'
22

33
providers:
44
- Workbench\App\Providers\WorkbenchServiceProvider
5-
- Barryvdh\Debugbar\ServiceProvider
5+
# - Barryvdh\Debugbar\ServiceProvider
6+
# - Fruitcake\LaravelDebugbar\ServiceProvider
67
- Clockwork\Support\Laravel\ClockworkServiceProvider
78
- Guanguans\LaravelSoar\SoarServiceProvider
89
- LaraDumps\LaraDumps\LaraDumpsServiceProvider

tests/TestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace Guanguans\LaravelSoarTests;
2626

2727
use Guanguans\LaravelSoar\Facades\Soar;
28+
use Guanguans\LaravelSoar\Outputs\DebugBarOutput;
2829
use Illuminate\Contracts\Config\Repository;
2930
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
3031
use Illuminate\Foundation\Testing\DatabaseMigrations;
@@ -68,6 +69,14 @@ protected function getPackageAliases(mixed $app): array
6869
];
6970
}
7071

72+
protected function getPackageProviders(mixed $app): array
73+
{
74+
return [
75+
...parent::getPackageProviders($app),
76+
DebugBarOutput::laravelDebugbarServiceProviderClass(),
77+
];
78+
}
79+
7180
protected function defineEnvironment(mixed $app): void
7281
{
7382
tap($app->make(Repository::class), function (Repository $repository): void {

0 commit comments

Comments
 (0)