Skip to content

Commit 52c7a93

Browse files
committed
refactor(OutputManager, SoarServiceProvider): improve code readability and functionality
- Refactor `OutputManager` logic to replace shorthand syntax with explicit `if` statements for better clarity. - Update `sanitize` method in `ScoresSanitizer` to use `transform` instead of `map` for in-place collection modification. - Add `OutputScoresMiddleware` and `RayOutput` registration in `SoarServiceProvider`. - Rename `registerMacros` to `registerMixins` for clearer intent in `SoarServiceProvider`. - Reorganize singleton registrations for consistent order and inclusion of new middleware. - Remove redundant `toAlias` method from `SoarServiceProvider` to simplify the codebase.
1 parent b527ae0 commit 52c7a93

File tree

4 files changed

+39
-53
lines changed

4 files changed

+39
-53
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ parameters:
8383
# - identifier: cast.string
8484
# - identifier: encapsedStringPart.nonString
8585
# - identifier: logicalAnd.resultUnused
86-
# - identifier: return.void
8786
# - identifier: symplify.noDynamicName
8887
# - identifier: typePerfect.noMixedMethodCaller
8988
- identifier: argument.templateType
@@ -93,6 +92,7 @@ parameters:
9392
- identifier: missingType.generics
9493
- identifier: missingType.iterableValue
9594
- identifier: return.type
95+
- identifier: return.void
9696
- identifier: shipmonk.deadMethod
9797
- identifier: symplify.explicitTraitSuffixName
9898
- identifier: symplify.requireExceptionNamespace

src/OutputManager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public function output(Collection $scores, CommandFinished|Response $outputter):
5050
continue;
5151
}
5252

53-
$output instanceof SanitizerContract and $scores = $output->sanitize($scores);
53+
if ($output instanceof SanitizerContract) {
54+
$scores = $output->sanitize($scores);
55+
}
56+
5457
event(new OutputtingEvent($output, $scores, $outputter));
5558
$result = $output->output($scores, $outputter);
5659
event(new OutputtedEvent($output, $scores, $outputter, $result));

src/Outputs/Concerns/ScoresSanitizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ trait ScoresSanitizer
2222

2323
public function sanitize(Collection $scores): Collection
2424
{
25-
return $scores->map(fn (array $score): array => Arr::except($score, $this->except));
25+
return $scores->transform(fn (array $score): array => Arr::except($score, $this->except));
2626
}
2727
}

src/SoarServiceProvider.php

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,36 @@
1313

1414
namespace Guanguans\LaravelSoar;
1515

16+
use Composer\InstalledVersions;
1617
use Guanguans\LaravelSoar\Commands\ClearCommand;
1718
use Guanguans\LaravelSoar\Commands\RunCommand;
1819
use Guanguans\LaravelSoar\Commands\ScoreCommand;
1920
use Guanguans\LaravelSoar\Mixins\QueryBuilderMixin;
20-
use Guanguans\LaravelSoar\Outputs\ClockworkOutput;
21-
use Guanguans\LaravelSoar\Outputs\ConsoleOutput;
22-
use Guanguans\LaravelSoar\Outputs\DebugBarOutput;
23-
use Guanguans\LaravelSoar\Outputs\DumpOutput;
24-
use Guanguans\LaravelSoar\Outputs\JsonOutput;
25-
use Guanguans\LaravelSoar\Outputs\LogOutput;
2621
use Illuminate\Contracts\Container\Container;
2722
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2823
use Illuminate\Database\Eloquent\Relations\Relation as RelationBuilder;
2924
use Illuminate\Database\Query\Builder as QueryBuilder;
25+
use Illuminate\Foundation\Console\AboutCommand;
3026
use Illuminate\Support\Collection;
3127
use Illuminate\Support\ServiceProvider;
32-
use Illuminate\Support\Str;
3328

