Semaio Magento 2 Config Import Export

repository·main·Indexed 41 days ago

https://github.com/semaio/magento2-configimportexport

A tool for importing and exporting Magento 2 configuration data from the core_config_data table using structured YAML or JSON files. It provides CLI commands `config:data:export` and `config:data:import` to manage settings across environments, supporting environment variable substitution, encryption placeholders, and scope-specific configurations.

Tokens
3.4K
Snippets
15
Records
18
Agent score
49%

What's inside semaio-magento2-configimportexport

  1. Delete or Keep configuration values using magic strings

    main

    You can control the behavior of specific configuration keys using special magic strings in your YAML/JSON files:

    • Delete a value: Use !!DELETE to remove a configuration value and return to default behavior.
    • Keep a value: Use !!KEEP to ensure the importer does not change a specific value. This is useful for maintaining environment-specific sensitive data that exists in the database but shouldn't be overwritten by the import process.

    Example:

    vendorx/general/api_key:
      default:
        0: "!!DELETE"
    vendorx/general/api_key:
      default:
        0: "!!KEEP"
  2. Use theme code substitution in config files

    main

    Instead of hard-coding theme IDs, use the %theme(path)% placeholder. This allows you to use the theme code (the component name defined in registration.php).

    Requirement: You must include the area (e.g., frontend/) in the path.

    Example:

    design/theme/theme_id:
      default:
        0: '%theme(frontend/Vendor/theme)%'
  3. Use encryption value substitution in config files

    main

    For fields using Magento's \Magento\Config\Model\Config\Backend\Encrypted backend model (like passwords), use the %encrypt(value)% placeholder.

    You can nest environment variable placeholders inside the encryption placeholder so that secrets are resolved from the environment before being encrypted.

    Example (Nested):

    payment/provider/secret_key:
      default:
        0: '%encrypt(%env(VENDORX_SECRET_KEY)%)%'

    Warning: Use this with caution. It is generally recommended to keep sensitive data in the environment's database rather than in Git.

  4. Export Magento configuration data via CLI

    main

    You can export all configuration values from your Magento installation into a yaml or json file using the config:data:export command. After a successful export, the resulting file(s) are written to the var directory of your Magento installation. By default, files are saved to var/export/config/Ymd_His/.

    php bin/magento config:data:export
  5. Use JSON for configuration import

    main

    The module supports JSON files. Like YAML, you can use a Flattened format (using the full path as a key) or a Hierarchical format (nesting keys to represent the path).

    {
      "general": {
        "store_information": {
          "name": {
            "default": {
              "0": "Example"
            },
            "websites": {
              "1": "Example Website"
            },
            "stores": {
              "1": "Example Store DE",
              "2": "Example Store EN"
            }
          }
        }
      }
    }
  6. Use YAML for configuration import

    main

    YAML is the default file format supported by this module. You can structure your configuration using either a Flattened format (where the configuration path is a single key) or a Hierarchical format (where the path is nested via keys).

    # Hierarchical format example
    general:
      store_information:
        name:
          default:
            0: 'Example'
          websites:
            1: 'Example Website'
          stores:
            1: 'Example Store DE'
            2: 'Example Store EN'
  7. Configure the Magento configuration folder structure

    main

    To use the importer, organize your configuration files in a specific directory structure within your Magento root.

    Files in the base folder are always imported. Environment-specific files (e.g., dev, production, staging) are imported based on the environment argument provided to the CLI. If a configuration key exists in both base and the environment folder, the environment value overwrites the base value.

    Example Structure:

    magento_root
    ├── config
    │   └── store
    │       ├── base
    │       │   └── general.yaml
    │       ├── dev
    │       │   └── admin.yaml
    │       └── production
    │           └── web.yaml

    Usage Example: To import settings for the dev/therouv environment from the config/store folder:

    php bin/magento config:data:import config/store dev/therouv
  8. Use the If-Not-Set modifier for seeding configuration

    main

    To import a configuration value only if no value has been previously stored in the database, add the if: not-set modifier to the config path.

    This is ideal for seeding initial values for third-party extensions without overwriting manual changes made in the environment. The importer checks the core_config_data table; if a value exists there, the import for that path is SKIPPED (already set). Note that values from config.xml or app/etc/config.php do not count as

  9. Configure JSON flattened format scopes

    main

    In the JSON flattened format, use the full configuration path as the key. Scopes are defined as follows:

    • default: For the default scope (use "0" as the key).
    • websites: For website scope (use the Website ID as a string).
    • stores: For store scope (use the Store ID or the Store View code as a string).

    Example of setting multiple scopes for a single path:

    {
      "general/store_information/name": {
        "default": {
          "0": "Example"
        },
        "websites": {
          "1": "Example Website"
        },
        "stores": {
          "1": "Example Store DE",
          "2": "Example Store EN"
        }
      }
    }
  10. Reference the config:data:export CLI options

    main

    The config:data:export command supports several flags to control the format, structure, and scope of the exported configuration data.

    OptionFlagDescription
    --format-mOutput format: yaml or json (default: yaml)
    --hierarchical-aCreate a hierarchical structure instead of a flat one. Enable with y (default: n)
    --filename-fSpecifies the export file name. Defaults to config (unless --filePerNameSpace is used)
    --filepath-pSpecifies the export path. Defaults to var/export/config/Ymd_His/
    --include-iPath prefix(es) to export. Multiple values can be comma-separated
    --includeScopeScope name(s) to export. Multiple values can be comma-separated
    --exclude-xPath prefix(es) to exclude. Multiple values can be comma-separated
    --filePerNameSpace-sExport each namespace into its own separate file. Enable with y (default: n)
    php bin/magento config:data:export --help
    Usage:
     config:data:export [-m|--format[="..."]] [-a|--hierarchical[="..."]] [-f|--filename[="..."]] [-i|--include[="..."]] [--includeScope[="..."]] [-x|--exclude[="..."]] [-s|--filePerNameSpace[="..."]]
  11. Reference: config:data:import CLI options

    main

    The following options are available for the config:data:import command:

    OptionFlagDescription
    --base-bBase folder name (default: base)
    --format-mFormat: yaml, json (Default: yaml)
    --no-cacheDo not clear cache after config data import.
    --recursive-rRecursively go over subdirectories and import configs.
    --prompt-missing-env-vars-pPrompt in interactive mode when environment variables are found but not configured (Default: true)
    --allow-empty-directories-eDo not throw error if import directories are empty.
    --lock-lAdditionally lock imported values in app/etc/config.php (read-only in Admin).