0

In this example, I am trying to configure a package through a service provider for 2 separate account connections to Xero.

In the service provider:

    public function register()
    {
        // Merge defaults
        $this->mergeConfigFrom(
            __DIR__.'/../config.php', 'xero.config'
        );

        // Grab config
        $config = $this->app->config->get('xero.config');

        $this->app->bind('XeroPrivate', function () use ($config) {
            return new \XeroPHP\Application\PrivateApplication($config);
        });

    ...

I have a new config loaded in $config = $this->app->config->get('xero.config2');

What is the best way to switch between these configurations?

I think this has to be done at the service provider level, so is it best to make 2 service providers and bind each configuration to a different key?

Adam Lambert
  • 1,311
  • 3
  • 24
  • 45
  • Can you elaborate what do you mean by 2 accounts please? Whats the purpose of 2 separate configs? – Mihir Bhende Feb 22 '19 at 17:01
  • Each config has a separate set of API keys, as well as some public/private key file paths. – Adam Lambert Feb 22 '19 at 17:13
  • Why don't you use one config with nested array? Like `config('api1.key')` `config('api2.key')`? – Mihir Bhende Feb 22 '19 at 17:15
  • I can that yes. But I still need to be able to update which key the SDK is using within or prior to the service provider. The service provider is connecting another package for all the Xero API heavy lifting. This line `return new \XeroPHP\Application\PrivateApplication($config);` instantiates the class from within the package I am using, so `$config` is a pre-defined array in this case which I cannot alter. I just need to alter the config array values prior to it being used here. – Adam Lambert Feb 22 '19 at 17:16
  • The 2 configs ... are you picking 1 config on the fly based on user or based on env? – Tarek Adam Feb 22 '19 at 17:20
  • In this case, we do not have any users, but do have 2 different configs to use in order to use 2 different sets of credentials. You could think of this as 2 users. Within my ENV I have `XERO_APIKEY1`, `XERO_APIKEY2` etc. – Adam Lambert Feb 22 '19 at 17:21
  • I guess if we were expanding this to multiple users, then storing configs in the database and rewriting the service provider to pull these in would make sense. In this case however, we just have 1 user with 2 sets of fixed credentials. So ideally I just need to be able to find a way to dynamically switch these out prior to using the class. Without DI/Service container I would do something like `$xero = new Xero(); $xero->setConfig($config); $xero->makeCall();`, then change to config 2 etc. – Adam Lambert Feb 22 '19 at 17:27
  • Also you can just provider format: `config('provider1::API.endpoint')` and `config('provider2::API.endpoint')`, by this way you are using the same keys for different provider. It is like a provider by account. Therefore, you can do `$xero->setConfig(config('provider2::API'));` – manix Feb 22 '19 at 19:12
  • Why not just load both configurations into your `Xero` class, then use a `switch` of some sort (`radio buttons`, `dropdown` etc.) in your application to change between the configurations on the fly? – Peppermintology Jun 09 '19 at 13:11

0 Answers0