Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
Last active November 4, 2021 16:46
Show Gist options
  • Save kmuenkel/c9607e959b049260df188e34b8bfc804 to your computer and use it in GitHub Desktop.
Save kmuenkel/c9607e959b049260df188e34b8bfc804 to your computer and use it in GitHub Desktop.
Register Service Providers for 'require-dev' packages only if 'composer install' was run without the '--no-dev' argument.
<?php
namespace App\Providers;
class AppServiceProvider
{
public function register()
{
$this->registerDev()
}
protected function registerDev()
{
$installed = [];
if (File::exists($path = base_path('vendor').'/composer/installed.json')) {
$installed = json_decode(File::get($path), true);
$installed = array_column($installed['packages'] ?? $installed, 'name');
}
$configuration = json_decode(File::get(base_path('composer.json')), true);
$configuration = array_keys($configuration['require-dev'] ?? []);
if (array_intersect($installed, $configuration)) {
foreach (config('app.dev_providers', []) as $providerName) {
$this->app->register($providerName);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment