Skip to content

Instantly share code, notes, and snippets.

@jalogut
Last active March 1, 2020 21:41
Show Gist options
  • Save jalogut/666a6098b6ebaaa859c4f62fb0717f97 to your computer and use it in GitHub Desktop.
Save jalogut/666a6098b6ebaaa859c4f62fb0717f97 to your computer and use it in GitHub Desktop.
Magento 2 code to dump only specific system settings when executing app:config:dump
// module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="VendorName_ModuleName" setup_version="0.0.1" >
<sequence>
<module name="Magento_Config"/>
</sequence>
</module>
</config>
// di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="specificConfigInitialDataProvider" type="VendorName\ModuleName\Model\Config\Source\InitialConfigSource">
<arguments>
<argument name="reader" xsi:type="object">Magento\Framework\App\DeploymentConfig\Reader</argument>
<argument name="configType" xsi:type="const">Magento\Config\App\Config\Type\System::CONFIG_TYPE</argument>
</arguments>
</virtualType>
<virtualType name="appDumpConfigSystemSource" type="Magento\Config\App\Config\Source\DumpConfigSourceAggregated">
<arguments>
<argument name="sources" xsi:type="array">
<item name="modular" xsi:type="array">
<item name="source" xsi:type="object">Magento\Config\App\Config\Source\ModularConfigSource</item>
<item name="sortOrder" xsi:type="string">10</item>
</item>
<item name="dynamic" xsi:type="array">
<item name="source" xsi:type="object">Magento\Config\App\Config\Source\RuntimeConfigSource</item>
<item name="sortOrder" xsi:type="string">100</item>
</item>
<item name="initial" xsi:type="array">
<item name="source" xsi:type="object">specificConfigInitialDataProvider</item>
<item name="sortOrder" xsi:type="string">1000</item>
</item>
</argument>
</arguments>
</virtualType>
<virtualType name="specificConfigSystemSource" type="appDumpConfigSystemSource">
<arguments>
<argument name="rules" xsi:type="array">
<item name="default" xsi:type="const">Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_EXCLUDE</item>
<item name="includeInConfigPhp" xsi:type="const">Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_INCLUDE</item>
</argument>
</arguments>
</virtualType>
<virtualType name="skipDumpEnvSystemSource" type="Magento\Config\App\Config\Source\DumpConfigSourceAggregated">
<arguments>
<argument name="sources" xsi:type="array">
</argument>
<argument name="rules" xsi:type="array">
</argument>
</arguments>
</virtualType>
<type name="Magento\Deploy\Console\Command\App\ApplicationDumpCommand">
<arguments>
<argument name="sources" xsi:type="array">
<item name="system" xsi:type="array">
<item name="source" xsi:type="object">specificConfigSystemSource</item>
<item name="pool" xsi:type="const">Magento\Framework\Config\File\ConfigFilePool::APP_CONFIG</item>
<item name="comment" xsi:type="string"></item>
</item>
<item name="system_env" xsi:type="array">
<item name="source" xsi:type="object">skipDumpEnvSystemSource</item>
<item name="namespace" xsi:type="string">skipSystem</item>
</item>
</argument>
</arguments>
</type>
<preference for="Magento\Config\Model\Config\TypePool" type="VendorName\ModuleName\Model\Config\Type\TypeConfigPool" />
<type name="VendorName\ModuleName\Model\Config\Type\TypeConfigPool">
<arguments>
<argument name="includeInConfigPhp" xsi:type="array">
<item name="dev/js/merge_files" xsi:type="string">1</item>
<item name="dev/css/merge_css_files" xsi:type="string">1</item>
<item name="dev/static/sign" xsi:type="string">1</item>
</argument>
</arguments>
</type>
</config>
// VendorName\ModuleName\Model\Config\Source\InitialConfigSource
<?php
/**
* InitialConfigSource
*/
namespace VendorName\ModuleName\Model\Config\Source;
use Magento\Framework\App\Config\ConfigSourceInterface;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\DataObject;
class InitialConfigSource implements ConfigSourceInterface
{
/**
* @var Reader
*/
private $reader;
/**
* @var string
*/
private $configType;
/**
* @var string
* @deprecated 100.2.0 Initial configs can not be separated since 2.2.0 version
*/
private $fileKey;
/**
* DataProvider constructor.
*
* @param Reader $reader
* @param string $configType
* @param string $fileKey
*/
public function __construct(Reader $reader, $configType, $fileKey = null)
{
$this->reader = $reader;
$this->configType = $configType;
$this->fileKey = $fileKey;
}
/**
* @inheritdoc
*/
public function get($path = '')
{
$data = new DataObject($this->reader->load(ConfigFilePool::APP_CONFIG));
if ($path !== '' && $path !== null) {
$path = '/' . $path;
}
return $data->getData($this->configType . $path) ?: [];
}
}
?>
// VendorName\ModuleName\Model\Config\Type\TypeConfigPool
<?php
/**
* TypeConfigPool
*/
namespace VendorName\ModuleName\Model\Config\Type;
class TypeConfigPool
{
const TYPE_INCLUDE_IN_CONFIG_PHP = 'includeInConfigPhp';
private $includeInConfigPhp = [];
public function __construct(array $includeInConfigPhp = [])
{
$this->includeInConfigPhp = $includeInConfigPhp;
}
public function isPresent($path, $type): bool
{
if (!isset($this->filteredPaths[$type])) {
$this->filteredPaths[$type] = $this->getPathsByType($type);
}
return in_array($path, $this->filteredPaths[$type]);
}
private function getPathsByType($type): array
{
if ($type != self::TYPE_INCLUDE_IN_CONFIG_PHP) {
return [];
}
return array_keys(array_filter(
$this->includeInConfigPhp,
function ($value) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment