Skip to content

Commit

Permalink
refactor(music): improve dependency injection and initialization
Browse files Browse the repository at this point in the history
- Change constructor parameters to be nullable for Meting and Driver.
- Use null coalescing assignment to initialize Meting and Driver if not provided.
- Update the app service provider to simplify Music singleton registration.
  • Loading branch information
guanguans committed Sep 27, 2024
1 parent 4a3f341 commit 6ae0cf9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
14 changes: 9 additions & 5 deletions app/Music.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Concurrency\Driver;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Timebox;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Dumpable;
Expand All @@ -37,12 +38,15 @@ class Music implements Contracts\HttpClientFactory, Contracts\Music
use Localizable;

public function __construct(
private Meting $meting,
private Driver $driver,
private ?Meting $meting = null,
private ?Driver $driver = null,
private ?Timebox $timebox = null,
) {
$this->meting = $meting->format();
$this->timebox = $timebox ?: new Timebox;
$this->meting ??= new Meting;
$this->driver ??= Concurrency::driver();
$this->timebox ??= new Timebox;

$this->meting->format();
}

/**
Expand Down Expand Up @@ -72,7 +76,7 @@ public function search(string $keyword, array $sources = []): Collection
])
->values()
->mapWithKeys(static fn (array $song, int $index): array => [$index + 1 => $song]),
6180 * 1000
3820 * 1000
);
}

Expand Down
3 changes: 0 additions & 3 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ final class AppServiceProvider extends ServiceProvider
Conditionable::when as whenever;
}

/** @var array<class-string|int, class-string> */
public array $singletons = [];

/**
* @noinspection PhpMissingParentCallCommonInspection
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Support/Meting.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace App\Support;

final class Meting extends \Metowolf\Meting
class Meting extends \Metowolf\Meting
{
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
protected array $temp = [];
Expand Down
13 changes: 2 additions & 11 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

use App\Contracts\Music as MusicContract;
use App\Music;
use App\Support\Meting;
use Illuminate\Console\OutputStyle;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Log\LogManager;
use Illuminate\Support\Facades\Concurrency;
use Intonate\TinkerZero\TinkerZeroServiceProvider;
use LaravelZero\Framework\Application;
use Psr\Log\LoggerInterface;
Expand All @@ -31,15 +29,8 @@
}
})
->booting(static function (Application $app): void {
$app->singleton(
Music::class,
static fn (Application $app): Music => new Music(new Meting, Concurrency::driver())
);

$app->bind(
MusicContract::class,
static fn (Application $app): MusicContract => $app->make(Music::class)
);
$app->singleton(Music::class);
$app->bind(MusicContract::class, Music::class);

$app->singletonIf(
OutputStyle::class,
Expand Down

0 comments on commit 6ae0cf9

Please sign in to comment.