Skip to content

Commit

Permalink
✨ Update the Laracord provider to support Laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed May 9, 2024
1 parent 504791b commit 2fe2448
Showing 1 changed file with 65 additions and 18 deletions.
83 changes: 65 additions & 18 deletions src/LaracordServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Laracord;

use Illuminate\Contracts\Foundation\CachesConfiguration;
use Illuminate\Contracts\Http\Kernel as KernelContract;
use Illuminate\Support\ServiceProvider;
use Laracord\Http\Kernel;
use LaravelZero\Framework\Components\Database\Provider as DatabaseProvider;

class LaracordServiceProvider extends ServiceProvider
{
Expand All @@ -31,29 +33,15 @@ class LaracordServiceProvider extends ServiceProvider
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/app.php', 'app');
$this->mergeConfigFrom(__DIR__.'/../config/cache.php', 'cache');
$this->mergeConfigFrom(__DIR__.'/../config/commands.php', 'commands');
$this->mergeConfigFrom(__DIR__.'/../config/database.php', 'database');
$this->mergeConfigFrom(__DIR__.'/../config/discord.php', 'discord');
$this->mergeConfigFrom(__DIR__.'/../config/filesystems.php', 'filesystems');
$this->mergeConfigFrom(__DIR__.'/../config/view.php', 'view');

$paths = [
'cache' => $this->app['config']->get('cache.stores.file.path'),
'view' => $this->app['config']->get('view.compiled'),
];

foreach ($paths as $path) {
if (! is_dir($path)) {
mkdir($path, 0755, true);
}
}
$this->mergeConfigs();
$this->createDirectories();

foreach ($this->providers as $provider) {
$this->app->register($provider);
}

$this->registerDatabase();

$this->app->singleton(KernelContract::class, Kernel::class);
}

Expand All @@ -78,4 +66,63 @@ public function boot()
Console\Commands\TokenMakeCommand::class,
]);
}

/**
* Merge the application configuration.
*/
protected function mergeConfigs(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/app.php', 'app');
$this->mergeConfigFrom(__DIR__.'/../config/cache.php', 'cache');
$this->mergeConfigFrom(__DIR__.'/../config/commands.php', 'commands');
$this->mergeConfigFrom(__DIR__.'/../config/database.php', 'database');
$this->mergeConfigFrom(__DIR__.'/../config/discord.php', 'discord');
$this->mergeConfigFrom(__DIR__.'/../config/filesystems.php', 'filesystems');
$this->mergeConfigFrom(__DIR__.'/../config/view.php', 'view');
}

/**
* Create the application directories.
*/
protected function createDirectories(): void
{
$paths = [
'cache' => $this->app['config']->get('cache.stores.file.path'),
'view' => $this->app['config']->get('view.compiled'),
];

foreach ($paths as $path) {
if (! is_dir($path)) {
mkdir($path, 0755, true);
}
}
}

/**
* Register the Database service provider if needed.
*/
protected function registerDatabase(): void
{
if (! (new DatabaseProvider($this->app))->isAvailable()) {
$this->app->booting(fn () => $this->app->register(DatabaseProvider::class));
}
}

/**
* Merge the given configuration with the existing configuration.
*
* @param string $path
* @param string $key
* @return void
*/
protected function mergeConfigFrom($path, $key)
{
if (! ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) {
$config = $this->app->make('config');

$config->set($key, array_merge(
$config->get($key, []), require $path
));
}
}
}

0 comments on commit 2fe2448

Please sign in to comment.