I have created custom service provider which extends XeroServiceProvide, Basically, I have multiple Xero Account and I want to change two configuration params value runtime consumer_key and consumer_secret. Is there a quick way. I have checked Service Container contextual binding but don't know how to use.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use DrawMyAttention\XeroLaravel\Providers\XeroServiceProvider;
class CustomXeroServiceProvider extends XeroServiceProvider
{
private $config = 'xero/config.php';
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Register the application services.
*
* @return void
*/
public function register($configParams = [])
{
parent::register();
if(file_exists(config_path($this->config))) {
$configPath = config_path($this->config);
$config = include $configPath;
}
$this->app->bind('XeroPrivate', function () use ($config,$configParams) {
if(is_array($configParams) && count($configParams) > 0){
if(isset($configParams['consumer_key'])){
$config['oauth']['consumer_key'] = $configParams['consumer_key'];
}
if(isset($configParams['consumer_secret'])){
$config['oauth']['consumer_secret'] = $configParams['consumer_secret'];
}
}
return new \XeroPHP\Application\PrivateApplication($config);
});
}
}
From Controller I tried changing value like this but bind params not changing dynamically
foreach($centers as $center) {
config(['xero.config.oauth.consumer_key' => $center->consumer_key]);
config(['xero.config.oauth.consumer_secret' => $center->consumer_secret]);
}
Update 2
Is there a way I can rebind service container after updating config file values or somehow i can refresh service provider binding?
config(['xero.config.oauth.consumer_key' => 'XXXXXXX']);
config(['xero.config.oauth.consumer_secret' => 'XXXXXX']);
// rebind Service provider after update
So it's the service provider that registers all the service container bindings, and it's done via the register method of the service provider implementation. That should raise another question: how does Laravel know about various service providers? Perhaps you think Laravel should figure that out automatically too?
In fact, it's called service container bindings, and you need to do it via the service provider. So it's the service provider that registers all the service container bindings, and it's done via the register method of the service provider implementation. That should raise another question: how does Laravel know about various service providers?
Next, you need to inform Laravel about your custom service provider so that it can load it along with other service providers during bootstrapping. To register your service provider, you just need to add an entry to the array of service providers in the config/app.php file. Add the following line to the providers array:
In every Laravel application, the appinstance is the container. The app()helper returns an instance of the container. Just like your custom container, the Laravel service container has a bind()and a make()method used for binding services and retrieving services. There is another method called singleton().
This is how I ended up doing. I have created a custom function that sets value on runtime.
/**
* connect to XERO by center
* @param $center
* @return mixed
*/
public static function bindXeroPrivateApplication($center){
$configPath = config_path('xero/config.php');
$config = include $configPath;
config(['xero.config.oauth.consumer_key' => $center->consumer_key]);
config(['xero.config.oauth.consumer_secret' => $center->consumer_secret]);
return \App::bind('XeroPrivate', function () use ($config,$center) {
$config['oauth']['consumer_key'] = $center->consumer_key;
$config['oauth']['consumer_secret'] = $center->consumer_secret;
return new \XeroPHP\Application\PrivateApplication($config);
});
}
I have model called Center.php and I am calling above function from that same model as below.
$center = Center::find(1);
self::bindXeroPrivateApplication($center);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With