3429
class SoarServiceProvider extends ServiceProvider
3530
{
3631
public array $singletons = [
37-
Bootstrapper::class => Bootstrapper::class,
38-
QueryBuilderMixin::class => QueryBuilderMixin::class,
39-
40-
ClockworkOutput::class => ClockworkOutput::class,
41-
ConsoleOutput::class => ConsoleOutput::class,
42-
DebugBarOutput::class => DebugBarOutput::class,
43-
DumpOutput::class => DumpOutput::class,
44-
JsonOutput::class => JsonOutput::class,
45-
LogOutput::class => LogOutput::class,
32+
Bootstrapper::class,
4633
];
4734

4835
/**
4936
* @noinspection PhpMissingParentCallCommonInspection
5037
*
51-
* @throws \Illuminate\Contracts\Container\BindingResolutionException
5238
* @throws \ReflectionException
5339
*/
5440
public function register(): void
5541
{
5642
$this->setupConfig();
57-
$this->registerMacros();
58-
$this->registerSoar();
43+
$this->registerMixins();
5944
$this->registerOutputManager();
45+
$this->registerSoar();
6046
}
6147

6248
/**
@@ -77,8 +63,6 @@ public function boot(): void
7763
public function provides(): array
7864
{
7965
return [
80-
$this->toAlias(OutputManager::class),
81-
$this->toAlias(Soar::class),
8266
Bootstrapper::class,
8367
OutputManager::class,
8468
Soar::class,
@@ -100,30 +84,16 @@ private function setupConfig(): void
10084
}
10185

10286
/**
103-
* @throws \Illuminate\Contracts\Container\BindingResolutionException
10487
* @throws \ReflectionException
10588
*/
106-
private function registerMacros(): void
89+
private function registerMixins(): void
10790
{
108-
$queryBuilderMixin = $this->app->make(QueryBuilderMixin::class);
91+
$queryBuilderMixin = new QueryBuilderMixin;
10992
EloquentBuilder::mixin($queryBuilderMixin);
11093
QueryBuilder::mixin($queryBuilderMixin);
11194
RelationBuilder::mixin($queryBuilderMixin);
11295
}
11396

114-
private function registerSoar(): void
115-
{
116-
$this->app->singleton(
117-
Soar::class,
118-
static fn (): Soar => Soar::make(
119-
config('soar.options', []),
120-
config('soar.binary')
121-
)->setSudoPassword(config('soar.sudo_password'))
122-
);
123-
124-
$this->app->alias(Soar::class, $this->toAlias(Soar::class));
125-
}
126-
12797
private function registerOutputManager(): void
12898
{
12999
$this->app->singleton(
@@ -137,10 +107,19 @@ private function registerOutputManager(): void
137107
/** @var string $class */
138108
return [$class => $container->make($class, $parameters)];
139109
})
140-
->pipe(static fn (Collection $collection): OutputManager => new OutputManager($collection->all()))
110+
->pipe(static fn (Collection $outputs): OutputManager => new OutputManager($outputs->all()))
141111
);
112+
}
142113

143-
$this->app->alias(OutputManager::class, $this->toAlias(OutputManager::class));
114+
private function registerSoar(): void
115+
{
116+
$this->app->singleton(
117+
Soar::class,
118+
static fn (): Soar => Soar::make(
119+
config('soar.options', []),
120+
config('soar.binary')
121+
)->setSudoPassword(config('soar.sudo_password'))
122+
);
144123
}
145124

146125
private function registerCommands(): void
@@ -151,20 +130,24 @@ private function registerCommands(): void
151130
RunCommand::class,
152131
ScoreCommand::class,
153132
]);
133+
134+
$this->addSectionToAboutCommand();
154135
}
155136
}
156137

157-
/**
158-
* @param class-string $class
159-
*/
160-
private function toAlias(string $class, string $prefix = 'soar.'): string
138+
private function addSectionToAboutCommand(): void
161139
{
162-
$alias = Str::snake(class_basename($class), '.');
163-
164-
if (Str::startsWith($alias, Str::replaceLast('.', '', $prefix))) {
165-
return $alias;
166-
}
167-
168-
return $prefix.$alias;
140+
AboutCommand::add(
141+
str($package = 'guanguans/laravel-soar')->headline()->toString(),
142+
static fn (): array => collect(['Homepage' => "https://github.com/$package"])
143+
->when(
144+
class_exists(InstalledVersions::class),
145+
static fn (Collection $data): Collection => $data->put(
146+
'Version',
147+
InstalledVersions::getPrettyVersion($package)
148+
)
149+
)
150+
->all()
151+
);
169152
}
170153
}

0 commit comments

Comments
 (0)