Configuration

If your are familar with Shopware plugins you might know you can create a simple config.xml file for defining a plugin configuration.

dustin/shopware-utils lets you define more than one config file and automatically installs it's default values. This can be done for plugins and AdditionalBundles. All .xml-files in Resources/config/ (except services.xml and routes.xml) will be considered a config file. If xml-parsing detects a structure other than a typical configuration file, the file is skipped.

Create as many config files as you want in your plugin or bundle:

<bundle-or-plugin-dir> |— Resources |— — config |— — — config.xml |— — — my_additional_config.xml |— — — another_config.xml

Now you are able to use this configuration in services or create a new administration module with a user interface.

Use configuration in services

Configurations can be encapsulated into an object and used as service via dependency injection. dustin/shopware-utils brings a Symfony service which can be used as factory to create a configuration object.

Create a global config object

<service id="my_plugin.config" class="Dustin\ShopwareUtils\Core\Framework\Struct\Encapsulation">
    <factory service="Dustin\ShopwareUtils\Core\System\SystemConfig\ConfigFactory" method="createConfig" />
    <argument type="string">MyPlugin.config</argument>
<service>

This will create a new service with the global config config from the bundle or plugin MyPlugin.

Use the config object in a service

<service id="My\Service">
    <argument type="service" id="my_plugin.config" />
<service>
use Dustin\ShopwareUtils\Core\Framework\Struct\Encapsulation;

class Service {

    public function __construct(
        private readonly Encapsulation $config
    ) {}
    
    public function doSomething(): void {
    
        // get a single config value
        $configValue = $this->config->get('config_key');
        
        // get an associative array of multiple config values
        $configValues = $this->config->getList(['first_key', 'second_key']);
        
        // check if a key exists
        $has = $this->config->has('key');
        
        // get all keys of a config
        $keys = $this->config->getFields();
        
        // Check if a config is empty
        $isEmpty = $this->config->isEmpty();
        
        // convert config to an array
        $array = $this->config->toArray();
        
        // iterate over all values
        foreach($this->config as $key => $value) {
            // do some fancy stuff
        }
    }
    
}

Create a config object for sales channels

If you need the configuration for a specific sales channel (especially in storefront services) you can also create a config object which holds one inner config object per sales channel.

<service id="my_plugin.config.per_sales_channel" class="Dustin\ShopwareUtils\Core\Framework\Struct\Encapsulation">
    <factory service="Dustin\ShopwareUtils\Core\System\SystemConfig\ConfigFactory" method="createConfigPerSalesChannel" />
    <argument type="string">MyPlugin.config</argument>
<service>

<service id="My\Service">
    <argument type="service" id="my_plugin.config.per_sales_channel" />
<service>
use Dustin\ShopwareUtils\Core\Framework\Struct\Encapsulation;

class Service {

    public function __construct(
        private readonly Encapsulation $configPerSalesChannel
    ) {}
    
    public function doSomething(string $salesChannelId): void {
        $config = $this->configPerSalesChannel->get($salesChannelId);
    
       // continue with using the config object like shown above
    }
    
}

Last updated