Skip to content

Commit

Permalink
refactor(music): replace MusicManager with MusicContract
Browse files Browse the repository at this point in the history
- Refactored MusicCommand to use MusicContract instead of MusicManager.
- Updated the facade to return MusicContract.
- Adjusted app bootstrap to bind MusicContract to the Music singleton.
- Improved the initialization of the music instance with the new contract.
  • Loading branch information
guanguans committed Sep 23, 2024
1 parent c39dd05 commit 5723d61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
7 changes: 2 additions & 5 deletions app/Commands/MusicCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

use App\Concerns\Hydrator;
use App\Contracts\Music as MusicContract;
use App\Music;
use App\Support\Utils;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Facades\File;
use Laravel\Prompts;
use LaravelZero\Framework\Commands\Command;
Expand Down Expand Up @@ -144,9 +142,8 @@ public function schedule(Schedule $schedule): void
#[\Override]
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->music = $this->laravel->make(Music::class, [
'driver' => Concurrency::driver($this->option('driver')),
]);
$this->option('driver') and config()->set('concurrency.default', $this->option('driver'));
$this->music = $this->laravel->make(MusicContract::class);
$this->input->setOption('dir', $this->option('dir') ?: Utils::defaultSavedDir());
$this->option('lang') and config()->set('app.locale', $this->option('lang'));
File::ensureDirectoryExists($this->option('dir'));
Expand Down
19 changes: 4 additions & 15 deletions app/Facades/Music.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,28 @@

namespace App\Facades;

use App\MusicManager;
use Illuminate\Support\Facades\Facade;

/**
* @method static \GuzzleHttp\Client createHttpClient(array $config = [])
* @method static void download(string $url, string $savedPath)
* @method static mixed driver(string|null $driver = null)
* @method static \App\MusicManager extend(string $driver, \Closure $callback)
* @method static void flushMacros()
* @method static \App\MusicManager forgetDrivers()
* @method static \Illuminate\Contracts\Container\Container getContainer()
* @method static string getDefaultDriver()
* @method static array getDrivers()
* @method static bool hasMacro(string $name)
* @method static void macro(string $name, object|callable $macro, object|callable $macro = null)
* @method static void macro(string $name, object|callable $macro)
* @method static void mixin(object $mixin, bool $replace = true)
* @method static \Illuminate\Support\Collection search(string $keyword, array $sources = [])
* @method static \App\MusicManager setContainer(\Illuminate\Contracts\Container\Container $container)
* @method static void tap(callable|null $callback = null)
* @method static \App\MusicManager|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
* @method static \App\MusicManager|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
*
* @see \App\MusicManager
* @see \App\Music
*/
final class Music extends Facade
{
/**
* @noinspection MissingParentCallInspection
* @noinspection MethodVisibilityInspection
* @noinspection PhpMissingParentCallCommonInspection
*/
#[\Override]
protected static function getFacadeAccessor(): string
{
return MusicManager::class;
return \App\Contracts\Music::class;
}
}
15 changes: 15 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
* @see https://github.com/guanguans/music-dl
*/

use App\Contracts\Music as MusicContract;
use App\Music;
use App\MusicManager;
use App\Support\Meting;
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 @@ -22,6 +26,17 @@
->withSingletons([
MusicManager::class,
])
->booted(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)
);
})
->booted(static function (Application $app): void {
if (class_exists(TinkerZeroServiceProvider::class) && !$app->isProduction()) {
$app->register(TinkerZeroServiceProvider::class);
Expand Down

0 comments on commit 5723d61

Please sign in to comment.