# Config Service

To access any config variable set from the h0stware settings or a module, you can use the ConfigService class.

### Access the config service

To access the config service, you can - just like everywhere else - use the Laravel dependency injection function `app()`. This return the requested class instance.

### Save values

To save config values, you can use the `save()` function of the config service. The function take a key -> value array of config elements as first parameter and the salesChannelAware boolean as second parameter.

If you leave the salesChannelAware to true, the config elements are written to the currently active saleschannel. If you set it to false, the config elements are written to the global scope of all sales channels in h0stware.

See the example below to save config entries to the currently selected sales channel or in the global scope:

```php
$configArray = [
    "HwMollie.sandbox" => false,
    "HwMollie.key.live" => "live key"
];

// save the config
app(ConfigService::class)->save($configArray);

app(ConfigService::class)->save($configArray, false); // save to global scope
```

### Get config value(s)

To get a value by the key, you can use the `get()` function of the config service. The function take the key of the requested config element as first parameter and the salesChannelAware boolean as second parameter.

The keys always use dot notation (.) to provide sections of config values. If you want e.g. all config values of a dot prefix, you may only provide the prefix as key. See the following example for better understanding:

```php
$configService = app(ConfigService::class);

$configService->get("HwMollie");
// returns following:
array:3 [▼
  "sandbox" => "false"
  "handler" => array:2 [▼
    "paysafecard" => "true"
    "useMollieLimits" => "true"
  ]
  "key" => array:2 [▼
    "live" => "vpsnow LIVE"
    "test" => "TEST api"
  ]
]

$configService->get("HwMollie.handler");
// returns following:
array:2 [▼
  "paysafecard" => "true"
  "useMollieLimits" => "true"
]

$configService->get("HwMollie.handler.paysafecard");
// returns following:
"true"
```

### Flatten / Unflatten elements

As you have noticed before, the config element keys use the dot notation:

```php
array:3 [▼
  "HwMollie.sandbox" => "false"
  "HwMollie.handler.paysafecard" => "true"
  "HwMollie.key.live" => "livea"
]
```

You may use the `unflattenConfigArray()` function of the config service to create a multi dimensional array of the dot syntax:

```php
array:1 [▼
  "HwMollie" => array:3 [▼
    "sandbox" => "false"
    "handler" => array:1 [▼
      "paysafecard" => "true"
    ]
    "key" => array:1 [▼
      "live" => "livea"
    ]
  ]
```

Vice versa, you can use the `flattenConfigArray()` function to create a onedimensional array of dot syntax from a multidimensional array, which is required to e.g. save the config values.
