Created
June 4, 2024 13:30
-
-
Save DominikStyp/c7ec46d87f2903f1d864ac887a11be56 to your computer and use it in GitHub Desktop.
Laravel: How to change/swap container instance behind the Facade for example Log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Patches\LogManager; | |
use Illuminate\Log\LogServiceProvider; | |
use Illuminate\Support\Facades\Log; | |
class CustomLogServiceProvider extends LogServiceProvider | |
{ | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$instance = new LogManager($this->app); | |
// this is crutial since Illuminate\Support\Facades\Facade caches the instance in the static variable $resolvedInstance | |
Log::swap($instance); | |
// this will make sure that app('log') is also resolved correctly | |
$this->app->extend('log', function ($app) use ($instance) { | |
return $instance; | |
}); | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment