jeudi 13 septembre 2018

How to bind configuration value Laravel service provider on run-time?

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 enter link description here 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 Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2CPCEdd
via IFTTT

Aucun commentaire:

Enregistrer un commentaire